Finding a Gameobject with a specific value

What I Have:
—10 invisible displayObjects(Legion) with an id variable assigned to them
—each of these has 18 sprites(Soldiers) with two id variables
one that corespondes to its Legion (LegionID)
another that coresponds to its formation position (FormationID)
What I Want To Do
—Find the spritesSoldier with the LegionID of 7 and the FormationID of 13 [import]uid: 28068 topic_id: 30914 reply_id: 330914[/import]

A very basic solution is to group your sprites in a table or group then iterate over the group to find the object that matches your id/formationid. [import]uid: 147305 topic_id: 30914 reply_id: 123667[/import]

  1. I thought you could only add values to a table how can I add sprites to a table
  2. Also what is the best way to iterate over a group [import]uid: 28068 topic_id: 30914 reply_id: 123769[/import]

You can add anything into a table(even other tables).

Below is one way to loop through a table.

[lua]for i, line in ipairs(myTableNameHere) do
– add logic here, such as
if myTableNameHere.id == 7 and myTableNameHere.formationid == 32 then
–found match, do something with it i guess.
end

end[/lua]

I would put that in it’s own function where you pass in the id and formationid. When it finds a match it just returns that sprite by “return myTableNameHere[i]” [import]uid: 147305 topic_id: 30914 reply_id: 123810[/import]

A very basic solution is to group your sprites in a table or group then iterate over the group to find the object that matches your id/formationid. [import]uid: 147305 topic_id: 30914 reply_id: 123667[/import]

What I have

[code]
local allLegions = {};
local SelectedUnits = {};

local function LegionSelect( self, event )
for i, line in ipairs(allLegions) do
if allLegions==self and allLegions.selected==false then
allLegions[i].selected=true
allLegions[i]= SelectedUnits [#SelectedUnits]
end
end
end

local function DeployTroops()
allLegions[#allLegions + 1] = display.newImage( “bla.png” );
local Legion = allLegions[#allLegions]
Legion.selected=false;
Legion:addEventListener( “touch”, LegionSelect )
end

DeployTroops();

[code]
What I want:
when the displayObject Legion is touched, Legion.selected becomes true and that Legion is added to the array called local SelectedUnits
[import]uid: 28068 topic_id: 30914 reply_id: 124136[/import]

  1. I thought you could only add values to a table how can I add sprites to a table
  2. Also what is the best way to iterate over a group [import]uid: 28068 topic_id: 30914 reply_id: 123769[/import]

You can add anything into a table(even other tables).

Below is one way to loop through a table.

[lua]for i, line in ipairs(myTableNameHere) do
– add logic here, such as
if myTableNameHere.id == 7 and myTableNameHere.formationid == 32 then
–found match, do something with it i guess.
end

end[/lua]

I would put that in it’s own function where you pass in the id and formationid. When it finds a match it just returns that sprite by “return myTableNameHere[i]” [import]uid: 147305 topic_id: 30914 reply_id: 123810[/import]

What I have

[code]
local allLegions = {};
local SelectedUnits = {};

local function LegionSelect( self, event )
for i, line in ipairs(allLegions) do
if allLegions==self and allLegions.selected==false then
allLegions[i].selected=true
allLegions[i]= SelectedUnits [#SelectedUnits]
end
end
end

local function DeployTroops()
allLegions[#allLegions + 1] = display.newImage( “bla.png” );
local Legion = allLegions[#allLegions]
Legion.selected=false;
Legion:addEventListener( “touch”, LegionSelect )
end

DeployTroops();

[code]
What I want:
when the displayObject Legion is touched, Legion.selected becomes true and that Legion is added to the array called local SelectedUnits
[import]uid: 28068 topic_id: 30914 reply_id: 124136[/import]