Get clone in array
ullvieib

Hi!

I create clones in an array, but how do I get the sceneobjects???

 

var tray = [];            for(var i = 0; i<5; i++){                tray[i] = wade.getSceneObject("bomb").clone();                tray[i].setPosition(bombx,bomby);                wade.addSceneObject(tray[i]);                bombx = bombx+70;            }

 

1 Comment
foxcode

Hi ullvieib

 

Your tray array will contain all the scene objects, so I assume you want to also have access to them via wade.getSceneObject() ?

 

When you clone an object with .clone(), wade renames the object as all objects must have a unique identifier. To get this new name for use in wade.getSceneObject, you can simply call getName() on the objects

tray[0].getName();           // This will return the name, if it is empty, you can set your own nametray[0].setName("bomb_1");   // Set namevar myBombName = tray[0].getName(); // Will now be "bomb_1"// So in your examplevar tray = [];var names = [];for(var i = 0; i<5; i++){    tray[i] = wade.getSceneObject("bomb").clone();    tray[i].setName("bomb_" + i);      // Set the name of each bomb to "bomb_" plus the current index    names[i] = tray[i].getName();     // Store the names in an array for later use (optional)          tray[i].setPosition(bombx,bomby);    wade.addSceneObject(tray[i]);    bombx = bombx+70;}

I do however wonder why you would need to access the scene objects like this since you already have them in a tidy array. It would be helpful to know what you are trying to do with these objects which requires you to use getSceneObject.

Post a reply
Add Attachment
Submit Reply
Login to Reply