I have a character that I am trying to make jump, limit to one jump, and only allow jumping if not already jumping if a button is touched.
My code either makes it so the character can jump as many times as it wants or jump once but for some reason it wont jump if button is pressed immediately after done jumping, I have to wait a second or two before the character is able to jump again.
Something is definitely wrong with my logic here.
Please help.
Here is my code.
--Hero local hSheetData = { width = 166, height = 285, numFrames = 12, sheetContentWidth = 1950, sheetContentHeight = 1385 } local sequenceData = { { name = "rest", frames = {9,10,12}, time = 500}, { name = "run", frames = {3,4,5,6}, time = 500 }, { name = "jump", frames = {1,2}, time = 500 } } local sheetInfo = require("punkie") local myImageSheet = graphics.newImageSheet( "hero/punkie.png", sheetInfo:getSheet() ) local sprite = display.newSprite( myImageSheet , sequenceData ) sprite:scale( .25, .25 ) sprite:play() sprite.x = plat1.x; sprite.y = plat1.y-10; local pentagonShape = { 0,-26, 17,-10, 12, 34, -19,34, -22,-20 } physics.addBody( sprite, "dynamic", { density=3.0, friction=10, bounce=0, shape=pentagonShape } ) sprite.isFixedRotation = true --Buttons local jump = display.newRect( 150, 30, 30, 30 ) jump.y = height - 30 jump.x = width - 30 --Jump Functions local function isJumping(self, event) vx, vy = sprite:getLinearVelocity() if (sprite.sequence == "jump") then print("Velocity "..sprite:getLinearVelocity( )) end if (vy \< -10 and sprite.sequence == "jump") then print( sprite:getLinearVelocity( ) ) sprite:pause() sprite:setSequence("rest") sprite:play( ) end end local function isJump( event ) if(event.phase == "began" ) then if(vy \> -10 and vy \< 30) then print( sprite:getLinearVelocity( ) ) sprite:setLinearVelocity( 0, -200 ) sprite:pause() sprite:setSequence("jump") sprite:play( ) print(sprite.sequence) end end end --Listeners jump:addEventListener( "touch", isJump ) sprite.enterFrame = isJumping Runtime:addEventListener( "enterFrame", isJump ) Runtime:addEventListener( "enterFrame", isJumping )