Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
SceneManager Problem — Gideros Forum

SceneManager Problem

kontinyukontinyu Member
edited July 2012 in General questions
I can't seem to erase sprites from previous scenes.
function Scene1:init(t)
 
function hexagon (x,y,w,h,c)  --xpos,ypos,width,height,color
 
	hexsprite = Sprite.new()
	hexsprite.c = c
	local hex = Shape.new()
	hex:setLineStyle(0, 0x000000, 0.5)
	hex:setFillStyle(Shape.SOLID, c)
	hex:beginPath()
	hex:moveTo(0, 0)
	hex:lineTo(w, 0)
	hex:lineTo(w+w*0.5, h*0.5)
	hex:lineTo(w, h)
	hex:lineTo(0, h)
	hex:lineTo(-w*0.5, h*0.5)
	hex:lineTo(0, 0)
	hex:closePath()
	hex:endPath()
	hexsprite:addChild(hex)
 
	hexsprite:setPosition(x,y)
	self:addChild(hexsprite)
	hexsprite:addEventListener(Event.TOUCHES_MOVE, onTouchesMove,hexsprite)
end
end


and to erase those sprites i'm using

sceneManager.scene1:removeChild(self)

in main.lua file. but when i touch on "refresh" button, - which loads same scene - my ram usage doubles. so it seems like it's not erasing hexsprites, just load another layer of them over last scene...
i'm sure it's obvious but i can't see it.

PS: Am i the only one who hates this forum's search engine?

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Well, firstly it's not deleted immediately, it's garbage collected later. And even if you call garbage collector, the memory probably still won't be released immediately.

    But as far as I know, you don't even have to remove it that way. You can simply reload the scene, to itself:
    sceneManager:changeScene("scene1", 1, SceneManager.flipWithFade, easing.outBack)
    PS: no you're not the only one. :) Use site:giderosmobile.com/forum in google to find something :)
  • I think i'm doing something wrong at adding stuff to "scene1". as child objects.
    PS:i'm already doing it that way. thx.
  • @kontinyu: Maybe you should use something like this:
    function Scene1:init(t)
    	-- do something here to initialise 
    end
     
    function Scene1:addHexagon (x,y,w,h,c)  --xpos,ypos,width,height,color
    	local hexsprite = Sprite.new()
    	hexsprite.c = c
    	local hex = Shape.new()
    	hex:setLineStyle(0, 0x000000, 0.5)
    	hex:setFillStyle(Shape.SOLID, c)
    	hex:beginPath()
    	hex:moveTo(0, 0)
    	hex:lineTo(w, 0)
    	hex:lineTo(w+w*0.5, h*0.5)
    	hex:lineTo(w, h)
    	hex:lineTo(0, h)
    	hex:lineTo(-w*0.5, h*0.5)
    	hex:lineTo(0, 0)
    	hex:closePath()
    	hex:endPath()
    	hexsprite:addChild(hex)
     
    	hexsprite:setPosition(x,y)
    	self:addChild(hexsprite)
    	hexsprite:addEventListener(Event.TOUCHES_MOVE, onTouchesMove, hexsprite)
    end
     
    -- Create your scene with parameter t (I don't know what t is used for)
    scene = scene1.new(t)
     
    -- Add 50 sprites
    local sprites = {}
    local s, hexsprite
    for s=1, 50 do sprite[s] = scene:addHexagon(x, y, w, h, c) end
     
    --[[
    	do something here
    ]]
     
    -- remove the sprites
    for s=1, 50 do 
    	hexsprite = sprites[s]
    	if scene:contains(hexsprite) then 
    		hexsprite:removeEventListener(Event.TOUCHES_MOVE, onTouchesMove, hexsprite)
    		scene:removeChild(hexsprite) 
    	end
     
    end
  • TeranthTeranth Member
    edited July 2012
    @Scouser, I like your example I have used loops to clean up a few times seems to be a better solution in the long run anyway to have a set pool of objects--as people helped me figure out with ClickFish :P

    Anyway my 2 cents here: You may want to add the following to your removing process as Gideros Seems to hold memory as long as there is ANY reference to that object weak or otherwise.

    I don't like setting self to nil, but as you example shows self being used I will use it here, it should work fine as long as you don't inherit the classes cleanup cycle.
    sceneManager.scene1:removeChild(self)
    self.c = nil
    self = nil
    On that note, I have been using the event. REMOVED_FROM_STAGE for each of my new objects, has worked wonders. Good to see I am not the only one writing a Hex rendering system though :) I was thinking about releasing a template of mine once it's done since I am sure some people would like to be able to do Hex Tiles.

    Anyway I would setup something like:
    HexTile = Core.class(Sprite)
     
    function HexTile:init(x,y,w,h,c)
    hexsprite = Sprite.new()
    	hexsprite.c = c
    	local hex = Shape.new()
    	hex:setLineStyle(0, 0x000000, 0.5)
    	hex:setFillStyle(Shape.SOLID, c)
    	hex:beginPath()
    	hex:moveTo(0, 0)
    	hex:lineTo(w, 0)
    	hex:lineTo(w+w*0.5, h*0.5)
    	hex:lineTo(w, h)
    	hex:lineTo(0, h)
    	hex:lineTo(-w*0.5, h*0.5)
    	hex:lineTo(0, 0)
    	hex:closePath()
    	hex:endPath()
    	hexsprite:addChild(hex)
     
    	hexsprite:setPosition(x,y)
    	self:addChild(hexsprite)
    	hexsprite:addEventListener(Event.TOUCHES_MOVE, onTouchesMove,hexsprite)
     
            self.hexsprite = hexsprite
     
            self:addEventListener(Event.REMOVE_FROM_STAGE, self.onRemoved, self)
    end
     
    function HexTile:onRemoved(e)
     self.hexsprite:removeEventListener(Event.TOUCHES_MOVE, onTouchesMove,self.hexsprite)
     self.hexsprite:removeFromParent()
     self.hexsprite = nil
     self.c = nil
     self:removeFromParent()
     self = nil
    end
    Hopefully I am making sense today :)

    *Edited the Init() function since I forgot to add the x,y,w,h,c arguments to it..heh
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
  • I also wrote a hex tile system but I decided to wait for @atilim to implement hex and isometric to the optimised TileMap renderer. I have also started using Event.REMOVED_FROM_STAGE, it fixed a couple of problems I had. I had a conversation with @techdojo this afternoon discussing the removal of the REMOVED_FROM_STAGE event handler in the event handler itself.

    I did only throw that example together and said something like this in my post so it was implied that it wasn't the only way to do it :)
  • @Scouser hmm that is actually a question I had as well, I was wondering if I should remove that event inside of the event call, or if that was unneeded as generally the garbage collector will remove that object altogether--and hopefully the events I would assume, but I wonder if that event could cause the garbage collector to not free the memory due to the reference? :)

    Yeah I was sort of hoping @atilim would finish his Hex/Iso setup with the tile-maps before I needed to use mine, since I am not getting the same performance out of my HexRenderer compared to the tilemap setup in Gideros. There is just something cool about Iso/Hex that makes me always want to make my games in that format--even though my artists HATE making iso tiles heh :P
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
  • Can someone tell me why self:addChild(hexsprite) used here. Doesnt it need to be added something like a parent node , why adding it to itself ?
  • ar2rsawseenar2rsawseen Maintainer
    @twisttap
    because inside function Scene1:init self is referenced to a current scene which is Scene1, which is now loaded in SceneManager.
    So this scene is added to stage and everything added to this scene will
    1. appear on screen when scene appears
    2. get removed from screen when scene is removed

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • TeranthTeranth Member
    edited July 2012
    @twisttap Since HexTile is a Sprite itselfs, to make sure that the hexsprite is shown where-ever the HexTile has been displayed you add the sprite to it's parent the HexTile--then when you add a new HexTile to the scene, you don't have to add the sprite and the HexTile they are tied together from the beginning.

    Since it is done this way, you can add a HexTile to your stage by doing the following since it is all handled internally:
    local h  = HexTile.new(0,0, 10, 10, 10, 0x00FF00)
    stage:addChild(h)

    Likes: MoKaLux

    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.