Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to effectively handle Runtime EventListener
Started by vb66r55 Nov 07 2018 10:01 AM

4 replies to this topic

Best Answer Rob Miracle , 07 November 2018 - 12:45 PM

You **cannot** remove the scene you're in except under one very specific condition:

function scene:hide( event )
     local phase = event.phase

     if phase == "did" then
          composer.removeScene("thecurrentscene") -- where you provide the value for the current scene
     end
end

If you don't remove your scene that way, your only choice is to remove it when another scene is on the screen. But as I said you only need to do this if you're running low on memory. If you're just doing it as a convenient way to reset a scene, then you don't need to remove it until you're ready to go to it as I showed in the previous.

 

Scenes are discrete objects. 

composer.removeScene("Battle")
composer.gotoScene("Menu")

doesn't exist between scenes or outside of the scene. That code is going to be inside a scene and you have to follow the rules, i.e. you can't remove the scene that's on screen except in the one circumstance I stated.

 

Seriously, either remove the scene when it hides or call removeScene() before you gotoScene() and leave the other in memory until you need to go back to it.

 

Rob

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

vb66r55

[GLOBAL: userInfoPane.html]
vb66r55
  • Observer

  • 11 posts
  • Corona SDK

Greetings,

 

I use a lot of Runtime EventListener in the game,but when I removeScene the Runtime EventListener not cleared.

 

Does this mean that I have to manually remove it?

 

Because I use them individually, I want to know if there is a better way.

 

Another problem is when I composer.removeScene( "Battle") and composer.gotoScene( "Menu" ) ,repeated this method several times,the Corona Console told I can not removeScene( "Battle") because it not exist,and told me use loadScene to replace gotoScene.
 
I don't know much about the difference between the two??
 
Thanks.


[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,860 posts
  • Corona Staff

You are responsible for removing any Runtime listeners yourself. Touch and Tap listeners are attached to objects and when Composer hides a scene (or removes it) those touch handlers can't be interacted with (or will be removed for you), but anything on the Runtime isn't managed by Composer.

 

As for the second issues, that's a very odd message. It would be very helpful if you could copy and paste the exact text from the console log.

 

composer.loadScene() is used to pre-load a scene. Basically, it creates the scene object and calls scene:create() and nothing more. Then later you can call composer.gotoScene() and you won't have to do all the work that scene:create() does which can improve performance for scenes that take a lot to construct. In most cases you don't need to do this as scene creation is pretty quick.

 

You can have a scene remove itself if you want, but you can only do it in scene:hide()'s "did" phase. Unless you need to free up memory, immediately, you don't need to remove a scene right away. Composer is designed to cache scenes so you can avoid re-creating them later. Most people choose to remove scenes because it's the lazy way to reset a scene. I'm even guilty of taking this shortcut.

 

In my case, since I'm not stressing memory with my game scene, I won't remove it right away. Instead, I will do:

composer.removeScene("game")
composer.gotoScene("game")

so that it resets right before I want to go to it. 

 

Rob



[TOPIC: post.html]
#3

vb66r55

[GLOBAL: userInfoPane.html]
vb66r55
  • Observer

  • 11 posts
  • Corona SDK

You are responsible for removing any Runtime listeners yourself. Touch and Tap listeners are attached to objects and when Composer hides a scene (or removes it) those touch handlers can't be interacted with (or will be removed for you), but anything on the Runtime isn't managed by Composer.

 

As for the second issues, that's a very odd message. It would be very helpful if you could copy and paste the exact text from the console log.

 

composer.loadScene() is used to pre-load a scene. Basically, it creates the scene object and calls scene:create() and nothing more. Then later you can call composer.gotoScene() and you won't have to do all the work that scene:create() does which can improve performance for scenes that take a lot to construct. In most cases you don't need to do this as scene creation is pretty quick.

 

You can have a scene remove itself if you want, but you can only do it in scene:hide()'s "did" phase. Unless you need to free up memory, immediately, you don't need to remove a scene right away. Composer is designed to cache scenes so you can avoid re-creating them later. Most people choose to remove scenes because it's the lazy way to reset a scene. I'm even guilty of taking this shortcut.

 

In my case, since I'm not stressing memory with my game scene, I won't remove it right away. Instead, I will do:

composer.removeScene("game")
composer.gotoScene("game")

so that it resets right before I want to go to it. 

 

Rob

Dear Rob,

 

Thank you for your answer.

 

The two question console log↓

 COMPOSER: 
 Battle's was not removed because it does not exist. Use composer.loadScene() or composer.gotoScene().
 
My approach is 
composer.removeScene("Battle")
composer.gotoScene("Menu")
 

and

composer.removeScene("Menu")
composer.gotoScene("Battle")
 

Because I want the players to go back and choose their skills and challenge them again.

 

Can I have a better way?

 

Even if it’s too much trouble, it’s okay, I want to have a more accurate way to use the scene back and forth.

 

Thanks   :)



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,860 posts
  • Corona Staff

  Best Answer

You **cannot** remove the scene you're in except under one very specific condition:

function scene:hide( event )
     local phase = event.phase

     if phase == "did" then
          composer.removeScene("thecurrentscene") -- where you provide the value for the current scene
     end
end

If you don't remove your scene that way, your only choice is to remove it when another scene is on the screen. But as I said you only need to do this if you're running low on memory. If you're just doing it as a convenient way to reset a scene, then you don't need to remove it until you're ready to go to it as I showed in the previous.

 

Scenes are discrete objects. 

composer.removeScene("Battle")
composer.gotoScene("Menu")

doesn't exist between scenes or outside of the scene. That code is going to be inside a scene and you have to follow the rules, i.e. you can't remove the scene that's on screen except in the one circumstance I stated.

 

Seriously, either remove the scene when it hides or call removeScene() before you gotoScene() and leave the other in memory until you need to go back to it.

 

Rob


  • vb66r55 likes this

[TOPIC: post.html]
#5

vb66r55

[GLOBAL: userInfoPane.html]
vb66r55
  • Observer

  • 11 posts
  • Corona SDK

I understand what I should do.

 

Thank you.

 

:D




[topic_controls]
[/topic_controls]