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