Removing from an array of DisplayGroups?

Hello!

I’m still getting the hang of LUA, so bear with me.

I have this:

-- Set up our ship array  
shipArray = {}  
empireNumber = math.random (1, 5)  
for i=1,numberOfShips do  
 local x = math.random (0, screenWidth)  
 local y = math.random (0, screenHeight)  
 shipArray[i] = display.newGroup()  
 --shipArray[i]:insert(display.newImage(getShipFile(empireNumber,"titan")),true)  
 shipArray[i]:insert(display.newImage(getRandomShip(empireNumber)),true)  
 shipArray[i].x = x;  
 shipArray[i].y = y;  
 shipArray[i].rotation = 90  
 shipArray[i].xScale = shipScale  
 shipArray[i].yScale = shipScale  
 shipArray[i]:addEventListener( "touch", selectShip )  
 physics.addBody( shipArray[i] , crateMaterial )  
 shipArray[i].rotation = math.random(0,359)  
 shipArray[i].linearDamping = 5  
 shipArray[i].angularDamping = 1  
 shipArray[i].index = i   
 local txt=display.newText('Ship '..i,0,0,nil,25)  
 txt:setTextColor( 190, 255, 131, 150 )  
 shipArray[i].label = txt;  
 shipArray[i]:insert(txt,0)]]   
end  

First off, is this “correct” usage of the Group function? I’d like an array of my ship objects that I can attach text to for labeling my ship. It works fine so far…

But now I was wanting to change the graphics of my ship “on the fly”.

Is there a simple way to replace the graphic that my object is using, or do I need to use the groupRemove? And if I DO need to use the groupRemove, how would I use it properly?

I tried this function to replace ALL my ships images, but I’m not having luck, it’s only replacing ONE ship object’s graphics, and putting it in the upper left corner…

[code]

function changeShips()
for i=numberOfShips,1,-1 do
oldX = shipArray[i].x;
oldY = shipArray[i].y;
oldRotation = shipArray[i].rotation;
oldXScale = shipArray[i].xScale;
oldXScale = shipArray[i].yScale;
print("Old x: “…oldX…” Old y: "…oldY)
shipArray[i]:removeSelf()
shipArray[i]:insert(display.newImage(getRandomShip(2)),true)
shipArray[i].x = oldX;
shipArray[i].y = oldY;
shipArray[i].rotation = 90
shipArray[i].xScale = shipScale
shipArray[i].yScale = shipScale
shipArray[i]:addEventListener( “touch”, selectShip )
–[[
physics.addBody( shipArray[i] , crateMaterial )
shipArray[i].rotation = math.random(0,359)
shipArray[i].linearDamping = 5
shipArray[i].angularDamping = 1
shipArray[i].index = i]]–

end
end
[/code] [import]uid: 11636 topic_id: 4212 reply_id: 304212[/import]

what value do you get for [lua]print(numShips)[/lua]

you could try

[lua]for i = #shipArray, 1, -1 do[/lua]

(# = length)

also personally i would do

[lua]local ship = display.newImage(…etc…)
ship.x=x
ship.y=y
…etc…
shipArray[i] = ship[/lua]

but that’s a personal preference
[import]uid: 6645 topic_id: 4212 reply_id: 13261[/import]

Well, I found a way to do it, but it’s separated into two parts:

First, I do a for next loop on my ship array and removeSelf() on each of their display groups

 for i=1,numberOfShips do  
 shipArray[i]:removeSelf()  
 end  

Next, I can reload an image to :insert into the ship’s image group.

Meh, not the most elegant, but I’m still getting used to LUA so I’m excusing myself for sloppy code. :slight_smile:

@jmp909 Hey that’s a nice layout, kind of makes sense to me…‘finalizing’ the array once all data is entered. Thanks for the tip! [import]uid: 11636 topic_id: 4212 reply_id: 13541[/import]