OK, fresh from my last problem that was solved, here’s what I have an table of objects that are display groups:
local touchShip = function( event )
if "ended" == event.phase then
print("We touched "..event.target.name)
end
end
shipTable = {}
-- So we're making a table of tables...not sure if this is the route to go
for i=1,10 do
-- set our shipTable[i] as a display group
shipTable[i] = display.newGroup()
-- Create a temporary "Spaceship" as a rectangle
local tempRect = display.newRect( 0,0,10,20 )
-- Color it...
tempRect:setFillColor(255,0,0,255)
-- insert the ship graphic into the group.
shipTable[i]:insert(tempRect,false)
-- Give the ship a name
shipTable[i].name = "Ship: "..i
local tempText = display.newText(shipTable[i].name,0,0,nil,14)
shipTable[i]:insert(tempText,false)
-- Place our ship randomly and set a rotation...
shipTable[i].x = math.random(0,300)
shipTable[i].y = math.random(0,300)
shipTable[i].rotation = math.random(0,359)
-- Attach the ship graphic to an event
shipTable[i]:addEventListener( "touch", touchShip )
end
----------------------------------------------------------------
-- MAIN LOOP
----------------------------------------------------------------
local function main( event )
for i=1,10 do
shipTable[i].rotation = shipTable[i].rotation + 1
end
end
Runtime:addEventListener( "enterFrame", main )
As you can see, the ships’ labels rotate along with the ship itself. Same thing when I have physics applied to the ships.
This is all expected of course, since they’re in the same display group: shipTable[i].
My question is, how would I attach labels to the ships and NOT have the text rotate along with ‘em? My fallback plan is a table of text objects that I constantly update with their corresponding ships’ coordinates, just not sure how efficient this is:
[code]
textTable = {}
for i=1,10 do
local tempText = display.newText(shipTable[i].name,0,0,nil,14)
r = math.random(0,255)
g = math.random(0,255)
b = math.random(0,255)
tempText:setTextColor(r,g,b)
– set the text locations to the corresponding ship arrays’ locations
tempText.x = shipTable[i].x
tempText.y = shipTable[i].y
textTable[i] = tempText
end
local function updateTextLocation()
for i=1,10 do
textTable[i].x = shipTable[i].x
textTable[i].y = shipTable[i].y
end
end
– MAIN LOOP
local function main( event )
for i=1,10 do
shipTable[i].rotation = shipTable[i].rotation + 1
shipTable[i].x = shipTable[i].x + 1
updateTextLocation()
end
end[/code]
It works fine, and I don’t plan on having billions of my awesome spaceships on-screen at once…just checking to see if there were an easier way…and I’m not complaining! That was pretty easy anyways!
[import]uid: 11636 topic_id: 4429 reply_id: 304429[/import]