1

Is it possible to save a Sprite into an Array?

What i want is to automatically generate Sprites (by using a for loop) then store each one into an array so i'll be able to control them later.

So that i won't need to do this a milion times:

var s:Sprite = new Sprite();
1
  • Can you be clearer? What do you mean by "so that i won't need to do this a million times"? And what will you do with the sprites later? Finally, how many sprites are we talking about? Commented Apr 4, 2012 at 12:45

3 Answers 3

2

First, declare a property sprites to hold the sprites:

var sprites:Array = [];

Then create the sprites and add them to the array:

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

Now, you can retrieve the sprites using their index in the array:

var s:Sprite = sprites[23];
// ...
Sign up to request clarification or add additional context in comments.

Comments

1

Yes you can.

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

Comments

0
//put iteration no here how many you want

var no_sprit:int = 10;    
var sprite_array = [];

for (var i:int = 0; i < no_sprit:int; i++) {    
    var my_sprite:Sprite = new Sprite();    
    sprite_array[i] = my_sprite;    
}

// you can get these sprite     
your_sprite = sprite_array[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.