Hold screen and tap screen

So my game just uses touch to trigger a jump.

I’m thinking of doing a hold screen to make the character duck under objects

So I think I need to start with a touch began statement calculate past two frames so that he ducks during that hold. Then when there’s a touch end he doesn’t jump.

Does that sound right?

Thanks

yes.
measure the time t of hold, and if t > t_hold, then duck and do not jump.

Awesome thank you @Jmv38 !

My code isn’t working because I’m using a timer = timer+1 and it’s resetting. I think I need to use a delta timer instead to calculate the hold…

@Majormorgan Here’s an example. I didn’t add jump or duck code so I just move the sprite up or down to show jump or duck. You can add you’re own jump/duck code. Tap screen to jump. Tap and hold for greater than .2 seconds to duck.


displayMode(FULLSCREEN)

function setup()
    dy=HEIGHT/2
    tmr=-1
    delay=.2
end

function draw()
    background(40,40,50)
    fill(255)
    text("tap screen to jump",WIDTH/2,HEIGHT-100)
    text("hold screen for .2 sec then release to duck",WIDTH/2,HEIGHT-150)
    sprite("Planet Cute:Character Boy",WIDTH/2,dy)
    if tmr>=0 then
        tmr=tmr+DeltaTime  -- add to timer
    end
    if jump then    -- perform jump code
        dy=dy+1
    end
    if duck then    -- perform duck code
        dy=dy-1
    end
end

function touched(t)
    if t.state==BEGAN then
        tmr=0   -- reset timer
    end
    if t.state==ENDED then
        duck=false  -- reset variables
        jump=false
        if tmr>delay then   -- timer greater than delay count
            duck=true   -- set duck
        else
            jump=true   -- set jump
        end
        tmr=-1
    end
end

The user and all related content has been deleted.

Hey @dave1707 they are both cool examples and the swipe up and swipe down could be an even better answer! I’m coding In your timed approach above for the hold approach. I’ve got the less than tmr delay working. Still not cracked the hold part yet, but I’m almost there. I have to modify you code because of the engine I built is different.

Your ideas are really cool. I’ll post when I have it working

Thank you so much!

@Majormorgan This isn’t the hold screen or tap screen, but a flick in an up or down direction to jump or duck. With a flick up or down, there isn’t the delay to determine if the touch is being held to cause the duck. Also, there isn’t a need for a timer.


supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
    done=true
    cb={x=60,y=300}
    val=2
end

function jumpTween()
    t1=tween(.5,cb,{y=400},tween.easing.cubicOut)
    t2=tween(.5,cb,{y=300},tween.easing.cubicIn,tweenDone)
    tween.sequence(t1,t2)   
end

function duckTween()
    t1=tween(.5,cb,{y=200},tween.easing.cubicOut)
    t2=tween(.5,cb,{y=300},tween.easing.cubicIn,tweenDone)
    tween.sequence(t1,t2)   
end

function draw()
    background(40, 40, 50)
    fill(255)
    text("flick up to jump, down to duck",WIDTH/2,HEIGHT-100)
    rect(WIDTH/2,260,20,50)
    if cb.x<50 or cb.x>WIDTH-50 then    -- keep sprite on the screen
        val=-val
    end
    cb.x=cb.x+val -- move sprite 
    sprite("Planet Cute:Character Boy",cb.x,cb.y)    -- draw sprite
end

function tweenDone()
    done=true   -- tween is done, allow next jump or duck
end

function touched(t)
    if t.state==MOVING and done then
        done=false
        if t.deltaY<0 then  -- flick down
            duckTween()
        end
        if t.deltaY>0 then  -- flick up
            jumpTween()
        end
    end
end

Cool!

In the end the principle of the code worked really well and I had to create some rules around the specific things in my game. But the code idea was really helpful!