newbie Director problem

I am using a shake event to subtract 1 from a global variable (bullseyeOne) every time the phone is shaken. It works fine unless I use my button to reload the page using director. After I reload, I subtract 2 every shake. reload again, and subtract 3, etc.

Obviously doing something wrong. Sure would appreciate some pointers, I’m pretty new at this. Thanks!

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()

display.setStatusBar(display.HiddenStatusBar)

local background = display.newImage (“farmbg.jpg”)
localGroup:insert(background)


local function pressBack(event) – director function reloads page
if event.phase == “ended” then
director:changeScene (“level1”, "fade ")
end
end


– display button to relaod page
local backButton = display.newImage (“againButton.png”)
backButton.x = 160
backButton.y = 455
localGroup:insert(backButton)
backButton:addEventListener( “touch”, pressBack)


local function shakeme( event ) – shake event subtracts 1 from bullseyeOne
if (event.isShake) then
myCircle = display.newCircle( 90, 140, 50 )
myCircle:setFillColor(0,0,0)
myCircle:setStrokeColor ( 255, 0, 0)
myCircle.strokeWidth = 2
transition.to (myCircle, {time=3000, alpha =0})

_G.bullseyeOne = _G.bullseyeOne - 1

print("bullseyeOne = "…bullseyeOne)
end
end


Runtime:addEventListener(“accelerometer”,shakeme)

return localGroup
end
[/lua] [import]uid: 96383 topic_id: 17522 reply_id: 317522[/import]

it just means that the event is not being removed… and being duplicated.

you are adding the Runtime:addEventListener everytime, but you are *not* removing it. So when you reload, you must just remove the eventListener. There are two ways of managing it.

set up a variable/boolean/flag whatever you want to call it.

local isHooked = false
local function shakeme

function shakeme(event)

end

if isHooked ~= false then
Runtime:removeEventListener(“accelerometer”, shakeme)
end

Runtime:addEventListener(“accelerometer”, shakeme)
isHooked = true
then in your reload, you will be able to remove the previous handler and recreate a new one. So no more duplication.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17522 reply_id: 66572[/import]

Thanks so much! [import]uid: 96383 topic_id: 17522 reply_id: 66595[/import]

Hey JayantV, I tried to implement your suggestion, but had no success. I have distilled the code (and the problem) still farther. I cannot find a way to remove the Runtime listener for my shake event. Please, if you have a moment, I have been wracking my brains for several days. If you use this code, and press the white button several time, then initiate a shake event, you will see printed in terminal “Level 2 Bullseye” as many time as you clicked the button. I have tried every way from Sunday to remove that listener so that regardless of the number of times you click the button you only see the words printed once. Could I trouble you to have a look and make another suggestion, or let me know in the more likely scenario that I mis applied your code. Thanks so much.

[lua]module(…, package.seeall)

function new()
local localGroup = display.newGroup()

display.setStatusBar(display.HiddenStatusBar)

Runtime:removeEventListener(“accelerometer”,shakeme2)

local background = display.newRect( 0, 0, 320, 480 )
background:setFillColor(255,255,255)
localGroup:insert(background)


function pressBack(event) – button function
if event.phase == “ended” then
director:changeScene (“level2”, “fade”)
end
end


local backButton = display.newRect ( 60, 415, 200, 50 ) – button
backButton:setFillColor(0,0,0)
localGroup:insert(backButton)
backButton:addEventListener( “touch”, pressBack)


local isHooked = false

local function shakeme2( event ) – shake function
if (event.isShake) then
print (“Level 2 Bullseye”)
end
end

if isHooked ~= false then
Runtime:removeEventListener(“accelerometer”, shakeme2)
end

Runtime:addEventListener(“accelerometer”,shakeme2)
isHooked = true


return localGroup
end[/lua] [import]uid: 96383 topic_id: 17522 reply_id: 73654[/import]

Am wrestling with the same problem.
My conclusion so far is that it’s how the Director loads Lua screens. It doesn’t only load the main constructor but the entire file.

So in your code example above director will load the entire file which results in you first setting isHooked to false, because it just got restated. It will not be true where you are checking it, therefore you do not remove it. So you end up duplicating them.

The best protection that I’ve found is to use a “pre scene” which stores system flags such as these. Call it the “load scene” which is trigger by main. Instead of going to the scene you have up there you load go director:changeScene(loadScene.lua)
declare all global system flags in this .lua file and the finnish it off by doing a
director:changeScene(xxx.lua)

Whatever the name of your game file is.

Hope that helps! [import]uid: 99059 topic_id: 17522 reply_id: 80721[/import]

It works on my app now so I’ll post the details of my problem and solution.
I wanted to use the back button on Android to shift back through the menus for users who prefer that navigation method.

Each time I got back to the main menu it would trigger a reinitialisation of the listener.
The solution was that I added a scene which I call globalSetup.lua. This is the scene loaded from my main.lua. Within it I placed this code:

backButtonReady = false  
  
local myclosure = function() return director:changeScene("mainMenu") end  
timer.performWithDelay(100, myclosure, 1)  

Note the timer. You run into other problems if you try to directly run the changeScene without a delay or something else in there.

When initializing my trigger to activate the back buttons I had this piece of code:

if (globalSetup.backButtonReady == false) then  
 initVars()  
end  

The initVars() was modified from the sample code found here:
http://developer.anscamobile.com/forum/2011/08/18/android-device-back-button-utilizing-director-class

To look like this:

local function initVars ()  
  
 -----------------------------------  
 -- Listeners  
 -----------------------------------  
 Runtime:addEventListener( "key", onKeyEvent )  
 Runtime:addEventListener( "enterFrame", animate )  
 globalSetup.backButtonReady = true  
end  

There. Now it only triggers once. The problem I used to have was that it worked perfectly until I got back to the main menu. After that it would double trigger. That’s because I never removed the old listener but kept adding a new one.

Seems a lot like your problem.

I have my back button setup as a global listener and it listened through all the scenes.

This might not be the cleanest solution to you problem, but it worked for me. [import]uid: 99059 topic_id: 17522 reply_id: 80736[/import]