I am having a problem with calling a touch event when a button is pressed. There is a function that I would like to call when I press the pause button (pauseBtn), but it is not being called at all. I tried the same function with different buttons: when I add the event listener to another button (resumeBtn), the touch event is only being called when I press the left hand side of the button. And when I add the listener to yet another button (menuBtn), only then is the touch event being called properly. It would be great if anyone could look at my code and find out the reason behind this. Thanks a lot.
[lua]
module(…, package.seeall)
function new()
local physics = require(“physics”)
physics.start()
physics.setDrawMode(“hybrid”)
game = require(“beebegames”)
local localGroup = display.newGroup()
–Creation of variables
local randPauseBtnFile1 = math.random(1, 2)
local randPauseBtnFile2 = math.random(3, 4)
local randPauseBtnFile = “PauseBtn” … tostring( math.random(randPauseBtnFile1, randPauseBtnFile2 ) ) … “.png”
local pauseBtn = display.newImageRect(randPauseBtnFile, 40, 40)
pauseBtn.x = 460
pauseBtn.y = 15
physics.addBody(pauseBtn,“static”)
local pauseScreen = display.newImageRect(“PauseScreen.png”, 480, 320)
pauseScreen.x = 240
pauseScreen.y = 160
pauseScreen.isVisible = false
local pauseMenu = display.newImageRect(“PauseMenu.png”, 300, 185)
pauseMenu.x = display.contentWidth/2
pauseMenu.y = display.contentHeight/2
pauseMenu.isVisible = true
local pauseText = display.newText("–Paused Game–", 0, 0, “Apple Casual”, 20)
pauseText.x = display.contentWidth/2
pauseText.y = display.contentHeight/2 - 80
pauseText:setTextColor(0,150, 255)
pauseText.isVisible = true
local resumeBtn = display.newImageRect(“ResumeBtn.png”, 175, 57)
resumeBtn.x = display.contentWidth/2
resumeBtn.y = display.contentHeight/2 - 30
resumeBtn.isVisible = true
physics.addBody(resumeBtn,“static”)
local resumeBtnText = display.newText(“Resume”, 0, 0, “Apple Casual”, 25)
resumeBtnText:setTextColor(125, 250, 240, 255)
resumeBtnText.x = resumeBtn.x
resumeBtnText.y = resumeBtn.y
resumeBtnText.isVisible = true
local menuBtn = display.newImageRect(“MenuBtn.png”, 175, 57)
menuBtn.x = display.contentWidth/2
menuBtn.y = resumeBtn.y + 70
menuBtn.isVisible = true
physics.addBody(menuBtn,“static”)
local menuBtnText = display.newText(“Menu”, 0, 0, “Apple Casual”, 25)
menuBtnText:setTextColor(120, 120, 240, 255)
menuBtnText.x = menuBtn.x
menuBtnText.y = menuBtn.y
menuBtnText.isVisible = true
–Function that is not being called properly
local function pauseGame(event)
if (event.phase == “ended”) then
print(“pause”)
menuBtnText = display.newText(“Pause”, 0, 0, “Apple Casual”, 25)
menuBtnText.x = display.contentWidth/2
menuBtnText.y = display.contentHeight/2
end
return true
end
--Works completely correctly for only menu button (menuBtn)
–Want it to work for pause button (pauseBtn)
pauseBtn:addEventListener(“touch”, pauseGame)
localGroup:insert(pauseScreen)
localGroup:insert(pauseMenu)
localGroup:insert(pauseText)
localGroup:insert(resumeBtn)
localGroup:insert(resumeBtnText)
localGroup:insert(menuBtn)
localGroup:insert(menuBtnText)
localGroup:insert(pauseBtn)
return localGroup
end
[/lua]