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);
}
精彩评论