Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Creating moving clouds

New Here ,
May 08, 2014 May 08, 2014

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

TOPICS
ActionScript
299
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 08, 2014 May 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2014 May 08, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 08, 2014 May 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2014 May 08, 2014

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=?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 08, 2014 May 08, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2014 May 08, 2014

I worked it out and understand it now, thank you

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 08, 2014 May 08, 2014
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines