I am making a game that uses buttons to move the player left and right and a button that makes the player jump. I am running into problems when I attempt to press the jump button while moving left or right. I have activated multitouch and am using the setFocus() function with the optional event.id parameter. I am using build 2393a and this happens on my 4th gen iPod touch and iPhone 5.
Several other things that may help find the problem:
-
It was working fine until I changed the config.lua file to change the display size depending on the device.
-
The jump arrow touch listener does not run at all when the problem occurs.
-
At some point (I haven’t figured out what causes it) the problem stops and the touch system works fine until I restart the program.
-
Here is the code for the touch arrows:
–multitouch must be activated system.activate(“multitouch”) --left arrow button leftArrow = display.newImage(“Tiles/Arrow.png”, 100, display.contentHeight-70) leftArrow:setFillColor(1, 0.4) leftArrow.rotation = 180 leftArrow.touch = function(self, event) --if button is pressed, move left if event.phase == “began” then --if there is a touch move left display.getCurrentStage():setFocus(self, event.id) player.vx = -200 elseif event.phase == “ended” or event.phase == “canceled” then --if it is released stop display.getCurrentStage():setFocus(nil) player.vx = 0 end end leftArrow:addEventListener(“touch”, leftArrow) --right arrow button rightArrow = display.newImage(“Tiles/Arrow.png”, 250, display.contentHeight - 70) rightArrow:setFillColor(1, 0.4) rightArrow.touch = function(self, event) --if button is pressed, move right if event.phase == “began” then --if there is a touch move right display.getCurrentStage():setFocus(self, event.id) player.vx = 200 elseif event.phase == “ended” or event.phase == “canceled” then --if it is released stop display.getCurrentStage():setFocus(nil) player.vx = 0 end end rightArrow:addEventListener(“touch”, rightArrow) --up arrow button upArrow = display.newImage(“Tiles/Arrow.png”, display.contentWidth - 150, display.contentHeight - 70) upArrow:setFillColor(1, 0.4) upArrow.rotation = 270 upArrow.touch = function(self, event) --if button is pressed, jump and stop further jumping if not player.isAlive or levelComplete then --if the game is not active, cancel function (this is not the problem, I have tested it) return end print(“jumping”, player.onGround) if player.onGround > 0 and not player.isJumping then --if player is on the ground player.gravityScale = 1 player:setLinearVelocity(select(1, player:getLinearVelocity()), 0) player:applyForce(0, -950, player.x, player.y) --apply jumping force player.isJumping = true end end upArrow:addEventListener(“touch”, upArrow)
-
My only ideas are that the system.activate(“multitouch”) function is not running until the problem stops later in the program or that this is a bug with Corona because I updated to build 2393a while trying to solve the problem.
If you have any ideas on how I can solve the problem please let me know!