Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
moving groups of sprites — Gideros Forum

moving groups of sprites

roberthahnroberthahn Member
edited December 2011 in General questions
Ok, sorry for spamming you with lots of questions, this should be my last post for tonight.

I'm porting a board game I love to the iPad. The key game mechanic I'm working on right now involves moving a line of stones in a row across a grid. For example, if I have 3 stones in a row like so:

. O O O . . .

I'd like to put one finger on each end of that group, then slide the whole group left or right.

To begin, I wanted to just get one stone moving, then start moving 2 or more once I have the basic workflow done.

After looking over the docs, it seemed to me that this would be the correct way to solve the problem:

* all stones needed to play the game are added directly to the stage when the game starts.
* at the top of a lua file, define group = Sprite.new()
* in a function that captures Event.TOUCHES_BEGIN events, locate the stone that was hit and add it to group.
* when I want to slide the group across the board, i can just update the X/Y position of the group, and all the stones in it (all one of them for now) will come along for the ride
* Once I am done moving the group, put the stones, one at a time, back into the stage, at the new position.

So far I have the first 4 points working for a single stone. Once I have all 5 points done, I'll add more stones to the group to ensure it continues to work.

The problem is at the fifth step. When I add the stone back to the stage, it goes right back to the location it was in originally. I tried to save the X/Y coordinates of the group beforehand, but that did not seem to work either.

I hope I'm explaining this clearly. Can you tell me how to move the stone from one group to another without moving it on the screen?
Tagged:

Comments

  • atilimatilim Maintainer
    edited April 2012
    Hi,

    There are two functions Sprite:localToGlobal and Sprite:globalToLocal to solve this problem. You can use these functions like:

    1. Use Sprite:localToGlobal to find the global (screen) coordinates of the sprite before moving:
    local x, y = sprite:localToGlobal(0, 0)
    2. Move your sprite to another group.
    anotherGroup:addChild(sprite)
    3. Find the new local coordinates in this group by using Sprite:globalToLocal and set the position:
    sprite:setPosition(anotherGroup:globalToLocal(x, y))
    Hope this helps. Happy coding on your new game :)
  • I see! thanks for your help!
Sign In or Register to comment.