开发者

AS3: Generating multiple movie clips (random locations)?

开发者 https://www.devze.com 2023-04-12 11:27 出处:网络
I\'m trying to generate multiple stars in the stage, but I keep getting an action script error. stop();

I'm trying to generate multiple stars in the stage, but I keep getting an action script error.

stop();
var i;
var arrayStars:Array;
for(i=0; i<70; i++) {
    arrayStars[i] = new Star(); //Star is a linked movie clip exported to AS3.
    arrayStars[i].x = Math.floor(Math.random() * 1650);
    arrayStars[i].y = Math.floor(Math.random() * 1060);
    addChild(arrayStars[i]);
}

The error I get is:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

    开发者_开发百科;   at Untitled_fla::MainTimeline/frame1()[Untitled_fla.MainTimeline::frame1:7]

       at runtime::ContentPlayer/loadInitialContent()

       at runtime::ContentPlayer/playRawContent()

       at runtime::ContentPlayer/playContent()

       at runtime::AppRunner/run()

       at global/runtime::ADLEntry()

Thanks in advance.


You need to initialize your Array.

var arrayStars:Array = [];

Or:

var arrayStars:Array = new Array();

Also, I'd be more inclined to do this:

var arrayStars:Array = [];

for(var i:int = 0; i<70; i++)
{
    var star:Star = new Star();

    star.x = Math.floor(Math.random() * 1650);
    star.y = Math.floor(Math.random() * 1060);

    addChild(star);
    arrayStars.push(star);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消