将AS2星空代码转换为AS3时出错

时间:2013-06-26 21:17:17

标签: actionscript-3 flash actionscript-2

我试图将starfirld效果的AS2脚本转换为AS3但是我仍然会遇到奇怪的错误 我真的很感激,如果有人能帮助我理解我做错了什么 这是原始的AS2代码:

var stars = 100;
var maxSpeed = 16;
var minSpeed = 2;
var i = 0;
while (i < stars)
{
    var mc = this.attachMovie("star", "star" + i, i);
    mc._x = random(Stage.width);
    mc._y = random(Stage.height);
    mc.speed = random(maxSpeed - minSpeed) + minSpeed;
    var size = random(2) + 6.000000E-001 * random(4);
    mc._width = size;
    mc._height = size;
    ++i;
} // end while
this.onEnterFrame = function ()
{
    for (var _loc3 = 0; _loc3 < stars; ++_loc3)
    {
        var _loc2 = this["star" + _loc3];
        if (_loc2._y > 0)
        {
            _loc2._y = _loc2._y - _loc2.speed;
            continue;
        } // end if
        _loc2._y = Stage.height;
        _loc2.speed = random(maxSpeed - minSpeed) + minSpeed;
        _loc2._x = random(Stage.width);
    } // end of for
};

这是我的AS3版本:

import flash.events.Event;
import flash.events.MouseEvent;

function starField():void 
{
    var stars:int = 100;
    var maxSpeed:int = 16;
    var minSpeed:int = 2;
    var i:int = 0;
    while (i < stars)
    {
        var mc = new Star();
        addChild(mc)
        mc._x = Math.random()(stage.stageWidth);
        mc._y = Math.random()(stage.stageHeight);
        mc.speed = Math.random()(maxSpeed - minSpeed) + minSpeed;
        var size = Math.random()(2) + 6.000000E-001 * Math.random()(4);
        mc._width = size;
        mc._height = size;
        ++i;
    } // end while
}

addEventListener(Event.ENTER_FRAME, update);
function update(_e:Event):void
{
    for (var _loc3 = 0; _loc3 < 100; ++_loc3)
    {
        var _loc2 = this["star" + _loc3];
        if (_loc2._y > 0)
        {
            _loc2._y = _loc2._y - _loc2.speed;
            continue;
        } // end if
        _loc2._y = stage.stageHeight;
        _loc2.speed = Math.random()(maxSpeed - minSpeed) + minSpeed;
        _loc2._x = Math.random()(stage.stageWidth);
    } // end of for
};

我得到的错误消息是:“TypeError:错误#1010:一个术语未定义且没有属性。在_fla :: MainTimeline / update()” 我知道它有'更新'功能有问题,但我确定它指的是哪个术语?

2 个答案:

答案 0 :(得分:0)

我敢打赌,这里有一罐果汁是你的问题:

 var _loc2 = this["star" + _loc3];

将这些放入关联数组并从那里访问它们。

答案 1 :(得分:0)

@Discipol是对的。 只是想补充几点说明: 您还可以使用显示列表按名称获取影片剪辑:

var _loc2:MovieClip = MovieClip(getChildByName("star" + _loc3));

您已经获得了无类型变量,并且您依赖MovieClip作为动态类来在运行时添加属性(例如速度)。对于一个非常简单的项目,影响几乎不可察觉,但从长远来看,对于更大的项目,如果您不使用时间轴并添加所需的属性,则值得扩展Sprite:

package  {

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


    public class Star extends Sprite {

        private var speed:Number;
        private var minSpeed:Number;
        private var maxSpeed:Number;

        public function Star(min:Number,max:Number) {
            minSpeed = min;
            maxSpeed = max;
            var size = (Math.random()*2) + 1.82211880039 * (Math.random()*4);
            width = size;
            height = size;
            this.addEventListener(Event.ADDED_TO_STAGE,reset);
        }
        private function reset(e:Event = null):void{
            speed = (Math.random() * (maxSpeed-minSpeed)) + minSpeed;
            x = Math.random() * stage.stageWidth;
            if(e != null) y = Math.random() * stage.stageHeight;//initialized from added to stage event
            else          y = stage.stageHeight;//otherwise reset while updating
        }
        public function update():void{
            if (y > 0) y -= speed;
            else       reset();
        }
    }

}

其余的代码就像:

var stars:int = 100;
var starClips:Vector.<Star> = new Vector.<Star>(stars,true);//a typed fixed vector is faster than a dynamically resizable untyped Array
for(var i:int = 0 ; i  < stars; i++) starClips[i] = addChild(new Star(16,2)) as Star;

this.addEventListener(Event.ENTER_FRAME,updateStars);
function updateStars(e:Event):void{
    for(var i:int = 0 ; i  < stars; i++) starClips[i].update();
}