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]
