I have the following code, which generates 24 “alien” objects, declares a local function with the movement each object must have, the problem is that it only applys to the last generated object, i’ve searched all of the physics and group docs and haven’t found a solution
[lua]
for i=1,24 do
--create the object
objetoAlien=display.newCircle(posicionXAlien, posicionYAlien, 15)
physics.addBody( objetoAlien,“dynamic” )
--track the object’s position
local posicion=objetoAlien.x
local moverDerecha=true
--function to move the object based in it’s position, make it go left and right
local function movimiento( event )
if (moverDerecha==true) then
objetoAlien:setLinearVelocity(20,0)
if (objetoAlien.x>posicion+20) then
moverDerecha=false
end
else
objetoAlien:setLinearVelocity(-20,0)
if (objetoAlien.x<posicion-20) then
moverDerecha=true
end
end
end
--line i’m having problem with, only works on the last generated object
Runtime:addEventListener(“enterFrame”,movimiento)
--tracks where the last object was generated to decide if it should make the
--next one in a new row
if (posicionXAlien>=(screenW-50)) then
posicionXAlien=originX-20
posicionYAlien=posicionYAlien+50
end
--changes the position for the next object
posicionXAlien=posicionXAlien+50
--inserts the object in the group
grupoAlien:insert( objetoAlien )
end
[/lua]
thanks