Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Adding sprites to a group — Gideros Forum

Adding sprites to a group

roberthahnroberthahn Member
edited January 2012 in General questions
Hey guys! Happy New Year!

I'd like to learn more about the process of adding sprites to a group. Hopefully you can help.

Suppose i have a game piece located on the stage at x:300, y:300. I scripted my game so that if you tap it, I create a group sprite, position it at x:300, y:300, add the piece to it, and set the piece's coordinates to x:0, y:0. As a result, there would be no visible change (such as movement) onscreen, but I've now added that piece to a group.

Suppose I have a second piece on the stage at x:350, y:250, which, when tapped, will be added to the group. Since I created and positioned the group sprite already, I would have expected that I would be able to add the second piece to the group, then position that piece to be x:50, y:-50 (which in theory preserves the positional relationships between the two pieces).

It's altogether possible I have a bug in my code, but that does not appear to work - one of my pieces moves up to x:0, y:0 on the stage (so it's at the upper left corner of the display) and the other one is either right on top of it or off the screen.

So: Is my understanding of how adding pieces to groups without visually altering them correct? If not, how should I be thinking of this?

One tool that may have helped me understand what's going on is the ability to set a background color or a border on the group sprite, and see how added pieces change the group sprite's dimensions. So if you would agree, I'd love to know how I could achieve that effect.


  • ndossndoss Guru
    edited January 2012
    I have a similar problem but I can't seem to replicate it with simple example code. Below is my attempt to do what you've described ...
    -- Create group sprite and add it to the stage
    local a = Sprite.new()
    -- Create bitmap and add it to stage @ 100,100
    local b1 = Bitmap.new(Texture.new("sun.png"))
    -- Reparent bitmap
    -- Reposition group sprite to 100,100
    -- Create another bitmap and add it to stage @ 150,150
    local b2 = Bitmap.new(Texture.new("sun.png"))
    -- Reparent bitmap and reposition to 50,50
    This seems to work as expected for me.

    I have code that's a part of a larger project that behaves exactly as you describe (i.e., the child TextField stays at 0,0 no matter where I move the parent) but I can't seem to replicate the bad behavior when I take the code out of the larger project. Still trying to figure out what I'm doing wrong ....

  • Found my problem ... a coding error on my part.
  • atilimatilim Maintainer
    edited January 2012
    I've coded a simple example based on your scenario:
    -- create a box and position it to (300, 300)
    box1 = Bitmap.new(Texture.new("box.png"))
    box1:setPosition(300, 300)
    -- create a group and position it to (300, 300)
    group = Sprite.new()
    group:setPosition(300, 300)
    -- set the position of box1 to (0, 0) and add to group as a child
    box1:setPosition(0, 0)
    -- create a second box at (350, 250)
    box2 = Bitmap.new(Texture.new("box.png"))
    box2:setPosition(350, 250)
    -- set the position of box2 to (50, -50) and add to group as a child
    box2:setPosition(50, -50)
    It works as expected.

    But I think the functions globalToLocal and localToGlobal would be very helpful while doing these calculations. If I replace the last two lines of the example above with this:
    x, y = box2:localToGlobal(0, 0) -- get the box2's global position
    group:addChild(box2) -- reparent it whereever you like
    box2:setPosition(box2:getParent():globalToLocal(x, y)) -- set the position relative to parent
    it works the same.

  • Thank you @atilim. I had tried the globalToLocal/localToGlobal before, but got unexpected behaviour -- this was due to bugs in my script, but the way I chose to handle it was to rewrite my code so that I could reason out what was supposed to happen, then make it right. My code is much more solid now than then, and I think I'll try using these constructs again.

    BTW, since I posted this, I did find a few bugs in my code that made me realize that I was on the right track after all, so that's good.

    Thanks again for your help. I'm pretty new to building games. I've made several attempts before on much harder game ideas in other languages/frameworks and was never able to pull it off. Thanks to the kinds of abstractions Gideros exposes, and to me picking something simpler to build, I feel like I actually have a chance to build something that I'd want to sell! :)
Sign In or Register to comment.