Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Issue with Event.REMOVED_FROM_STAGE — Gideros Forum

Issue with Event.REMOVED_FROM_STAGE

MellsMells Guru
edited February 2013 in General questions
Hi all,

I am facing an issue with Event.REMOVED_FROM_STAGE.
In my example when a sprite is removed from his parent :
- if its direct parent is stage, then the event is triggered
- if its direct parent is NOT stage, then then event is not triggered

Doesn't removing a sprite from its parent trigger the "removed from stage" event?
If not, how can I trigger the event?

What am I doing wrong?

The code is below, and I have attached a test file.
 
Game = Core.class(Sprite)
 
function Game:init()
	function onRemovedFromStage(sprite)
			print ("------------------------------------------------- "..sprite.name.." REMOVED FROM STAGE")		
	end
 
	_mySprite1 = Sprite.new()
	_mySprite1.name = "Sprite1"
	self:addChild(_mySprite1)
	_mySprite1:addEventListener("removedFromStage", function() onRemovedFromStage(_mySprite1) end)
	_mySprite1:removeFromParent()
 
 
	_mySprite2 = Sprite.new()
	stage:addChild(_mySprite2)
	_mySprite2.name = "Sprite2"
	_mySprite2:addEventListener("removedFromStage", function() onRemovedFromStage(_mySprite2) end)
	_mySprite2:removeFromParent()
end
 
game = Game.new()
I get :
------------------------------------------------- Sprite2 REMOVED FROM STAGE

Dislikes: Yan

twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
+1 -1 (+0 / -1 )Share on Facebook

Comments

  • BJGBJG Member
    edited February 2013
    The reference manual notes that: "If a sprite is removed from the scene tree, the sprite instance and all of its descendants receive Event.REMOVED_FROM_STAGE".

    However, "Sprite1" isn't part of the scene tree because it's not added to the stage and its parent isn't either. If you included "stage:addChild(self)" before "self:addChild(_mySprite1)" then the event would trigger.
  • MellsMells Guru
    edited February 2013
    @BJG
    I have failed to describe my issue.
    For now I am using the scene manager that is shipping with most of the Gideros examples (ebook) and is given as a resource for beginners for best practices.
    I am transitioning to another scene manager but I have to make it work for a project that I had from "the past".

    So - with the scene manager shipped with Gideros - , it doesn't seem to work.
    I don't understand : if sprites added as child to each Class (page) are displayed on screen, that means that the page is added to the stage.

    Please find the files attached (I have modified Page0 to include the removeFromParent() code and that's it).

    At least I wanted people to know about it (unless it's another mistake that I have made?), it can be confusing for those of us who use the SceneManager and assume that .
    zip
    zip
    Gideros_Ebook_Template.zip
    6M
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • BJGBJG Member
    edited February 2013 Accepted Answer
    Oh, OK. I'm not sure how that template works, but if you add: "stage:addChild(self)" at the beginning of the init function for Page0...
    Page0 = gideros.class(Sprite)
     
    function Page0:init(pageNo, parent)
    	stage:addChild(self)
    	self.no = pageNo
    ...then both events fire. So I think the problem is that the object being created from the Page0 class (and therefore _mySprite1) isn't part of the scene tree when you expect.

    (I'm not suggesting that this is the solution; it's just for debugging.)
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @Mells what @BJG describe is right. Moving code from init function to enterEnd event should solve that problem. Because you see, init method is executed when creating object, before it is added to the stage.

    So
    Game = Core.class(Sprite)
     
    function Game:init()
        self:addEventListener("enterEnd", self.onEnterEnd(, self)
    end
     
    function Game:onEnterEnd()
    	function onRemovedFromStage(sprite)
    			print ("------------------------------------------------- "..sprite.name.." REMOVED FROM STAGE")		
    	end
     
    	_mySprite1 = Sprite.new()
    	_mySprite1.name = "Sprite1"
    	self:addChild(_mySprite1)
    	_mySprite1:addEventListener("removedFromStage", function() onRemovedFromStage(_mySprite1) end)
    	_mySprite1:removeFromParent()
     
     
    	_mySprite2 = Sprite.new()
    	stage:addChild(_mySprite2)
    	_mySprite2.name = "Sprite2"
    	_mySprite2:addEventListener("removedFromStage", function() onRemovedFromStage(_mySprite2) end)
    	_mySprite2:removeFromParent()
    end
    I think should solve the problem
  • @BJG, @ar2rsawseen
    I just found out about it (tried with a timer).
    Thank you :)
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • ar2rsawseenar2rsawseen Maintainer
    edited February 2013
    @Mells timer would also solve this, because then the code will be executed after init method. But using event should be more efficient and correct solution ;)
  • @ar2rsawseen
    Yes sure, I meant that I was using a timer just to test and this made me understand (one second before I got notified by your answer :) that the error had to do with removing after the init.
    thanks for the help.

    Likes: ar2rsawseen

    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.