# \[FMX\] more function HELP please

**URL:** <https://forum.kirupa.com/t/fmx-more-function-help-please/18522>\
**Category:** flash\
**Created:** [April 17, 2003, 2:37pm UTC](https://forum.kirupa.com/t/fmx-more-function-help-please/18522 "2003-04-17T14:37:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgelston](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/tgelston/32/54_2.png) [@tgelston](https://forum.kirupa.com/u/tgelston)\
**Post date:** [April 17, 2003, 2:37pm UTC](https://forum.kirupa.com/t/fmx-more-function-help-please/18522/1 "2003-04-17T14:37:36Z")

</div>

Okay - people have been a great help so far, thanks to everyone - I have some follow-up questions

Senocular gave the following bit of code in response to one of my questions -

lazerBeam.onEnterFrame = function(){  
for(var i=1; i \<= 100; i++){  
if (this.hitTest(\_root[“asteroid”+i])) trace(“boom”);  
}

MY PROBLEM: how do I call this function up? Meaning how do I use it. Actually I have not been successful in calling normal functions outside of the clip I declare them in either -

I have tried \_global.functionname(); and \_root.actions.functionname();

Here is an actual example of what I want to do

1. duplicate a MC
2. randomly position the duplicated MC at X = 600 Y = random
3. move rock across stage from right to left

I have a function that I created to accomplish # 1 – I put it in the load section of my actions clip and called it in the enterframe section, it works fine. I cannot get #2 and #3 to work.

I can easily achieve these not using functions but I would like to learn more modular programming and I didn’t think it would be so difficult

Thanks,  
Tobias

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 17, 2003, 2:46pm UTC](https://forum.kirupa.com/t/fmx-more-function-help-please/18522/2 "2003-04-17T14:46:26Z")

</div>

All right, so if you want to create a hundred asteroids, you’d have to do something like that (I put duplicateMovieClip, but you could also attachMovieClip):

```auto
for (var p=0 ; p < 100 ; p++){
  var clip = asteroid.duplicateMovieClip("asteroid"+p,p);
  clip._x = 600;
  clip._y = random (400);
}

```

Now if you want them to move right to left, you’re going to need a nice little function

```auto
function moveToLeft(){
  this._x -= this.speed;
}

```

This function will move the object that calls it by the amount **this.speed** which we’ll have to define.

You want to motion to be continous, so you’ll put that in an enterFrame like so:

```auto
for (var p=0 ; p < 100 ; p++){
  var clip = asteroid.duplicateMovieClip("asteroid"+p,p);
  clip._x = 600;
  clip._y = random (400);
  clip.speed = random (5) +5;
  clip.onEnterFrame = moveToLeft;
}

```

pom 🥚

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 17, 2003, 3:29pm UTC](https://forum.kirupa.com/t/fmx-more-function-help-please/18522/3 "2003-04-17T15:29:15Z")

</div>

Thanks for the quick reply.

I was going to ask what goes where. . . but then I stopped and thought - and created a quick example - all of that code can sit in the first frame of the timeline!! That is pretty cool, nothing needs to go on the asteroid clip.

Thanks,  
Tobias “on my way to learning functions” Gelston

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 17, 2003, 4:20pm UTC](https://forum.kirupa.com/t/fmx-more-function-help-please/18522/4 "2003-04-17T16:20:20Z")

</div>

okay I spoke to soon

what you gave me is not exactly what I want and I have been trying to modify it

I want the asteroid to continuosly be duplicated and moved across the stage. I created something like this

[AS]function \_root.makebits() {  
chance = Math.round(Math.random()\*15);  
if (chance == 7) {  
\_root.rock.duplicateMovieClip(“rock”+d, d+1000);  
d++;  
}  
}  
[/AS]

I declare the function in the load section of a clip that contains all my actions and the run the function by sticking it in

[AS]  
onClipEvent (enterFrame) {  
\_root.makebits()  
}  
[/AS]

How can I use your snazy way to make this better - or is my way acceptable?

I ask becuase I am responsible for teaching Flash to students and teachers and when I will eventually teach AS I want to make sure I explain things correctly. ALSO does anyone know of any good tutorials / writeup regarding use of functions in Flash?

Thanks,  
Tobias

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [April 17, 2003, 7:47pm UTC](https://forum.kirupa.com/t/fmx-more-function-help-please/18522/5 "2003-04-17T19:47:38Z")

</div>

Okay I have something close to what I want BUT  
Why does the following code require 2 frames to run - if I only have one frame only the duplication will work, they will not move left. Now I can place this on a MC and it will work in one frame but why? I thought the best way was to declare everything in the first frame of your root timeline.  
[AS]  
//define numbers  
rockSpeed = 10;

//Create Functions  
function dupFunction(){  
chance = Math.round(random (5));  
if (chance == 3){  
p += 1;  
\_root.clip = this.duplicateMovieClip(“dupBall” + p, p);  
\_root.clip.\_y = random (400);  
\_root.clip.\_xscale = random (50) + 10;  
\_root.clip.\_yscale = \_root.clip.\_xscale;  
}  
}  
function moveRemoveFunction(){  
//moves clip to left  
this.\_x -= rockSpeed;  
//removes clip when off stage  
if (this.\_x \< 0){  
this.removeMovieClip();  
}  
}  
//Apply Functions  
\_root.clip.onEnterFrame = moveRemoveFunction;  
\_root.ball.onEnterFrame = dupFunction;  
[/AS]

Thanks for helping me get my brain around these functions - I am learning. . . slowly!

-Thanks - Tobias
