Character continuously sliding off the screen, despite collision function repositioning it Please help.

Good day,

I am twelve years old, and I have recently started coding with lua. I’m working on a game to practice my coding skills and I have come across a problem. My character has to jump over enemies moving from the right to the left of the screen, I knew the character would be pushed off the screen, so I coded a function to prevent this from happening:

local function playerHit()

        character.x = _L + 250

        character.alpha = 1

        lives[livesCount].alpha = 0

        livesCount = livesCount - 1

        if(livesCount <= 0) then

            onGameOver()

        end

    end

This is run every time a collision between the enemy and player occurs:

 local function showPlayerHit()

            character.alpha = 0.5

            local tmr_onPlayerHit = timer.performWithDelay(100, playerHit, 1)

        end

        

            if(event.object1.id == “enemy” and event.object2.id == “character”) then

                showPlayerHit()

                removeOnPlayerHit(event.object1, nil)

            end

        

            if(event.object1.id == “character” and event.object2.id == “enemy”) then

                showPlayerHit()

                removeOnPlayerHit(nil, event.object2)

            end

The problem is, if the character hits the enemy at a certain point, she will begin to slide left or right. Is this caused by the code detecting multiple collisions? Should I use sensors to detect one collision and ignore all others?

A solution would be very helpful, as it will allow me to progress forward with my game and release it by the end of this month. Thank you in advance.

Sincerely, 

Alex

Thier are two phase of a collision began and ended. So unless you do an
if event.phase == “began” then
end
The code will run twice per object

Try this

if event.phase == “ended” then 

            if(event.object1.id == “enemy” and event.object2.id == “character”) then

                showPlayerHit()

                removeOnPlayerHit(event.object1, nil)

            end

        

            if(event.object1.id == “character” and event.object2.id == “enemy”) then

                showPlayerHit()

                removeOnPlayerHit(nil, event.object2)

            end

end 

The problem is back, this only occurs when the character is hit at an angle.

The back of the enemy’s physics box, looks like a bumpy slide.

Both event.phase == “ended” and event.phase == “began” failed to work.

Is there another way to check for multiple collisions?

https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html#handling

Global collisions

Thank you for replying, but how can I check for more than one collision, and how can I fix it?

I have tried setting linearVelocity in a Runtime listener, but that does not work.

 if((event.object1.id == "enemy" or event.object1.id == "enemy2" or event.object1.id == "enemy3" or event.object1.id == "enemy4") and event.object2.id == "character") then showPlayerHit() removeOnPlayerHit(event.object1, nil) end if(event.object1.id == "character" and (event.object2.id == "enemy" or event.object2.id == "enemy2" or event.object2.id == "enemy3" or event.object2.id == "enemy4")) then showPlayerHit() removeOnPlayerHit(nil, event.object2) end

This is what I use for my collision.

I have tried to use a function to keep the character in place, but it does not work.

if event.phase == “began” then

               if((event.object1.id == “enemy” or event.object1.id == “enemy2” or event.object1.id == “enemy3” or event.object1.id == “enemy4”) and event.object2.id == “character”) then

                   showPlayerHit()

                   removeOnPlayerHit(event.object1, nil)

               elseif(event.object1.id == “character” and (event.object2.id == “enemy” or  event.object2.id == “enemy2” or event.object2.id == “enemy3” or event.object2.id == “enemy4”)) then

                   showPlayerHit()

                   removeOnPlayerHit(nil, event.object2)

               end

           end

This is my new collision detection code. It still does not work.

Also, I use physics editor for my physics.

Bounce = 0 on both the character and the enemies, so I am not sure what is happening.

Here is the video that shows the problem. The problem occurs at around 40 to 45 seconds and continues until 1:10, I hope this helps.

https://www.youtube.com/watch?v=xGoZ1jEG0YA&feature=youtu.be

Sometimes the character will slide much more than this and disappear off the screen. I just don’t want it to slide at all.

Hi @sdktester15,

What appears to be happening is that, after collision with an enemy, the character is retaining some of the physical force/velocity that occurred from that collision. So even though you’re resetting its X position, it still has physical momentum carried over from the previous collision. This is typical and expected behavior, but fortunately the solution is (or should be) easy: just reset the character’s linear X velocity to 0 on the line directly before you reset its X position:

[lua]

character:setLinearVelocity( 0, nil )

[/lua]

Hope this does the trick,

Brent

Thank you everyone for helping me with this problem, I have solved the problem by simplifying the physics shapes of my enemies, and using Brent’s solution.

Thier are two phase of a collision began and ended. So unless you do an
if event.phase == “began” then
end
The code will run twice per object