Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Layers again and timed additions — Gideros Forum

Layers again and timed additions

DikkesnoekDikkesnoek Member
edited May 2013 in General questions
Hi,

I am struggling again with sprites and layers. I am trying some tips from previous
examples on this forum. I have a foreground layer which is the half top screen
like this:

BackgroundLayer = Core.class(Sprite)
local backgroundLayer = BackgroundLayer()

A layer where some bitmaps will be added every second and falling down:

FiguresLayer = Core.class(Sprite)
local figuresLayer = FiguresLayer.new()

And a foreground layer from half of the screen to the bottom:

ForegroundLayer = Core.class(Sprite)
local foregroundLayer = ForegroundLayer.new()

I want that the figures will be disappear when falling down and reaching the
foreground layer. So I tried to first add the backgroundLayer to the stage,
then the figuresLayer and at last the foregroundLayer. Result is that due the
timed addition of the new bitmap/sprites they will be on top of all the layers.
They will not disappear at all below the foregroundLayer.

Is there a good way to handle timed sprite additions?

Regards,

Marc

Comments

  • petecpetec Member
    Every time you add a sprite I'd try removing the foreground layer from the stage and then immediately adding it to the stage again to bring it to the front.

    I seem to remember reading that you don't actually have to remove it first, simply adding it again will bring it to the front, but without trying that I can't be sure :-S
  • ar2rsawseenar2rsawseen Maintainer
    @Dikkesnoek sorry I don't understand, if Figures layer is under foreground layer and you add figures to figures layer, how can they be on top of foreground layer?
  • @Dikkesnoek: Are you adding your timed sprites to stage instead of figuresLayer? You should not have to do anything with the layers once they are added to the stage but you MUST add the timed additions to the figuresLayer to get the effect you desire.
  • DikkesnoekDikkesnoek Member
    edited May 2013
    Hi,

    Thanks for your comment. To make it more clear I've drawn a sketch.

    image

    I tried to create 3 layers with different graphics. When all the graphics
    are added I add the layer to the stage.

    I thought that the falling figures layer must be between the foreground
    and the background layer. The difficulty is that the falling objects will
    keep coming every second. I don't know how to add the objects to the
    layer and finally to the stage in a proper way.

    Note that I've made the pipe sprite with the contact (onContact event)
    part of the foreground layer. Objects who are directed to the pipe have
    to disappear and give points for example. Now I see the figures on top
    of the pipe. Redrawing the foreground layer could be an interesting
    option. I hope that I've made it more clear.

    Regards,

    Marc
    Falling_Objects_Example.png
    495 x 334 - 36K
  • ar2rsawseenar2rsawseen Maintainer
    edited May 2013
    So you should do something like this:
    function scene:init()
        self.backgroundLayer = Sprite.new()
        self:addChild(self.backgroundLayer)
     
        self.figureLayer = Sprite.new()
        self:addChild(self.figureLayer)
     
        self.foregroundLayer = Sprite.new()
        self:addChild(self.foregroundLayer)
     
        -- now here you can add all the needed graphics
        self.backgroundLayer:addChild(Bitmap.new(Texture.new("bg.png", true)))
        self.foregroundLayer:addChild(Bitmap.new(Texture.new("fg.png", true)))
     
        self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end
     
    function scene:onEnterFrame()
        local fallingObject = Bitmap.new(Texture.new("fo.png", true))
        self.figureLayer:addChild(fallingObject)
    end
    BTW you should also split you can or bucket or whatever you have there in two pieces (background and foreground) to get the desired effect, and add each to a corresponding layer (background and foreground)
  • Hi ar2, thanks again. I will check your code. About splitting the bucket, you're
    right. I created separate parts. I am always struggling with stacking sprites.
    You helped me before on this but I have no a little devious situation.

    Regards,

    Marc
  • Do you think that I can the same by using the timer, like this:
     
    -- Every few seconds, we create a new figure.
    local function onTimer()
    	-- figure starts at random X position.
    	local figureStartX = math.random(40, 920)
     
    	figureType = "Rectangle"
    	figureWidth = 74
    	figureHeigth = 94
    	figure = Figure.new(figureStartX, figureStartY, 
                                figuretWidth, figureHeigth, figureType, world)
    	figureLayer:addChild(figure)
    	actors[figure.body] = figure
    end
     
    -- Set the timer for every x seconds to let a figure fall.
    local timer = Timer.new(1000, 0)
    timer:addEventListener(Event.TIMER, onTimer)
    timer:start()
    onTimer()
    Figure is the class where the falling figure will be created.

    Regards,

    Marc
  • It works, thank you all for your help. This forum is amazing!

    Regards,

    Marc
  • ar2rsawseenar2rsawseen Maintainer
    Yes sure you can, most important is that figureLayer is added between background and foreground layers and that you add all the figures only to it
Sign In or Register to comment.