apply a Runtime:addEventListener to each object of a group

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]

That’s because you are adding the listener to the Runtime, not the object itself. You should add the listener to the object.

objetoAlien:addEventListener( "enterFrame", movimiento )

I had already tried that, it doesn’t work, none of the objects move instead of just one

If i just call the function like “movimiento()” all objects start moving, but since the function is never uptaded they continue moving right until they leave the screen

Also tried with a timer, again it only worked on the last object

Try putting “local” in front of your object creation:

local objetoAlien=display.newCircle(posicionXAlien, posicionYAlien, 15)

Rob

Thanks a lot!

Declaring it local, and using the runtime listener it worked :smiley: thanks

That’s because you are adding the listener to the Runtime, not the object itself. You should add the listener to the object.

objetoAlien:addEventListener( "enterFrame", movimiento )

I had already tried that, it doesn’t work, none of the objects move instead of just one

If i just call the function like “movimiento()” all objects start moving, but since the function is never uptaded they continue moving right until they leave the screen

Also tried with a timer, again it only worked on the last object

Try putting “local” in front of your object creation:

local objetoAlien=display.newCircle(posicionXAlien, posicionYAlien, 15)

Rob

Thanks a lot!

Declaring it local, and using the runtime listener it worked :smiley: thanks