Event Listener Doesnt Work In Android Devices?

Hi guys, i am in the final weeks of developing my first game for both iOS and android :slight_smile:

in corona simulator everything seems perfect but apparently it’s not the case when I test my game with real device. I use galaxy nexus with android ICS 4.0

The problem is the pause and sound on/off button in the game. Here’s what I have

[code]-- PAUSE,RESUME,RESTART, SOUND BUTTON
pauseBtn = display.newImage(“pause.png”)
pauseBtn.x = ControlIconX
pauseBtn.y = ControlIconY
localGroup:insert(pauseBtn)

soundOn = display.newImage(“volume.png”)
soundOn.x = SoundIconX
soundOn.y = SoundIconY
localGroup:insert(soundOn)

soundOff = display.newImage(“mute.png”)
soundOff.x = XOutside
soundOff.y = SoundIconY
localGroup:insert(soundOff)

resumeBtn = display.newImage(“play.png”)
resumeBtn.x = XOutside
resumeBtn.y = ControlIconY
localGroup:insert(resumeBtn)

restartBtn = display.newImage(“restart.png”)
restartBtn.x = RestartIconX
restartBtn.y = ControlIconY
localGroup:insert(restartBtn)

local function pause ()
if paused == false then
physics.pause()
paused = true
gameIsActive = false
–Show resume button
pauseBtn.x = XOutside
resumeBtn.x = ControlIconX
monkeybaby:pause()
audio.stop (gameMusicChannel)
if(timerBabyEat ~= nil) then
timer.cancel(timerBabyEat)
end
end
end
pauseBtn:addEventListener(“tap”, pause)

local function resume ()
if paused == true then
physics.start()
paused = false
gameIsActive = true
–Show pause button
pauseBtn.x = ControlIconX
resumeBtn.x = XOutside
monkeybaby:play(“rolling”)
if option.getSoundStatus() == 1 then
option.setSoundStatus(1)
gameMusicChannel = audio.play(gameMusic, {channel=2, loops=-1})
else
audio.stop (gameMusicChannel)
end
timerBabyEat = timer.performWithDelay(200,babyroll,1)
end
end
resumeBtn:addEventListener(“tap”, resume)
[/code]

basically we use XOutside to put “resume” button outside the screen and we replace the pause button with this resume button when the user touch the pause button. But it doesnt work at all when I test the game in my phone (it works in corona simulator). When I said it doesnt work, i mean it doesnt work at all (nothing happens after I touch the buttons)

It seems eventlistener doesnt work in android WHILE it works in corona simulator, why is this happening?
2. Another bug is the movement to the left and right based on “virtual pad”. Here’s what I have:

[code]function touchleft (event)
if event.phase == “began” then
if gameIsActive == true then
pencetKiri = true
pencetKanan = false
motionx = -speed
motiony = 0
player:prepare(“playerleft”)
player:play(“playerleft”)
event.phase = “ended”
end
end
end
left:addEventListener(“touch”, touchleft)

function touchright (event)
if event.phase == “began” then
if gameIsActive == true then
pencetKiri = false
pencetKanan = true
motionx = speed
motiony = 0
player:prepare(“playerright”)
player:play(“playerright”)
event.phase = “ended”
end
end
end
right:addEventListener(“touch”, touchright)[/code]

so when the user touch the left button, the character will move to the left side. when he touch the right button the character will move to the right side. At the moment the codes work but sometimes the character will just keep moving without me touching it at all. Problem is, it only happens sometimes so I dont know what is wrong.

Let’s say I press left, right, left, then right for 3 seconds, then I dont touch anything…the character will then keep moving to the right direction (when I dont touch anything) until I press left again. Im not sure what’s wrong because this bug only happens sometimes. I also want to say in corona simulator the bug doesnt show up at all.

Can you guys help me what’s wrong?
[import]uid: 114765 topic_id: 26027 reply_id: 326027[/import]

>>sometimes the character will just keep moving without me touching it at all.<<

Isn’t that what your code does?
You check for event.phase == “began” and tell the sprite to start moving in a direction.
Nothing that I can see tells it to stop.

event.phase = “ended” does not do that. (I don’t think it does anything, actually)

Maybe you need something like this:

 function touchright (event)  
 if event.phase == "began" and gameIsActive then  
 pencetKiri = false  
 pencetKanan = true  
 motionx = speed  
 motiony = 0  
 player:prepare("playerright")  
 player:play("playerright")  
  
 end  
  
if event.phase == "ended" then --this happens when the finger is raised  
motionx = 0 -- stop moving  
end  
 end  

Although I was expecting to see the motion applied to the player, something like this:

if event.phase == "ended" then --this happens when the finger is raised  
 player:setLinearVelocity(0,0)  
end  

[import]uid: 108660 topic_id: 26027 reply_id: 105325[/import]

hi sir i have added event.phase = “ended” like you have suggested but it doesnt fix the bug. Sometimes it still keeps going left/right after i press the left/right arrow button and release it (keep it mind the bug only happens sometimes and not always)

[import]uid: 114765 topic_id: 26027 reply_id: 105489[/import]

hi sir, i have even tried this one

elseif event.phase == "ended" then --this happens when the finger is raised &nbsp; motionx = 0 &nbsp; motiony = 0 &nbsp; player:pause()

but still doesnt work. Sometimes the bug still shows up (although it’s random which mean i dont know what triggers it). I thought the trigger was if i pressed both left and right arrows at the same time but again most of the time it didnt happen just sometimes… [import]uid: 114765 topic_id: 26027 reply_id: 105504[/import]

hi all, it seems i still cant find solution about this and the real reason that trigger the error… :frowning: [import]uid: 114765 topic_id: 26027 reply_id: 106395[/import]