Question

Is it possible better to declare variables without having to write one by one? I need 200 but... no way.

My code i have:

var var0:Array = new Array();

var var01:Sprite = new Sprite();
var var02:Sprite = new Sprite();
var var03:Sprite = new Sprite();
etc to 200...

And inside for loop:

for( var i:Number = 0; i < arrayBig.length; i++ ){
    var0[i] = new Sprite();
    ...
}

But Why cant I write var var0[i]:Sprite = new Sprite(); in the statments?

The error is Error #1086 Syntax error: expecting semicolon before

Thank you very much!!

Was it helpful?

Solution

You cannot index independent variables and you cannot concatenate variable names to form names of other variables (well, not the way you attempted).

You need to create an array of Sprite objects. An array is a data structure that stores several things at once. (A one-dimensional array is called a vector.)

To store sprites in a vector, you'd write:

var sprites:Vector.<Sprite> = new Vector.<Sprite> ();

for ( var i:int = 0; i < 200; i++ )
{
    sprites.push ( new Sprite () );
}

This loop creates 200 Sprite instances and stores all of them in the sprites array (really, Vector). You can then access individual sprites by simply indexing them:

sprites[n]....

where n goes from 0 to N-1 if the Vector has N total elements.

OTHER TIPS

i will answer for your question.

"But Why cant I write var var0[i]:Sprite = new Sprite(); in the statements?" we cant override one object with that of the other same object in the class. thats the reason, we are declaring as "var0[i] = new Sprite();". spl case for sprite object in flex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top