0
votes

I've tried to convert a nice AS2 script for starfirld effect to AS3 But i'm still getting strange errors would really appreciate if any one could help me understand what am i doing wrong here is the original AS2 code:

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

and here is my AS3 version:

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

the error message I'm getting is: "TypeError: Error #1010: A term is undefined and has no properties. at _fla::MainTimeline/update()" I understand it has a problem with the 'update' function but I'm net sure which term it refer to?

2
What line is giving you problems? The compiler should tell you the line number. - Pier

2 Answers

0
votes

I'll bet a can of juice here is your problem:

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

put these into an associative array and access them from there.

0
votes

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