Touched still pressed on Director change

Hi all, I’ve got an irritating problem occurring with my code and Director class.

I’ve got some basic controls i’ve created to move a character left right, changing the x values for the image/character i’ve created.

I’ve also got a countdown timer which when it gets to zero changes scenes automatically.

Everything works great but when the timer gets to zero and a finger is still touching the screen it causes an error because it was still trying to move my character, the screen still changes but weird things occur afterwards because of the error.

The error: attempt to perform arithmetic on field ‘x’ (a nil value)

Which I understand why its happening but can’t think of a graceful solution. The only one I’ve created so far is to watch the clock and disable the controls with one second to go but this is still ineffective if you just rest your finger on the screen.

So I think i’m looking for a way to override all touch events when the the clock hits zero.

Can anyone help?

[import]uid: 33866 topic_id: 7311 reply_id: 307311[/import]

Can you not just remove your listeners from the controls when the clock reaches zero? Or use a boolean variable to set if controls active or not and set to false when clock reaches zero. [import]uid: 7863 topic_id: 7311 reply_id: 25711[/import]

I thought this too, but they don’t seem to stop ‘listening’ if you still have your finger pressed down… [import]uid: 33866 topic_id: 7311 reply_id: 25719[/import]

Fixed:) I moved my function to the bottom of my lua file, now it can see the listeners and all is well! [import]uid: 33866 topic_id: 7311 reply_id: 25722[/import]

@cl-apps

Can you explain to me why that would matter?
Should I always put my listeners at the top of the code?

I have been putting them at the bottom below the function it is listening for. [import]uid: 22392 topic_id: 7311 reply_id: 25735[/import]

@noahm26

Having your listeners at the bottom of the lua file is normal. However if you reference a listener on an object (object:addEventListener(“touch”, whatever)) then the function that includes object:removeEventListener(“touch”, whatever) has to be underneath else the console will moan that object = nil and won’t remove the event listener.

This is what I’ve found anyway, Runtimes don’t suffer this I’ve found. [import]uid: 33866 topic_id: 7311 reply_id: 25760[/import]