Jump and Run multitouch

Alright, so apparently it was just a problem with the joystick, it was a module I didn’t fully understand. I ended up just using a left and a right button. However, I finally figured out how to use multitouch and I am using using it now so thank you all for your help!!!

Hi @RGgamer,

This scenario mostly depends on how you have implemented multitouch. First of all, I assume you have enabled multitouch? If you have, then it’s a matter of determining how the multiple touches are handled by the various UI controls in your game.

Best regards,

Brent

Yes, I have enabled multitouch, but that didn’t do anything. How would I go about allowing the player to control the analog stick and tap/touch the jump btn at the same time?

it’s probably setFocus() at fault - suggest you verify that BOTH your joystick and button are handling multitouch focus properly (by passing event.id, see the docs)

What exactly do I do with setFocus() and event.id to get multitouch to work? Sorry, i’m quite a noob.

Hi @RGgamer,

Have you read through the guide and docs already? Multitouch is very dependent on your UI design, so there is no “general” way to implement it. Basically, however, each touch has an associated ID (event.id) which lets you distinguish it from other touches on the screen, and you can set focus on objects using that ID.

https://docs.coronalabs.com/guide/events/touchMultitouch/index.html

https://docs.coronalabs.com/api/event/touch/id.html

Brent

I did read through the docs. I just don’t understand what exactly setFocus does. I set the focus on the jump button, but that did nothing. 

Did you see this doc? Setting focus gives the object “ownership” of the touch it receives, and in the case of multitouch, only the touch (based on the ID) that you want to give it ownership of.

https://docs.coronalabs.com/api/type/StageObject/setFocus.html

Brent

It’ll help us help you faster if you post some code… Like how you add your buttons and all…

–SonicX278

Sorry for the late response, here is my code for the jump button:

[lua]

–Function to make player jump

local function playerJump(event)

   if (event.phase == “began”) then

      --Make sure player is on the ground

      if(canJump > 0) then

         --Make the player jump with physics

         player:setLinearVelocity( 0, -700 );

         --Set jump to true

         jump = true;

         playerSprite:setSequence(“jump”);

         playerSprite:setFrame(1);

         display.getCurrentStage():setFocus( jumpBtn );

         print( "Unique touch ID: "…tostring(event.id) )

      end

   end

   if (event.phase == “ended”) then

      display.getCurrentStage():setFocus( nil );

   end

end

–Jumping movement listener

jumpBtn:addEventListener(“touch”, playerJump);

[/lua]

Hi @RGgamer,

I surrounded your code with “lua” tags for clarity in the forums. Please do this when you post code in the future, since it helps others read it far easier.

[lua] -- your code [/lua]

As for your issue, when you say it “did nothing”, what is not happening? Does the player not jump at all? Or is the multitouch just not working as you want it to?

Brent

Sorry about the lua code thing. :confused:  But, yeah, the multitouch just doesnt work the way I want it to. I set the focus on the analog stick and the jump button when they are tapped, and then when the phase was ended, i set the focus to nil. But i still could only touch one at a time. If i touched the jump button, the player would just stop moving and jump instead.

both calls to setFocus() in most recently posted code will need to be modified, suggest another read thru the docs

the first call, to acquire focus, needs to additionally specify the specific event.id of THIS touch – as is, it’s stealing entire display-wide focus from everything.

the second call, to release focus, needs to specifiy event.target to release focus on THIS object – as is, it’s releasing entire display-wide focus from everything

and you’ll need to do equivalent inside your joystick if it is also using setFocus()

Yep, when multitouch is enabled you should also pass the touch ID to the focus API as the second argument. That tells Corona that the object should get ownership (focus) of that specific touch, and not another touch.

[lua]

display.getCurrentStage():setFocus( jumpBtn, event.id );

[/lua]

Alright, so my code ended up looking like this:

[lua]

local function playerJump(event)

if(event.phase == “began”) then

–Make sure player is on the ground

if(canJump > 0) then

–Make the player jump with physics

player:setLinearVelocity( 0, -700 );

–Set jump to true

jump = true;

playerSprite:setSequence(“jump”);

playerSprite:setFrame(1);

display.getCurrentStage():setFocus( jumpBtn, event.id );

print( "Unique touch ID: "…tostring(event.id) )

end

end

if(event.phase == “ended”) then

display.getCurrentStage():setFocus( nil, event.target );

end

end

[/lua]

I did the same for the analog stick , and then I just did it to the jumpBtn, still no results. What am I doing wrong??? 

reverse the arguments in setFocus() when releasing:  object first, nil event id second

Alright somethings not working here. Is it possible for someone to fill in the blanks here to allow for me to touch both of these at the same time without the touches interfering with each other:

[lua]

local function analogStickDrag(event)

if(event.phase == “began”) then

end

if(event.phase == “ended”) then

end

end

analogStick:addEventListener(“touch”, analogStickDrag);

local function jump(event)

if(event.phase == “began”) then

end

if(event.phase == “ended”) then

end

end

jumpBtn:addEventListener(“touch”, jump);

[/lua]

it is very hard to just paste into a template and expect it to work the way YOU want it to - for example, i don’t see a “moved” phase at all in there (though i suspect your joystick requires it).  it’ll really be worth your time to learn how setFocus works for yourself, so that YOU can set it up to work the way you need.

having said that, in general, this is a “common” usage pattern w multitouch:

(for study purposes only, don’t treat as “gospel” - cuz there’s never just one way, and diff problems need diff solutions)

function onTouch(event) if (event.phase=="began") then display.getCurrentStage():setFocus(event.target, event.id) event.target.hasFocus = true -- custom property created by you to "remember" that you got focus elseif (event.target.hasFocus) then -- only consider other phases if still have focus -- (mainly to preserve sanity if fe a "moved" arrives after "ended") if (event.phase=="moved") then -- depends: -- for button: you might want to release focus if moved beyond its bounds -- for stick: i assume you're already tracking delta motion somehow elseif (event.phase=="ended" or event.phase=="cancelled") then display.getCurrentStage():setFocus(event.target, nil) event.target.hasFocus = false end end end

I did everything the way it should be, but it still only lets me touch one at a time. Is it a problem that the joystick is created through a module, so all of the code for the joystick is in a different lua file?

and you’re sure you activated multitouch, and you’re building and testing on device, and device supports multitouch?

(granted they’re dumb-dumb questions, and no offense intended, but we need a baseline for assumptions)

suggest you make a new simple-as-possible test project.  just two non-overlapping rects as buttons, colored red, attach the function i gave above as touch listener to both.  in the code block for the began phase add event.target:setFillColor(0,1,0) and in the code block for the ended phase add event.target:setFillColor(1,0,0)

then test on device and see if you can get two simultaneously green rects

once you “get it”, then apply what you’ve learned to your actual code