0

我试图将一个很好的 AS2 脚本转换为 Starfirld 效果到 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: Error #1010: A term is undefined and has no properties. at _fla::MainTimeline/update()”我知道“更新”功能有问题,但我我确定它指的是哪个术语?

4

2 回答 2

0

@Discipol is right. Just wanted to add a few more notes: You can also use the display list to get the movie clip by name:

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

You've got untyped variables and your are relying on MovieClip as a dynamic class to add properties (such as speed) at runtime. For a really simple project the impact is barely noticeable, but on the long run, for bigger projects, it's worth extending Sprite if you don't use the timeline and add the properties you need:

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();
        }
    }

}

and the rest of the code would be as simple as:

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();
}
于 2013-06-27T09:23:32.103 回答
0

我敢打赌这里的果汁是你的问题:

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

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

于 2013-06-26T22:28:37.260 回答