Event triggers multiple times on composer.gotoScene

Hi all,

Just started working with composer and I can’t figure this out.

If I have a scene that calls composer.gotoScene(“otherScene”) i will get the following events:

  1. create

  2. show(will)

  3. show(did)

Then the other scene calls composer.gotoScene(“firstScene”) and then the first scene call composer.gotoScene(“otherScene”) again and I would expect to see the “show” events again but I get this:

  1. show(will)

  2. show(will)

  3. show(did)

  4. show(did)

Doing the cycle again gives:

  1. show(will)

  2. show(will)

  3. show(did)

  4. show(will)

  5. show(did)

  6. show(did)

Since the create event is only fired one time, I can only assume there is actually only one instance of the otherScene, but the events seem to build up.

What am I doing wrong/missing? I’m using the standard scene template from http://docs.coronalabs.com/api/library/composer/index.html

Cheers

Martin

How are you calling composer.gotoScene?  inside a touch handler or a widget.newButton?

If so, keep in mind that event handlers have multiple phases, like you will get a “began” phase and an “ended” phase and if you don’t test for them you end up executing the code in the handler twice.

Rob

Hi Rob,

Thank you for your reply. I don’t think I’ve been clear enough.

I have added a very basic example app to show you. It’s zip’ed because this forum don’t allow to upload .lua files… 

The small app is two scenes each pointing to the other. Clicking forth and back will increase the number of events with each click (printed to log).

Either this is a bug in composer or in the template or me.

Cheers

Martin

@mdg,

In your code, you were adding a new event listener every time you “did” enter the scene.

I’ve modified your code to make it a little safer, but if you don’t care for my changes that is cool, just don’t add the event listener on ever “did” and you’ll be fine.

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2014/07/multievent.zip

@Rob - Sorry to snipe this, but I figured you wouldn’t mind if I answered this.

@roaminggamer,

Thank you very much for this. I can’t believe it was the buttons eventhandler I’ve overlooked. Doh!

I also really like your example of button.tap = method. I have had trouble getting the event handler method to be local. So double thanks for that. :slight_smile:

Cheers

Martin

Ed, snipe away.  This is community forums.  You guys usually know more than we do any way :slight_smile:

Rob

How are you calling composer.gotoScene?  inside a touch handler or a widget.newButton?

If so, keep in mind that event handlers have multiple phases, like you will get a “began” phase and an “ended” phase and if you don’t test for them you end up executing the code in the handler twice.

Rob

Hi Rob,

Thank you for your reply. I don’t think I’ve been clear enough.

I have added a very basic example app to show you. It’s zip’ed because this forum don’t allow to upload .lua files… 

The small app is two scenes each pointing to the other. Clicking forth and back will increase the number of events with each click (printed to log).

Either this is a bug in composer or in the template or me.

Cheers

Martin

@mdg,

In your code, you were adding a new event listener every time you “did” enter the scene.

I’ve modified your code to make it a little safer, but if you don’t care for my changes that is cool, just don’t add the event listener on ever “did” and you’ll be fine.

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2014/07/multievent.zip

@Rob - Sorry to snipe this, but I figured you wouldn’t mind if I answered this.

@roaminggamer,

Thank you very much for this. I can’t believe it was the buttons eventhandler I’ve overlooked. Doh!

I also really like your example of button.tap = method. I have had trouble getting the event handler method to be local. So double thanks for that. :slight_smile:

Cheers

Martin

Ed, snipe away.  This is community forums.  You guys usually know more than we do any way :slight_smile:

Rob