[FMX] more function HELP please

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

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):

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

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:

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 :egg:

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

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

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