Porting code from AS2 to AS3

Here’s one way to port a fun AS2 snow tutorial to AS3.0.

Below’s my AS3 class file version:

package
{

import flash.events.Event;
import flash.display.Sprite;
import flash.display.MovieClip;

public class SnowFall extends MovieClip
{

private var mywidth;
private var myheight;
private var max_snowsize;
private var snowflakes;
private var t:Sprite;

public function SnowFall()
{
init(300,200,10,50);
}

public function init(w, h, maxsnowsize, totalflakes):void
{
mywidth = w;
myheight = h;
max_snowsize = maxsnowsize;
snowflakes = totalflakes;
// quantity
for (i=0; imyheight+10) {
m.y = -20;
}

if (m.x>mywidth+20)
{
m.x = -(mywidth/2) + Math.random() * (1.5*mywidth);
m.y = -20;
} else if (m.x<-20) { m.x = -(mywidth/2) + Math.random() * (1.5*mywidth); m.y = -20; } } } } [/sourcecode] This line "t = new Snow();" goes in place of "attachMovie()," assuming you have a library symbol whose class name is "Snow." Here's a frame script version: [sourcecode language='java'] import flash.events.Event; var mywidth; var myheight; var max_snowsize; var snowflakes; var t:Sprite = new Sprite(); function init():void { mywidth = 300; myheight = 200; max_snowsize = 10; snowflakes = 50; // quantity for (i=0; imyheight+10) {
e.target.y = -20;
}

if (e.target.x>mywidth+20) {
e.target.x = -(mywidth/2) + Math.random() * (1.5*mywidth);
e.target.y = -20;
} else if (e.target.x<-20) { e.target.x = -(mywidth/2) + Math.random() * (1.5*mywidth); e.target.y = -20; } } init(); [/sourcecode]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s