如何将AS2雪地效果转变为AS3雪地效果?

时间:2013-12-22 15:00:09

标签: actionscript-3

所以我设法找到了我喜欢并希望使用它的雪效果,但我意识到它在AS2中,我需要它在AS3中。由于AS2和AS3之间没有小的差别,我在这里找到一些这些问题。

正如你在fla中看到的那样,我也希望通过按钮控制风速和速度。

以下是AS2雪地效果的链接:http://www.freeactionscript.com/download/realistic-snow-fall-snowflake-effect.zip

这是AS2中的代码:

//settings
var speed:Number = 2;
var wind:Number = -2;
var movieWidth:Number = 550;
var movieHeight:Number = 400;

createSnow(_root, 100);


function createSnow(container:MovieClip, numberOfFlakes:Number):Void 
{
//run a for loop based on numberOfFlakes
for (var i = 0; i < numberOfFlakes; i++) 
{       
    //set temporary variable and attach snowflake to it from the library
    var tempFlake:MovieClip = container.attachMovie("snow_mc",  "snow"+container.getNextHighestDepth(), container.getNextHighestDepth());

    //variables that will modify the falling snow
    tempFlake.r = 1+Math.random()*speed;
    tempFlake.k = -Math.PI+Math.random()*Math.PI;
    tempFlake.rad = 0;

    //giving each snowflake unique characteristics
    var randomScale:Number = random(50)+50;
    tempFlake._xscale = randomScale;
    tempFlake._yscale = randomScale
    tempFlake._alpha = random(100)+50;
    tempFlake._x = random(movieWidth);
    tempFlake._y = random(movieHeight);

    //give the flake an onEnterFrame function to constantly update its    properties
    tempFlake.onEnterFrame = function() 
    {
        //update flake position
        this.rad += (this.k / 180) * Math.PI;
        this._x -= Math.cos(this.rad)+wind;
        this._y += speed;

        //if flake out of bounds, move to other side of screen
        if (this._y >= movieHeight) {
            this._y = -5;
        }
        if (this._x >= movieWidth) 
        {
            this._x = 1
        }
        if (this._x <= 0) 
        {
            this._x = movieWidth - 1;
        }
    }
}
}

//buttons
//wind
left_btn.onRelease = function()
{
wind = 2;
}
none_btn.onRelease = function()
{
wind = 0;
}
right_btn.onRelease = function()
{
wind = -2;
}
//speed
slow_btn.onRelease = function()
{
speed = .5;
}
normal_btn.onRelease = function()
{
speed = 1
}
fast_btn.onRelease = function()
{
speed = 3
}

1 个答案:

答案 0 :(得分:1)

这将非常相似。

首先是,而不是:

var tempFlake:MovieClip = container.attachMovie("snow_mc",  "snow"+...

你想要的东西:

var tempFlake = new snow_mc();
container.addChild(tempFlake);

然后将所有属性名称(如_x等)转换为其AS3等效项(无下划线,scaleX到位f _xscale等),Math.random() * 50代替random(50)

将所有onRelease替换为addEventListener(MouseEvent.CLICK, function() {})

最后,您需要一个框架循环,而不是tempFlake.onEnterFrame,如:

function onFrame(event: Event): void {
   foreach(var child: MovieClip in container) {
      child.rad += ... etc
   }
}
addEventListener(Event.ENTER_FRAME, onFrame);

这些步骤应足以使其作为AS3运行。一旦它运行,你可以通过创建一个封装了一个雪花的所有属性和更新的SnowFlake类来进一步使它成为 more AS3