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

Falling Snow AS3 code question...

Explorer ,
Nov 25, 2014 Nov 25, 2014

Copy link to clipboard

Copied

I have found some code for falling snow that I like and have successfully placed it into my Flash file as a Class. (This is all new to me, so bare with me please.) It looks great, however, I want the snow to be in front of a background image that is a city skyline, and I also want to have a movie clip layer that is in front of the snow that consists of images flying around the page, quotes, etc. Can I change this existing code to work that way? Or can I place it on it's own layer instead of using it as a Class? Any help would be greatly appreciated.

Thanks!

Here's the code:

/* Main Class */

/* Developed by Carlos Yanez */

/* Image: http://www.flickr.com/photos/andyarmstrong/89441086/ */

package

{

  import flash.display.MovieClip;

  import flash.events.Event;

  import flash.utils.Timer;

  import flash.events.TimerEvent;

  public class Snow extends MovieClip

  {

  private var flakesVector:Vector.<MovieClip> = new Vector.<MovieClip>();

  private var timer:Timer = new Timer(2000);

  public function Snow(speed:int = 3, flakesNumber = 100):void

  {

  for(var i:int = 0; i < flakesNumber; i++)

  {

  var flake:Snowflake = new Snowflake();

  flake.vel = (Math.random() * speed) + 0.5;

  flake.xSpeed = Math.floor(Math.random() * (0.5 - -0.5 + 1)) + -0.5;

  flake.scaleX = (Math.random() * 1) - 1.0;

  flake.scaleY = flake.scaleX;

  flake.x = Math.random() * stage.stageWidth;

  flake.y = Math.random() * stage.stageHeight;

  addChild(flake);

  flakesVector.push(flake);

  }

  addEventListener(Event.ENTER_FRAME, fall);

  timer.addEventListener(TimerEvent.TIMER, changeMovement);

  timer.start();

  }

  private function fall(e:Event):void

  {

  for(var i:int = 0; i < flakesVector.length; i++)

  {

  flakesVector.x += flakesVector.xSpeed;

  flakesVector.y += flakesVector.vel;

  if(flakesVector.y > stage.stageHeight)

  {

  flakesVector.x = Math.random() * stage.stageWidth;

  flakesVector.y = -flakesVector.height;

  }

  }

  }

  private function changeMovement(e:TimerEvent):void

  {

  for(var i:int = 0; i < flakesVector.length; i++)

  {

  flakesVector.xSpeed *= -1;

  }

  }

  }

}

TOPICS
ActionScript

Views

2.5K

Translate

Translate

Report

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
Explorer ,
Nov 25, 2014 Nov 25, 2014

Copy link to clipboard

Copied

By the way, I'm using Flash CS6. Thanks again for any help anyone can offer.

Votes

Translate

Translate

Report

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 ,
Nov 25, 2014 Nov 25, 2014

Copy link to clipboard

Copied

You need some way of containing the snow.  When you create content dynamically it is not tied to the timeline unless you place it within something that is.

What you can do is create an empty movieclip and place it in the upper left corner of the stage on whatever layer of the timeline you wish to have it (a layer above the background layer, below the words layer)  Then add your snow to that rather than the stage.

I am not sure how you set about instantiating the snow class, but you essentially want to add the flakes to the container movieclip you create, so instead of using

    addChild(flake);

you'd be changing it to be something like

   emptyMC.addChild(flake);

So you need to have a reference to that empty novieclip available to the snow class instance

Votes

Translate

Translate

Report

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
Explorer ,
Nov 25, 2014 Nov 25, 2014

Copy link to clipboard

Copied

So, you're saying make a small, empty box (Alpha set to 0), make it a movie clip, and then put this code in the Actions area instead of calling it as a Class? I don't mind it being continuous and lasting as long as the window/animation is open.

Also, if it helps, below is a link to where I got the code and instructions:

http://code.tutsplus.com/tutorials/create-a-customizable-snow-effect-with-actionscript-30--active-31...

Thanks!

Votes

Translate

Translate

Report

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 ,
Nov 25, 2014 Nov 25, 2014

Copy link to clipboard

Copied

No empty box is necessary, just create an empty movieclip symbol and locate it at the upper left of the stage.

Votes

Translate

Translate

Report

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
Explorer ,
Nov 25, 2014 Nov 25, 2014

Copy link to clipboard

Copied

Sorry, I guess I don't actually know how to make an empty movieclip symbol. I usually would convert a bitmap or something to a movieclip. I'll try to Google it, but if you have a quick solution, I'd be more than happy to hear it.

Votes

Translate

Translate

Report

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
Explorer ,
Nov 25, 2014 Nov 25, 2014

Copy link to clipboard

Copied

Okay, so here's a link to what I did to create an empty moviclip. http://grok.lsu.edu/Article.aspx?articleId=6936 (I know it's old, but I assume it's still the same.) Basically, Insert --> New Symbol --> MovieClip. I named it emptyMC. (Didn't name the instance.) And I also dragged it onto the layer and set it at 0, 0. Then I pasted the code from my Snow.as file that I got from the original source that I found at the above mentioned site and renamed the line you mentioned to emptyMC.addChild(flake);

Then I get and error that states

Scene 1, Layer 'FallingSnow', Frame 1, Line 61037: Packages cannot be nested.

Here's the code that's in the Actions panel of the layer named Falling Snow:

/* Main Class */

/* Developed by Carlos Yanez */

/* Image: http://www.flickr.com/photos/andyarmstrong/89441086/ */

package

{

  import flash.display.MovieClip;

  import flash.events.Event;

  import flash.utils.Timer;

  import flash.events.TimerEvent;

  public class Snow extends MovieClip

  {

  private var flakesVector:Vector.<MovieClip> = new Vector.<MovieClip>();

  private var timer:Timer = new Timer(2000);

  public function Snow(speed:int = 3, flakesNumber = 100):void

  {

  for(var i:int = 0; i < flakesNumber; i++)

  {

  var flake:Snowflake = new Snowflake();

  flake.vel = (Math.random() * speed) + 0.5;

  flake.xSpeed = Math.floor(Math.random() * (0.5 - -0.5 + 1)) + -0.5;

  flake.scaleX = (Math.random() * 1) - 1.0;

  flake.scaleY = flake.scaleX;

  flake.x = Math.random() * stage.stageWidth;

  flake.y = Math.random() * stage.stageHeight;

  emptyMC.addChild(flake);

  flakesVector.push(flake);

  }

  addEventListener(Event.ENTER_FRAME, fall);

  timer.addEventListener(TimerEvent.TIMER, changeMovement);

  timer.start();

  }

  private function fall(e:Event):void

  {

  for(var i:int = 0; i < flakesVector.length; i++)

  {

  flakesVector.x += flakesVector.xSpeed;

  flakesVector.y += flakesVector.vel;

  if(flakesVector.y > stage.stageHeight)

  {

  flakesVector.x = Math.random() * stage.stageWidth;

  flakesVector.y = -flakesVector.height;

  }

  }

  }

  private function changeMovement(e:TimerEvent):void

  {

  for(var i:int = 0; i < flakesVector.length; i++)

  {

  flakesVector.xSpeed *= -1;

  }

  }

  }

}

Votes

Translate

Translate

Report

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
Explorer ,
Dec 01, 2014 Dec 01, 2014

Copy link to clipboard

Copied

Well, just wanted to come back here and sadly say that none of this helped. But I did figure it out in a different way. Thanks for trying to help though Ned. I really appreciate it.

Votes

Translate

Translate

Report

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
Explorer ,
Sep 14, 2017 Sep 14, 2017

Copy link to clipboard

Copied

I know this is an old post but will you pleas share how you got this to work?

Votes

Translate

Translate

Report

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
Community Expert ,
Sep 14, 2017 Sep 14, 2017

Copy link to clipboard

Copied

Hi!

I quickly wrapped the animation inside of a Movie Clip. So now it's easier because it's only a matter of copying and pasting the Movie Clip in any layer needed, how many times needed.

https://goo.gl/EziMWq

Only remember:

- To have the Snowflake symbol inside the Library;

- To have the two classes (Snow.as and Snowflake.as) inside the same folder of the FLA file;

- And that you can change the snow flake artwork by only editing the Snowflake symbol.

And sorry about the quality of the drawing. Haha

I hope it helps.

Votes

Translate

Translate

Report

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
Explorer ,
Dec 14, 2017 Dec 14, 2017

Copy link to clipboard

Copied

@sandys20553341

Here's the help I just got on this for my latest question.

Hide AS3 animated snow during animation?

Didn't realize I had posted this 3 years ago. Very similar situation.

Votes

Translate

Translate

Report

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
Explorer ,
Dec 15, 2017 Dec 15, 2017

Copy link to clipboard

Copied

LATEST

I actually ended up creating the snow in After Effects and then importing my sequence images into Animate but thank you for replying!

Votes

Translate

Translate

Report

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