Avraam Mavridis
Avraam Mavridis

Reputation: 8940

event.x seems to not work

I have this scene, I want when the earth collide with the blackhole an explosion sprite to be played.

enter image description here

So I wrote this:

local function onCollision( event )
    if ( event.phase == "began" ) then   
        if(event.object1.name =="blackholeSprite" or event.object2.name =="blackholeSprite") then
            explosionSprite.x=event.x
            explosionSprite.y=event.y
        explosionSprite:play()
        timer.performWithDelay( 1500, gameOver )        
        end 
    end

end

The problem is that the explosion does not occur where the ball and the blackhole collide event.x and event.y . As you can see from the screenshot the explosion take place in the top-left corner. Any idea why this happens?

Upvotes: 1

Views: 105

Answers (1)

Maybe it is because of what is reported in the docs under "gotchas" for collision events.

Gotchas

The x and y position can be influenced by physics.getAverageCollisionPositions() and physics.setReportCollisionsInContentCoordinates().

Event position

During the "ended" phase (See the Collision Detection Guide), the X and Y positions are always zero. This is a Box2D limitation.

Also, when a collision involves a circle, and if the collision result is returned in local-space (see physics.setReportCollisionsInContentCoordinates(), then the local-space position of the collision is always 0,0. This is a Box2D limitation.

To solve your issue you could try to get the coordinates from (one of) the objects themselves, instead of getting them from the event.

Upvotes: 1

Related Questions