collision issue when a bullet collides with an object that moves with enterFrame

Hi, in my code I have runtime.enterFrame function that moves an object to the left and the return it to the right at x = 560, the problem is when a collision happens to the object, when the object collides with a bullet I want it to go the position of x = 560, but this doesn’t happen, check my code:

[lua]

ene3 = display.newImageRect(group,“enemy3.png”, 47, 44)

ene3.x = 560; ene3.y = math.random(screenTop+10, screenBottom)

ene3.name = “ene”

physics.addBody(ene3,“static”, {radius = 21})

function ene3_collision(e)

        if e.other.name == “bullet” then

            spawnExplosionToTable(e.target.x,e.target.y)

            ene3.x = 560

    end

    end

    ene3:addEventListener(“collision”, ene3_collision)

function ene3:enterFrame()

    if ene3.x < screenLeft-10 then

        ene3.x = 560

        ene3.y = math.random(screenTop+10, screenBottom)

    else

        ene3.x = ene3.x -5

    end

end

Runtime:addEventListener(“enterFrame”, ene3)

[/lua]

Hi @hammod-930,

It appears that you’re not specifying the collision listener properly (syntax error). For a “local” collision listener on the object itself (not in the Runtime), it should look like this:

[lua]

ene3.collision = ene3_collision

ene3:addEventListener( “collision”, ene3  )

[/lua]

Then, the listener function should be structured something like this:

[lua]

local function ene3_collision( self, event )

   if ( event.phase == “began” ) then

      print( self.name … ": collision began with " … event.other.name )

   end

end

[/lua]

Take care,

Brent

@Brent

I make it now like this but nothing canged

 [lua]

function ene3_collision(self,e)

        if e.other.name == “bullet” then

            spawnExplosionToTable(e.target.x,e.target.y)

            self.x = 560

    end

    end

    ene3.collision = ene3_collision

    ene3:addEventListener(“collision”, ene3)

    function ene3:enterFrame()

    if ene3.x < screenLeft-10 then

        ene3.x = 560

        ene3.y = math.random(screenTop+10, screenBottom)

    else

        ene3.x = ene3.x -5

    end

end

Runtime:addEventListener(“enterFrame”, ene3)

[/lua]

the ene3 doesn’t go to x = 560 after the collision, it keeps moving to the left !

Are you sure it’s even getting past this conditional clause? Can you add a print() to confirm it?

[lua]

if e.other.name == “bullet” then

[/lua]

@Brent

Yes, I added the print() in it showed up on the console, even the explosion showed up, but the ene3.x didn’t changed.

Hi @hammod-930,

This may be some conflict going on where the collision is being processed after the Runtime movement, so one is being overridden. Maybe something like this would work, where the Runtime enterFrame is removed when the collision occurs, then after a timer of 50 milliseconds, the object is repositioned and the Runtime enterFrame is added again.

[lua]

local function ene3_collision(self,e)

    if ( e.phase == “began” and e.other.name == “bullet” ) then

        Runtime:removeEventListener( “enterFrame”, ene3 )

        spawnExplosionToTable( e.target.x,e.target.y )

        timer.performWithDelay( 50, function() ene3.x = 560; Runtime:addEventListener( “enterFrame”, ene3 ) end, 1 )

    end

end

[/lua]

@Brent,

Thank you a lot for this help, now this works fine.

Hi @hammod-930,

It appears that you’re not specifying the collision listener properly (syntax error). For a “local” collision listener on the object itself (not in the Runtime), it should look like this:

[lua]

ene3.collision = ene3_collision

ene3:addEventListener( “collision”, ene3  )

[/lua]

Then, the listener function should be structured something like this:

[lua]

local function ene3_collision( self, event )

   if ( event.phase == “began” ) then

      print( self.name … ": collision began with " … event.other.name )

   end

end

[/lua]

Take care,

Brent

@Brent

I make it now like this but nothing canged

 [lua]

function ene3_collision(self,e)

        if e.other.name == “bullet” then

            spawnExplosionToTable(e.target.x,e.target.y)

            self.x = 560

    end

    end

    ene3.collision = ene3_collision

    ene3:addEventListener(“collision”, ene3)

    function ene3:enterFrame()

    if ene3.x < screenLeft-10 then

        ene3.x = 560

        ene3.y = math.random(screenTop+10, screenBottom)

    else

        ene3.x = ene3.x -5

    end

end

Runtime:addEventListener(“enterFrame”, ene3)

[/lua]

the ene3 doesn’t go to x = 560 after the collision, it keeps moving to the left !

Are you sure it’s even getting past this conditional clause? Can you add a print() to confirm it?

[lua]

if e.other.name == “bullet” then

[/lua]

@Brent

Yes, I added the print() in it showed up on the console, even the explosion showed up, but the ene3.x didn’t changed.

Hi @hammod-930,

This may be some conflict going on where the collision is being processed after the Runtime movement, so one is being overridden. Maybe something like this would work, where the Runtime enterFrame is removed when the collision occurs, then after a timer of 50 milliseconds, the object is repositioned and the Runtime enterFrame is added again.

[lua]

local function ene3_collision(self,e)

    if ( e.phase == “began” and e.other.name == “bullet” ) then

        Runtime:removeEventListener( “enterFrame”, ene3 )

        spawnExplosionToTable( e.target.x,e.target.y )

        timer.performWithDelay( 50, function() ene3.x = 560; Runtime:addEventListener( “enterFrame”, ene3 ) end, 1 )

    end

end

[/lua]

@Brent,

Thank you a lot for this help, now this works fine.