Copy link to clipboard
Copied
Hi
I have this assignment where we have to use a display list. I am trying to create clouds moving on the background.
I'm sure my code is very clumsy. I was able to get the clouds on my stage but I can only get one to move.
Please, I can't seem to wrap my head around this and as you can see I'm a novice.
import flash.display.*;
import flash.events.*;
var myBackground:BackGround = new BackGround();
addChild(myBackground);
var myCloud:MyCloud;
for (var i:Number = 0; i<6; i++)
{
myCloud = new MyCloud();
addChild(myCloud);
var randomValue: Number = Math.random()*2;
myCloud.x = Math.random()*stage.width;
myCloud.scaleX = myCloud.scaleY = randomValue;
}
myCloud.addEventListener(Event.ENTER_FRAME, flyBy);
function flyBy(event:Event):void
{
myCloud.x +=6;
if (myCloud.x> stage.stageWidth)
{
myCloud.x = 0;
}
}
addChild(myCloud);
Copy link to clipboard
Copied
For starters, you add the ENTER_FRAME listener after the loop where you create the clouds, so it is only being applied to the last cloud that was created. Move that listener inside the for loop.
Similarly, in the flyBy function you are targeting myCloud, which is only going to be the last cloud created. Inside that function try changing "myCloud" to "MovieClip(event.currentTarget)" so that it is targeting the cloud that has the klistener that triggered the function.
Copy link to clipboard
Copied
Thank you soooo much I've trying to figure that one out for two day.
One more problem how do I get the other clouds to re enter the stage?
Copy link to clipboard
Copied
If you change the flyBy function as I indicated it should do that... though you might end up wanting to add the width of the cloud as part of the processing as well once you see how it works without it.
Copy link to clipboard
Copied
all the clouds move initially, but then only one re enters?
{
myCloud = new MyCloud();
addChild(myCloud);
var randomValue: Number = Math.random()*2;
myCloud.x = Math.random()*stage.width;
myCloud.scaleX = myCloud.scaleY = randomValue;
myCloud.addEventListener(Event.ENTER_FRAME, flyBy);
function flyBy(event:Event):void
{
MovieClip(event.currentTarget).x +=6;
if (myCloud.x> stage.stageWidth)
{
myCloud.x = 0;
}
}
}
Not quite sure what you mean about the width?
myCloud.width=?
Copy link to clipboard
Copied
You didn't do what I said to do... You only replaced one of the myCloud's in that function. You need to replace them all or else any you don't still only point to the last one created. Reread that to understand it before you change anything... it's important to understand.
Copy link to clipboard
Copied
I worked it out and understand it now, thank you
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now