Touch Event Problem

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]

Can you add some prints into the pauseGame function to see if it’s getting fired? 

This doesn’t impact the button working, but why are you adding physics bodies to these buttons.  Do you really want physics objects bouncing off of your pause button?

Rob

Thanks so much for helping me out on this error. I did add a print statement, as shown in line 56. As I said earlier, the touch event is only being called when I press the left hand side of the resume button, completely correctly for the menu button, and nothing at all for the pause button (I want it to work for the pause button). This includes firing the print statements.  As for the physics bodies, I just wanted to see if making the button a physics body would change anything in regard to calling the touch event, but it showed no change, as you had said in your post. 

I don’t see in your code where you are ever adding a touch event on your resume button.  There is only one for the pause button.  Do you have a lot of transparency around your images?  Can you post a screen shot?

Rob

I just tried out different test cases beforehand by replacing pauseBtn in pauseBtn:addEventListener(“touch”, pauseGame) with resumeBtn and menuBtn, and the results I had explained before was what happened when I did that. I just tried it out for those buttons to see if there was a problem with the pause button itself; they were nothing more than test cases to try and figure out the error. My goal is to get this touch event working for the pause button. This is the screenshot:

Can you post your current code after the changes you’ve made?

Okay, I removed the physics body properties of the objects and put only a print statement in the touch event. 

[lua]

module(…, package.seeall)

function new()

    local localGroup = display.newGroup()

    

    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

    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

    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

    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

    local function pauseGame(event)

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

            print(“pause”)

        end

        return true

    end

    

    --Works for only menu button, sometimes for resume button, not at all for pause button

    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]

Can you add some prints into the pauseGame function to see if it’s getting fired? 

This doesn’t impact the button working, but why are you adding physics bodies to these buttons.  Do you really want physics objects bouncing off of your pause button?

Rob

Thanks so much for helping me out on this error. I did add a print statement, as shown in line 56. As I said earlier, the touch event is only being called when I press the left hand side of the resume button, completely correctly for the menu button, and nothing at all for the pause button (I want it to work for the pause button). This includes firing the print statements.  As for the physics bodies, I just wanted to see if making the button a physics body would change anything in regard to calling the touch event, but it showed no change, as you had said in your post. 

I don’t see in your code where you are ever adding a touch event on your resume button.  There is only one for the pause button.  Do you have a lot of transparency around your images?  Can you post a screen shot?

Rob

I just tried out different test cases beforehand by replacing pauseBtn in pauseBtn:addEventListener(“touch”, pauseGame) with resumeBtn and menuBtn, and the results I had explained before was what happened when I did that. I just tried it out for those buttons to see if there was a problem with the pause button itself; they were nothing more than test cases to try and figure out the error. My goal is to get this touch event working for the pause button. This is the screenshot:

Can you post your current code after the changes you’ve made?

Okay, I removed the physics body properties of the objects and put only a print statement in the touch event. 

[lua]

module(…, package.seeall)

function new()

    local localGroup = display.newGroup()

    

    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

    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

    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

    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

    local function pauseGame(event)

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

            print(“pause”)

        end

        return true

    end

    

    --Works for only menu button, sometimes for resume button, not at all for pause button

    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]