Multi action function needed

I have a tap and touch code. I need to have my character go from idle to running on touch and jump on tap, but I also have to work in a way for him to attack. My other issue with the code I have is after the jump, he doesn’t go back to idle position. Any advice?

local function jumpTap( event )
   if event.numTaps == 2 then
   animate:setSequence("jumping")
   animate:setLinearVelocity(0,-200)
   animate:play()
else
animate:setSequence("idle")
animate:play()
end
   return true
end


local function onTouch(event)
if(event.phase == "began")then
animate:setSequence("running")
animate:play()
elseif(event.phase == "ended")then
animate:setSequence("idle")
animate:play()
end
end

Sorry, I don’t understand, can you please expand upon the question?

I understand what you need to hero jump when player presses on screen and move then player holds finger on screen.And tap and touch is almost the same, touch doesn’t mean hold.

If I understand correctly this should be solution:

local hold = false
local checkNum = 0

local holding
holding = function()

  if hold then
     if checkNum == 0 then

      checkNum = 1
      timer.performWithDelay(500, holding)
      
    elseif checkNum == 1 then

      checkNum = 0

      -- player is holding finger on screen
    end

  else
    
    if checkNum == 0 then

      -- player tapped the screen

  end

end

local function oneTap(event)
  if event.phase == "began" then

    hold = true
    holding()
  elseif event.phase== "ended" then

    hold = false

  end
  
end

PS.: put event listener only on oneTap function

Is that what you want to do?

Mind you, I am new, but I do not see a way to implement that could without two event listeners.

I gave you exaple, or I didn’t understand you correctly?

@nduracak

First, please clarify. What are your inputs?

  • Will you be using on screen buttons for actions? If so, how many and what actions do they represent.

  • Will you be using the entire screen as a single button?

Second, what are your player actions?

  • List the actions your player can take. If there are restrictions on when the player can take an action, elaborate on what those restrictions are.

i.e. Before you try to implement it, you should be able to clearly how inputs map to actions and describe that to us, otherwise it is too hard to guess what you are trying to do and therefore hard to help.

Note: If you are copying the input-action scheme of a another game(s), please give their full names. So we can see this input-action to fully grok what you want to do.

A

  1. No buttons.
  2. I was hoping to swipe the screen if possible for attacks, tap for jumps, and hold to run. Idk if that is possible since I am fairly new.
    B
  3. Run, jump, attack. No restrictions as on now.

Input is just as samurai slash attack, running, and jumping, plus an idle animation.

No borrowed code, just simple lua low level stuff.

I input multi sprite sheets in this fashion;

local blank = {
{name = “taco”, other features
}
}

then I called in the sprite like this;

animation = display.newSprite(picture graphic, blank)

then I called it to a function show in my original post.

I don’t know if I’m being to vague here

Example for jumping and walking(running)

-- Animation setup

... (animation = display.newSprite(picture graphic, blank), etc.)

-- Jump and walk

local hold = false
local checkNum = 0

local holding
holding = function()

  if hold then
     if checkNum == 0 then

      checkNum = 1
      timer.performWithDelay(500, holding)
      
    elseif checkNum == 1 then

      checkNum = 0

      -- player is holding finger on screen
    end

  else
    
    if checkNum == 0 then

      -- player tapped the screen

  end

end

local function oneTap(event)
  if event.phase == "began" then

    hold = true
    holding()
  elseif event.phase== "ended" then

    hold = false

  end
  
end

Runtime:addEventListener("touch", oneTap)
1 Like

Is that a mobile game? Can you supply a link to the game? Or, maybe a youtube video showing this game?

I would advise against mixing tap and touch events. Personally, I always prefer using touch events given the nature of taps.

You could do something along these lines to differentiate between taps and touches, as well as swipes.

local getTimer = system.getTimer
local tapTimeLimit = 150
local swipePixelLimit = 20
local tapCount, touchStart, touchEnd = 0

local function touchEvent(event)
    local phase = event.phase
    if phase == "began" then
        tapCount = tapCount+1 -- Increment the tapCount, if you need to.
        touchStart = getTimer()
        
    elseif phase == "ended" or phase == "cancelled" then
        touchEnd = getTimer()
        
        -- Once the touch ends, determine if it was a tap or a touch.
        if touchEnd - touchStart < tapTimeLimit then
            print( "Tap!" )
            
        else
            tapCount = 0 -- Reset taps on each touch event.
            -- Then determine if the touch was a swipe or not.
            if event.xDelta > swipePixelLimit then
                print( "Swipe right!" )
            elseif event.xDelta < -swipePixelLimit then
                print( "Swipe left!" )
            else
                print( "Touch!" )
            end
        end
    end
    return true
end

Runtime:addEventListener( "touch", touchEvent )

Naturally, this is only an idea and you’d need to take it further.

As for resetting to idle sequence after jumps, it really depends on what you are doing specifically.

2 Likes

I don’t want to mix them, I would prefer to use only the touch feature, but I can’t find a way to have three touch events without it interfering with the other. I am fairly new and can’t find any examples of people using three touch events or more for a character to do an action. I can find twoish, but nothing else.

It is not a game, sorry, I just meant that my character has a slash type attack.

would I just add another check type for a third function? I need three actions, walking, jumping, and attack, but I can’t figure out how to because touch events are limited by began and ended it seems to me.

I suggest you do it along the lines of what @XeduR explained in his post. One touch event handler, and then figure out yourself if the gesture was a swipe or tap by checking delta x/y.

Since you also want the character to move when the player holds down it becomes a little trickier. In the “began” phase you could add an enter frame listener that moves the character on every frame. Then remove the same enter frame listener in the “ended” phase of the touch.

If you don’t want to move the character when the user just taps to jump, you could delay the start of the enter frame listener until you know that it’s not a tap event. How long depends really only on what you consider a good time frame.

Best way to solve this is to start by just getting the touch handler right. Ignore everything about running/jumping/slashing for now and just print out to the console to see that your gesture recognition is correct. Then move on to use the touch handler to actually control your character.

1 Like

I don’t understand