Solved

Flash, SnowFlake

Posted on 2011-03-15
8
653 Views
Last Modified: 2013-11-11
I have this actonscript code that i am developing in flash cs4 for a snow storm and I cannot seem to get it to work. I found the code on the net this is the address http://www.kirupa.com/developer/flashcs4/falling_snow_as3_cs4_pg1.htm  I am not sure what the problem is.

package
{
import flash.display.*;
import flash.events.*;
 
public class Snowflake extends MovieClip
{
private var xPos:Number = 0;
private var yPos:Number = 0;
 
private var xSpeed:Number = 0;
private var ySpeed:Number = 0;
 
private var radius:Number = 0;
 
private var scale:Number = 0;
private var alphaValue:Number = 0;
 
private var maxHeight:Number = 0;
private var maxWidth:Number = 0;
 
public function Snowflake()
{
SetInitialProperties();
}
 
public function SetInitialProperties()
{
//Setting the various parameters that need tweaking
xSpeed = .05 + Math.random()*.1;
ySpeed = .1 + Math.random()*3;
radius = .1 + Math.random()*2;
scale = .01 + Math.random();
alphaValue = .1 + Math.random();
 
var stageObject:Stage = this.stage as Stage;
maxWidth = stageObject.stageWidth;
maxHeight = stageObject.stageHeight;
 
this.x = Math.random()*maxWidth;
this.y = Math.random()*maxHeight;
 
xPos = this.x;
yPos = this.y;
 
this.scaleX = this.scaleY = scale;
this.alpha = alphaValue;
 
this.addEventListener(Event.ENTER_FRAME, MoveSnowFlake);
}
 
function MoveSnowFlake(e:Event)
{
xPos += xSpeed;
yPos += ySpeed;
 
this.x += radius*Math.cos(xPos);
this.y += ySpeed;
 
if (this.y - this.height > maxHeight)
{
this.y = -10 - this.height;
this.x = Math.random()*maxWidth;
}
}
}
}
0
Comment
Question by:fireandice68
  • 5
  • 3
8 Comments
 
LVL 2

Expert Comment

by:nycynik
Comment Utility
what error do you get when you compile or run it, or why does it not work?
0
 
LVL 2

Expert Comment

by:nycynik
Comment Utility
So i used the code, works fine for me.

I added some snowflakes to the stage, and then attached this code, and it moves from top to bottom, kind of like snowflakes would.

--

That said, you probably want to attach this code only to some movie-clip, not to the entire stage, so that the snow moves, and the snowman stays in place.

The best way to do that is make a snowflake class, and attach this as the main function of the snowflake class, and then add snowflakes to the stage.

Another way, is to do what I did here, but this is only if you have just one snowflake.  Add the snowflake to the stage, click f8 to convert to a symbol, type the instnace name "Flake", save everything, and run it.

package 
{ 
import flash.display.*; 
import flash.events.*; 
  
public class Brix extends MovieClip 
{ 
private var xPos:Number = 0; 
private var yPos:Number = 0; 
  
private var xSpeed:Number = 0; 
private var ySpeed:Number = 0; 
  
private var radius:Number = 0; 
  
private var scale:Number = 0; 
private var alphaValue:Number = 0; 
  
private var maxHeight:Number = 0; 
private var maxWidth:Number = 0; 
  
public function Brix() 
{ 
SetInitialProperties();
} 
  
public function SetInitialProperties() 
{ 
//Setting the various parameters that need tweaking 
xSpeed = .05 + Math.random()*.1; 
ySpeed = .1 + Math.random()*3; 
radius = .1 + Math.random()*2; 
scale = .01 + Math.random(); 
alphaValue = .1 + Math.random(); 
  
var stageObject:Stage = this.stage as Stage; 
maxWidth = stageObject.stageWidth; 
maxHeight = stageObject.stageHeight; 
  
Flake.x = Math.random()*maxWidth; 
Flake.y = Math.random()*maxHeight; 
  
xPos = Flake.x; 
yPos = Flake.y; 
  
Flake.scaleX = Flake.scaleY = scale; 
Flake.alpha = alphaValue; 
  
this.addEventListener(Event.ENTER_FRAME, MoveSnowFlake);
} 
  
function MoveSnowFlake(e:Event) 
{ 
xPos += xSpeed; 
yPos += ySpeed; 
  
Flake.x += radius*Math.cos(xPos); 
Flake.y += ySpeed; 
  
if (Flake.y - Flake.height > maxHeight) 
{ 
Flake.y = -10 - this.height; 
Flake.x = Math.random()*maxWidth;
}
}
}
}

Open in new window

0
 

Author Comment

by:fireandice68
Comment Utility
nycynik:
there is no error code the snow flake will just not move as it should
0
 
LVL 2

Expert Comment

by:nycynik
Comment Utility
so the code moves one mc from top to bottom, slowly, at various sizes (at random).

Works good for me, looks like snow.  Moves a little slow I guess, but it works.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:nycynik
Comment Utility
Is it that you just dont have the snowflakes?

Make sure the graphic of the snowflake is a movie clip.  Click on the graphic, hit f8 to convert it, and make sure the drop down shows moveiclip.
0
 

Author Comment

by:fireandice68
Comment Utility
Yes I made it a movie clip right from the start mmmm just not sure it might be that the .as file is in the wrong place I have it in a folder is that right or should it be in the same location as the .fla file
0
 
LVL 2

Accepted Solution

by:
nycynik earned 250 total points
Comment Utility
in your fla, you must set the class.

To do that, save the file, with the name "Snowflake.as"

Then open the fla, and in the properties, fine the box for "Class"

Enter "Snowflake" - note that there is no .as, remove that part, its the name of the class, not the name of the file.

Thats will link the class file in.
0
 

Author Comment

by:fireandice68
Comment Utility
Thx I have it working now, I deleted it from
My computer and started again and put it all in the same directory and it works
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now