Ammar Aslam
Ammar Aslam

Reputation: 21

How to prevent unlimited jump in game? Corona LUA

okay so I have a jump button and a runtime listener function for when the jump button is pressed. So whenver the jump button is pressed I apply linear impulse to the game hero.

local function controls(event)

 if(event.phase=="began") then

    if (buttonpressed.id=="jump") then

                hero:applyLinearImpulse(0,-10,hero.x,hero.y)
            end

 elseif(event.phase=="ended") then

 end

 end

Now problem is if if the user keeps on tapping the jump button then the hero keeps on going up. I cant think of anyhting to counter this. One thing I could do is change the above code to:

 local function controls(event)

 if(event.phase=="began") then

    if (buttonpressed.id=="jump" and hero.y>display.contentHeight/2) then

                hero:applyLinearImpulse(0,-10,hero.x,hero.y)
            end

 elseif(event.phase=="ended") then

 end

 end

But this would still allow the jumping button to work until half of the screen is reached. Kindly help me on this.

Thanks

Upvotes: 0

Views: 152

Answers (3)

Federica Venuto
Federica Venuto

Reputation: 582

You can also create a Boolean. When the jump begins the boolean is true. When the player collides with the floor is false. While the boolean is true you cannot jump again.

Upvotes: 0

Chris Tanner
Chris Tanner

Reputation: 1650

Add a jump counter that resets when the player collides with the floor, that way you can allow double jumps etc later if you want to.

Upvotes: 4

pschulz
pschulz

Reputation: 1535

You could check the velocity of the hero and only allow the jump if the velocity is below a certain threshold:

if (buttonpressed.id=="jump" and hero.y>display.contentHeight/2) then
    local vx, vy = hero:getLinearVelocity()
    if vy < velocity_threshold then -- you have to define this value
        hero:applyLinearImpulse(0,-10,hero.x,hero.y)
    end
end

This should work. Another approach could be (i don't know much about corona) using linearDamping. This results in a damping of the linear motion, which sounds like something you could need.

Upvotes: 0

Related Questions