views:

146

answers:

1

How do I load library objects by string list using Flash and AS3?
I need to understand string lists, arrays, and sprites better.

WHAT I WANT TO DO
Load enemies to stage using a string list "Orange ball, red ball, green ball etc"

EXPLANATION
The tutorials I've been given are too basic, or they are incomplete examples that don't explain the classes being used.

Trace statements are not showing me how I'm accessing the name or class of an object. I need examples. Non-packaged classes preferred. Thanks.

WHAT I GOT WORKING "I knew I'd mix up the class and name"
SYMBOL PROPERTIES
name is they're not name specific, I call mine noname"
class is Pyramid

//Creates 10 enemies and trace statement
import flash.display.Sprite;
var sprites:Array = new Array();
for(var i:Number=0;i<10;i++){
    //var test:Sprite = new Sprite();
    var test:Pyramid = new Pyramid();
        sprites.push(test);
    addChild(test);
    test.name = "littlebox_"+ i
    trace(test.name)
    with(test){

        x=i*25;

       }
}

/////////////////////////////////////////////////////////////////////// 

/*debu's example*/
//Creates 10 enemies 
var enemyArray:Array = new Array(); 

for (var i:int = 0; i < 10; i++) 
{ 
   var noname:Pyramid = new Pyramid(); 
   noname.x = i*10; //this will just assign some different x and y value depending on i. 
   noname.y = i*11; 
   enemyArray.push(noname); //put the enemy into the array 
   addChild(noname); //puts it on the stage 
}

///////////////////////////////////////////////////////////////////////

//centered and trace statement
var sprite:Sprite;
var noname:Pyramid = new Pyramid; 
//var noname:Pyramid2 = new Pyramid2; 
sprite = new Sprite();
sprite.name = "Pyramid" + 1;
addChild( noname);
noname.x = stage.stageWidth/3;
noname.y = stage.stageHeight/3;
trace( getChildByName( "Pyramid" + 1 ) ); // [object Sprite]

alt text

PREVIOUS MODIFIED

var enemyList:Object = new Object();
        //var enemy:Sprite = new Sprite();
var enemy:BadGuy = new BadGuy();
enemy.name = "BadGuy";
enemyList [enemy.name] = enemy;

//var enemyList:Object = new Object();  
//for (var i:int = 0; i < 10; i++) {
        //var enemy:Sprite = new Sprite(); 
//var enemy:BadGuy = new BadGuy(); 
//enemy.name = "BadGuy" + i;  
//enemyList [enemy.name] = enemy;  
//}  

//for (var i:String in enemyList){  
//var enemy:Sprite = enemyList[i]  
//do something to enemy sprite  
//}

PLEASE POST MORE ARRAY STUFF

+2  A: 

What I'd do in that situation is just use an ordinary Array. Declare it like this:

var enemyArray:Array = new Array();

Then it's ready to accept objects. Then create a for loop, much like you've done, to add as many enemies as you want to the Array and give them some positional info, and also to add them to the Stage:

//Creates 10 enemies
for (var i:int = 0; i < 10; i++)
{
   var enemy:Sprite = new Sprite();
   enemy.x = i*10; //this will just assign some different x and y value depending on i.
   enemy.y = i*11;
   enemyArray.push(enemy); //put the enemy into the array
   addChild(enemy); //puts it on the stage
}

This will put 10 enemy objects (which you will need to define, I assume you know how to link a new class to an object in the Flash IDE's library?) on the stage. From here you have the array populated with those enemies, and you can cycle through it to move them around, do collision detection on them, kill them off and remove them from the stage, etc.

The way you did it seems to put them into an Object according to their names, which is equally do-able I guess. I can't see why you would necessarily need to access them by name, in which case the Array way simplifies it somewhat.

Note also that this isn't loading enemies to stage using a string list, as that isn't really necessary - unless you were first putting the file locations of a bunch of images on your HD (to use as game sprites) in an array as Strings; then subsequently for-looping through that to load them to stage.

I hope this helps, let me know if you have any further questions, or if this isn't the answer you were looking for.

debu
It seems correct. I made two symbols and flipped the name and class to make sure. No error, it just doesn't show anything. check out my link. http://videodnd.weebly.com/
VideoDnd
Yeah, as I said you need to know how to link an object in Flash to a class name. Right click your 'enemy' object in the Flash library, and click 'properties'. Then make sure 'export for ActionScript' is ticked, and put in a name (starting with a capital) in the 'Class' box - say Badguy. Press ok, it'll give you a warning saying it can't find a definition for the class path, as you haven't created an actual .AS file for it - but that doesn't matter for now.Once you've done that, change the line:var enemy:Sprite = new Sprite();tovar enemy:Badguy = new Badguy();This'll draw it to stage.
debu
I'll check properties/class again, thanks.
VideoDnd