Solved

Flash, SnowFlake

Posted on 2011-03-15
8
659 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
ID: 35142995
what error do you get when you compile or run it, or why does it not work?
0
 
LVL 2

Expert Comment

by:nycynik
ID: 35143051
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
ID: 35143082
nycynik:
there is no error code the snow flake will just not move as it should
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Expert Comment

by:nycynik
ID: 35143572
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
 
LVL 2

Expert Comment

by:nycynik
ID: 35150355
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
ID: 35152091
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
ID: 35152158
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
ID: 35152963
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTML Relative path 9 32
html input type 3 31
jquery - show / hide div depending on day and time 19 46
hosting images 4 27
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

815 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

12 Experts available now in Live!

Get 1:1 Help Now