I got this problem with multitouch for IOS. What I’m have been trying to do is in a game if a person attempts to press the left arrow and right arrow at the same the moving character should be trying to move left or right quickly. The problem with the way I have the code written is when both buttons are pressed the characters ends up getting stuck going left only or right only and not just going left or right quick.
The same problem persists if the person presses left arrow and jumping button at the same time, or right arrow and jumping button at the same time.
My question is how do I make the character response normally when a person presses two buttons at the same time with the code I have below?
local function mover (event)
if event.phase == "began" or lleft == 1 then
print("began phase")
rright = 1
lleft = 0
jumptime = 0
local function moves ()
fis:setLinearVelocity( 200, 100 )
if tonumber(lifes.money) \<= 0 then
timer.cancel(go)
end
end
go = timer.performWithDelay(100, moves,0)
print(fis.x)
print(fis.y)
fis:prepare("heroright")
fis:play("heroright")
display.getCurrentStage():setFocus(event.target)
elseif event.phase == "ended" or event.phase == "cancelled" then
print("end phase")
timer.pause(go)
step = false
fis:setLinearVelocity( 0, 30 )
sprite.add (heroset, "hero", 1, 1, 200, 0)
fis:pause()
fis:prepare("heroright")
display.getCurrentStage():setFocus(nil)
end
end
rightb:addEventListener("touch", mover)
local function movel (event)
if event.phase == "began" or rright == 1 then
print("began phase")
lleft = 1
rright = 0
jumptime = 1
local function moves ()
fis:setLinearVelocity( -200, 100 )
if tonumber(lifes.money) \<= 0 then
timer.cancel(go)
end
end
go = timer.performWithDelay(100, moves,0)
print(fis.x)
print(fis.y)
fis:prepare("heroleft")
fis:play("heroleft")
display.getCurrentStage():setFocus(event.target)
elseif event.phase == "ended" or event.phase == "cancelled" then
print("end phase")
timer.pause(go)
fis:setLinearVelocity( 0, 0 )
fis:pause()
fis:prepare("heroleft")
display.getCurrentStage():setFocus(nil)
end
end
lefttb:addEventListener("touch", movel)
local function jump (event)
if jumptime == 0 and rright == 1 then
print("began phase")
fis:setLinearVelocity( 0, -460 )
rright = 1
jumptime = 1
print(jumptime)
sprite.add (heroset, "heroup", 2, 1, 200, 0)
fis:prepare("heroup")
fis:play("herorup")
Runtime:removeEventListener("enterFrame",mover)
--display.getCurrentStage():setFocus(event.target)
end
if jumptime == 0 and lleft == 1 then
print("began phase")
fis:setLinearVelocity( 0, -460 )
jumptime = 1
lleft = 1
print(jumptime)
sprite.add (heroset, "heroup", 6, 1, 200, 0)
fis:prepare("heroup")
fis:play("herorup")
Runtime:removeEventListener("enterFrame",mover)
--display.getCurrentStage():setFocus(event.target)
end
end
uptb:addEventListener("tap", jump)
local function canjum (event)
if jumptime == 0 or event.other.hit == "floor" then
jumptime = 0
print(jumptime)
end
end
fis:addEventListener("collision", canjum)
[import]uid: 17058 topic_id: 36605 reply_id: 336605[/import]