Having trouble with multitouch

I’m creating my first game which is pretty basic; I have a ship and two buttons that move the ship left and right. My problem is when I touch the left and right buttons too fast the ship just stops moving :stuck_out_tongue:

I’ve activated multitouch using system.activate(“multitouch”) but it doesn’t really help

Heres the code

[lua]-- Movement -----
local isMoving = {}

local movePlayerRight = function( event )
if isMoving == “right” then
playerShip.x = playerShip.x + 10
end
end

local movePlayerLeft= function( event )
if isMoving == “left” then
playerShip.x = playerShip.x - 10
end
end

– Controls -----
local function moveRight(event)
if (event.phase == “began”) then
isMoving = “right”
elseif (event.phase == “ended” or event.phase == “moved”) then
isMoving = {}
end
end
DpadrightArrow:addEventListener (“touch”, moveRight)

local function moveLeft(event)
if (event.phase == “began”) then
isMoving = “left”
elseif (event.phase == “ended” or event.phase == “moved”) then
isMoving = {}
end
end
DpadleftArrow:addEventListener (“touch”, moveLeft)

Runtime:addEventListener(“enterFrame”, movePlayerRight)
Runtime:addEventListener(“enterFrame”, movePlayerLeft)[/lua]

Thank You! [import]uid: 41389 topic_id: 19495 reply_id: 319495[/import]

@ leom271

Does your game really needs Multi Touch or is just to move the ship? Single touches works great for moving a character the only time you might need Multi Touch would be when you have left right and jump you might want to jump as you are moving the character. But if your game really needs Multi Touch use flags to stop the other button from working while you are touching the moving button.
For example if you are holding left and you want to move right there is no point in pressing right while holding left makes sense? So if you use flags to cancel the other button WHILE a button is being used will prevent the user from getting that weird controlling I used it before on a game I was working on and it worked great. Base on the flags I posted below you could make when you are moving left then move_right = false but when you release left button then move_right = true and vice versa.

Hope this helps,
LeivaGames

Example:
[lua]local move_left = true
local move_right = false [import]uid: 30314 topic_id: 19495 reply_id: 75297[/import]

Thanks LeivaGames, I’ve tried implementing this many different ways but its just not working

I think the problem is detecting different touches because when I’m holding say the right button and the player starts moving to the right but as soon as I touch any part of the screen with another finger it stops; shouldn’t just adding Multitouch fix this?

[import]uid: 41389 topic_id: 19495 reply_id: 75341[/import]

You need to also add in event.id to differentiate between the touch events [import]uid: 84637 topic_id: 19495 reply_id: 75363[/import]

Thanks Danny, where do I add event.id? [import]uid: 41389 topic_id: 19495 reply_id: 75367[/import]

In your “began” phase add this :

display.getCurrentStage():setFocus( event.target, event.id)  

In your “ended” or “cancelled” phase add this

display.getCurrentStage():setFocus(event.target, nil ) [import]uid: 84637 topic_id: 19495 reply_id: 75370[/import]

Nope it’s just not working, Im also trying to compare it with the MultitouchFingers sample with no luck its just confusing
heres what I have right now
[lua]local isMoving = {}

local movePlayerRight = function( event )
if isMoving == “right” then
playerShip.x = playerShip.x + 10
end
end

local movePlayerLeft= function( event )
if isMoving == “left” then
playerShip.x = playerShip.x - 10
end
end

– Controls -----
local function moveRight(event)
if (event.phase == “began”) then
display.getCurrentStage():setFocus( event.target, event.id)
isMoving = “right”
elseif (event.phase == “ended” or event.phase == “moved”) then
display.getCurrentStage():setFocus(event.target, nil )
isMoving = {}
end
end
DpadrightArrow:addEventListener (“touch”, moveRight)

local function moveLeft(event)
if (event.phase == “began”) then
display.getCurrentStage():setFocus( event.target, event.id)
isMoving = “left”
elseif (event.phase == “ended” or event.phase == “moved”) then
display.getCurrentStage():setFocus(event.target, nil )
isMoving = {}
end
end
DpadleftArrow:addEventListener (“touch”, moveLeft)

Runtime:addEventListener(“enterFrame”, movePlayerRight)
Runtime:addEventListener(“enterFrame”, movePlayerLeft)[/lua] [import]uid: 41389 topic_id: 19495 reply_id: 75382[/import]

I bought Peach shooter template which has multitouch and I pretty much rewrote the code based off her template and I’m still unable to make it work [import]uid: 41389 topic_id: 19495 reply_id: 75523[/import]

Why are you setting focus to nil if the phase is equal to moved?

[import]uid: 84637 topic_id: 19495 reply_id: 75525[/import]

I figured they would be the same but I saw a tutorial from Rafael and saw that it was wrong so I fixed that

Heres how it looks now
[lua]-- Movement -----
local motionx = 0
local motiony = 0
local speed = 10

local function stop (event)
if “ended” == event.phase then

motionx = 0
motiony = 0
end
end

Runtime:addEventListener(“touch”, stop )

local moveship = function(event)
playerShip.x = playerShip.x + motionx
playerShip.y = playerShip.y + motiony
end

Runtime:addEventListener(“enterFrame”, moveship)

– Controls -----
function DpadleftArrow:touch(event)
if (event.phase == “began”) then
motionx = -speed
motiony = 0
display.getCurrentStage():setFocus(event.target, event.id )
elseif (event.phase == “moved” ) then
motionx = 0
elseif (event.phase == “ended” ) then
motionx = 0
display.getCurrentStage():setFocus( event.target, nil )
end
end
DpadleftArrow:addEventListener(“touch”, DpadleftArrow)

function DpadrightArrow:touch(event)
if (event.phase == “began”) then

motionx = speed
motiony = 0
display.getCurrentStage():setFocus( event.target, event.id )
elseif (event.phase == “moved” ) then
motionx = 0
elseif (event.phase == “ended” ) then
motionx = 0
display.getCurrentStage():setFocus( event.target, nil )
end
end
DpadrightArrow:addEventListener(“touch”, DpadrightArrow)

Runtime:addEventListener(“enterFrame”, movePlayerRight)
Runtime:addEventListener(“enterFrame”, movePlayerLeft)[/lua]

Sorry for taking your time, I’ve tried figuring this out on my own and the Doc doesn’t have much info for MultiTouch; unless I’m making this seem too hard on my own :stuck_out_tongue: [import]uid: 41389 topic_id: 19495 reply_id: 75526[/import]