Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to prevent double click to jump?
Started by botafogo0382 Dec 02 2019 10:12 AM

5 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

botafogo0382

[GLOBAL: userInfoPane.html]
botafogo0382
  • Observer

  • 6 posts
  • Corona SDK

I'm creating a platform game and I came across with a character whose is jumping too high because the "double tap" is on.

 

How can I put a delay on a button to jump???

 

Full Code

-- Bibliotecas

local composer = require( "composer" )
local scene = composer.newScene()
local physics = require "physics"
local widget = require "widget"

-- Algumas Configurações
local w = display.contentWidth
local h = display.contentHeight

local sheetData = { width=83, height=121, numFrames=24}

local sheet = graphics.newImageSheet("img/percy.png", sheetData)

local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function goToNextLevel()
    
    composer.gotoScene("lastLevel", "fade", 700)
    composer.removeScene("level1")
end

-- Inicio do jogo
function scene:create( event )
	local sceneGroup = self.view
    physics.start()
    physics.setGravity(0, 14)
    local background = display.newImageRect( "img/game_background.jpg", screenW, screenH )
	background.anchorX = 0 
    background.anchorY = 0
    background.x = 0 + display.screenOriginX 
	background.y = 0 + display.screenOriginY

    borda1 = display.newRect(-700, 0, 20, 5000)
    borda1.alpha = 0
    physics.addBody( borda1, "static" )

    borda1 = display.newRect(1450, 0, 20, 5000)
    borda1.alpha = 0
    physics.addBody( borda1, "static" )

    local parede1 = display.newImageRect("grass.png", 5000, 100)
    parede1.x = 60
    parede1.y = 1024
    physics.addBody( parede1, "static" )

    local parede2 = display.newImageRect("grass.png", 100, 60)
    parede2.x = 70
    parede2.y = 760
    physics.addBody( parede2, "static" )

    local parede6 = display.newImageRect("grass.png", 100, 80)
    parede6.x = 870
    parede6.y = 540
    physics.addBody( parede6, "static" )

    local porta = display.newImageRect("img/porta.png", 120, 150)
    porta.x = 450
    porta.y = 900
    porta.myName = "porta"
    physics.addBody( porta, "static", {radius = 10, isSensor = true})

    
    local parede7 = display.newImageRect("grass.png", 100, 60)
    parede7.x = 450
    parede7.y = 540
    physics.addBody( parede7, "static" )

    local sequenceDataPlayer = 
    {
        { name = "idle", frames = {2} }, 
        { name = "walkToRight", frames = {7, 8, 9, 7}, time = 333, loopCount = 0},
        { name = "walkToLeft", frames = {13, 15, 14, 13}, time = 333, loopCount = 0},
        { name = "jump", frames = {1} },
    }

    local sequenceDataZombie = 
    {
        {name = "idle", frames = {6}},
        {name = "walkToRight", frames = {1, 2, 3, 4, 5, 6}, time = 333, loopCount = 0},
        {name = "walkToLeft", frames = {8, 9, 10, 11, 12, 13}, time = 333, loopCount = 0},
    }

    local player = display.newSprite(sheet, sequenceDataPlayer)
    player.x = -300
    player.y = 950
    player:setSequence("idle")
    player:play()
    physics.addBody( player, "dynamic", {radius = 45, bounce = 0, friction = 3})
    player.isFixedRotation = true
    player.myName = "player"

    local buttons = {}

    buttons[1] = display.newImage("seta.png") --direita
    buttons[1].x = 1000
	buttons[1].y = 950
	buttons[1].rotation = 90
    buttons[1].myName = "right"
    buttons[1].alpha=0.5

    buttons[2] = display.newImage("seta.png") --esquerda
    buttons[2].x = 800
	buttons[2].y = 950
    buttons[2].rotation = -90
    buttons[2].myName = "left"
    buttons[2].alpha=0.5

    buttons[3] = display.newImage("seta.png") --cima
    buttons[3].x = -300
    buttons[3].y = 950
    buttons[3].rotation= 0
    buttons[3].myName = "up"
    buttons[3].alpha=0.5

    local walkX = 0
    local walkY = 0

    local function touchFunction(e)
        local phase = event.phase
		local name = event.keyName
        if e.phase == "began" or e.phase == "moved" then
            if e.target.myName == "up" then
                player:setSequence("jump")	
                player:applyLinearImpulse( 0, -1)
                
            elseif e.target.myName == "left" then
                player:setSequence("walkToLeft")
                player:play()
                walkX = -4
                player.xScale = -1
            elseif e.target.myName == "right" then
                player:setSequence("walkToRight")
                player:play()
                walkX = 4
                player.xScale = -1
            end
        else 

            if e.target.myName == "right" then
                player:setSequence("idle")
                
            elseif e.target.myName == "left" then
                player:setSequence("idle")
            end
            walkY=0
            walkX=0
        end
        
    end

    local j=1

    for j=1, #buttons do
        buttons[j]:addEventListener("touch", touchFunction)
        if j == 3 then
            buttons[j]:addEventListener("tap", touchFunction)
        end
    end
    local update = function(e)
        player.x = player.x + walkX
        player.y = player.y + walkY
    end
    porta.collision = goToNextLevel
    porta:addEventListener( "collision" )
    
    Runtime:addEventListener("enterFrame", update)
    sceneGroup:insert(background)
    sceneGroup:insert(parede2)
    sceneGroup:insert(parede6)
    sceneGroup:insert(parede7)
    sceneGroup:insert(porta)
end

function scene:show( event )
	local sceneGroup = self.view
	local phase = event.phase
	
	if phase == "will" then
		
	elseif phase == "did" then
		
		physics.start()
	end
end

function scene:hide( event )
	local sceneGroup = self.view
	
	local phase = event.phase
	
	if event.phase == "will" then
        physics.stop()
        sceneGroup:remove(background)
        display.remove(parede1)

        sceneGroup:remove(parede2)
        sceneGroup:remove(parede6)
        sceneGroup:remove(parede7)
        sceneGroup:remove(porta)
	elseif phase == "did" then
		-- Called when the scene is now off screen
	end	
	
end

function scene:destroy( event )	
end

---------------------------------------------------------------------------------

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-----------------------------------------------------------------------------------------

return scene


[TOPIC: post.html]
#2

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 663 posts
  • Corona SDK

limit the applyLinearImpulse( ) to just the "began" phase.  Right now it is also set to fire during the "move" phase so it will trigger every time your finger moves even slightly.



[TOPIC: post.html]
#3

botafogo0382

[GLOBAL: userInfoPane.html]
botafogo0382
  • Observer

  • 6 posts
  • Corona SDK

I don't understand the tip... Sorry KKKK



[TOPIC: post.html]
#4

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Observer

  • 6 posts
  • Corona SDK

I think that would work:

 

local jumpDelay = 0

 

 

function jumpDelay()

   jumpDelay = 0

end

 

if jumpDelay == 0 then

   ...

   jumpDelay = 1

   timer.performWithDelay( 1000, jumpDelay ) 

end



[TOPIC: post.html]
#5

ConveyedRex7592

[GLOBAL: userInfoPane.html]
ConveyedRex7592
  • Observer

  • 6 posts
  • Corona SDK

If that doesn't work try that:

 

local jumpDelay = 0

 

function jumpDelay()

   jumpDelay = 0

end

 

if jumpDelay == 0 then

   ...

   jumpDelay = 1

end

 

if jumpDelay == 1 then

   timer.performWithDelay( 1000, jumpDelay )

end



[TOPIC: post.html]
#6

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 663 posts
  • Corona SDK

@Conveye - That would work except the jump is triggered by the 'moved' phase as well as the 'began' phase so if the user keeps their finger on the button longer than the delay and then moves it - triggering the 'move' phase, the character will jump again.

 

@Bogo - do yourself a favor and spend some time learning about event phases




[topic_controls]
[/topic_controls]