Copy link to clipboard
Copied
Hi,
I have a simple code adding movie clips. On the same time the movie clips are moving along the stage.
The first movie clip is already on the stage and the code should add additional three. The three movie clips defined with the if statements, are added correctly (to origX and origY positions).
But after adding the three, it continues adding them. And not to the firstly defined x and y, but to x=0 and y=0.
How do I stop it after the three are added?
The code is in a separate class file.
Many thanks in advance!
public class Duck extends MovieClip
{
var origX: Number;
var origY: Number;
var i: int;
var newDuck: part;
public function Duck()
{
origX = this.x;
origY = this.y;
this.addEventListener(Event.ENTER_FRAME, moveDuck);
}
function moveDuck(event: Event): void
{
this.x += 0.25;
if(this.x == origX + 50)
{
addDuck();
}
if(this.x == origX + 75)
{
addDuck();
}
if(this.x == origX + 100)
{
addDuck();
}
}
function addDuck(): void
{
newDuck = new part();
parent.addChild(newDuck);
newDuck.scaleX = 0.55;
newDuck.scaleY = newDuck.scaleX;
newDuck.x = origX;
newDuck.y = origY;
}
}
You function Duck() instanciates an enterFrame function that actually creates the duck instances on the stage. Once begun, there is nothing telling it to stop creating new ducks. You might want to add a for loop that counts up to the number of ducks that you want and then kills the enterframe function. I don't know why your x and y values are resetting to 0.
Copy link to clipboard
Copied
You function Duck() instanciates an enterFrame function that actually creates the duck instances on the stage. Once begun, there is nothing telling it to stop creating new ducks. You might want to add a for loop that counts up to the number of ducks that you want and then kills the enterframe function. I don't know why your x and y values are resetting to 0.
Copy link to clipboard
Copied
Thank you Rob for your comment.
I finally separated the code between timeline and class file and got it working this way.
The original thought to do it all in a class file was above my knowledge level.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now