Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Flash, SnowFlake

Posted on 2011-03-15
8
662 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses four methods for overlaying images in a container on a web page
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

829 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