Skip to main content
Known Participant
May 8, 2014
Question

Creating moving clouds

  • May 8, 2014
  • 7 replies
  • 373 views

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

This topic is closed to new replies. Start a new post to keep the conversation going.

7 replies

Ned Murphy
Legend
May 8, 2014

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.

Known Participant
May 8, 2014

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?

Ned Murphy
Legend
May 8, 2014

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.