selecting objects by adding to an array

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 or swiped over, Legion.selected becomes true and that Legion is added to the array called SelectedUnits [import]uid: 28068 topic_id: 31050 reply_id: 331050[/import]

[lua]local allLegions = {}
local SelectedUnits = {}

local function LegionSelect( event )
local legion = event.target
local i = legion.i
if (not legion.selected) then
legion.selected=true
SelectedUnits [#SelectedUnits + 1] = legion
–don’t know your needs but you also could store just the index of selected unit insteed of object
–ex:SelectedUnits [#SelectedUnits + 1] = i
end
end

There is a lot of other ways to do this. You could use tables insteed of “i” to get an objects from tables.
http://lua-users.org/wiki/TablesTutorial
local function DeployTroops()
local i = #allLegions + 1
local legion = display.newImage( “bla.png” )
legion.i = i
legion:addEventListener( “touch”, LegionSelect )
–legion.selected=false ->you don’t need this line because legion.selected=false could be writed as legion.selected=nil
allLegions[i] = legion
end

DeployTroops()[/lua] [import]uid: 138389 topic_id: 31050 reply_id: 124144[/import]

[lua]local allLegions = {}
local SelectedUnits = {}

local function LegionSelect( event )
local legion = event.target
local i = legion.i
if (not legion.selected) then
legion.selected=true
SelectedUnits [#SelectedUnits + 1] = legion
–don’t know your needs but you also could store just the index of selected unit insteed of object
–ex:SelectedUnits [#SelectedUnits + 1] = i
end
end

There is a lot of other ways to do this. You could use tables insteed of “i” to get an objects from tables.
http://lua-users.org/wiki/TablesTutorial
local function DeployTroops()
local i = #allLegions + 1
local legion = display.newImage( “bla.png” )
legion.i = i
legion:addEventListener( “touch”, LegionSelect )
–legion.selected=false ->you don’t need this line because legion.selected=false could be writed as legion.selected=nil
allLegions[i] = legion
end

DeployTroops()[/lua] [import]uid: 138389 topic_id: 31050 reply_id: 124144[/import]