-2

How can I ensure the cards always stay close to each other as shown below?

enter image description here

The player may pick one or more cards, and I need to ensure the remaining cards snap back together. Also if the player abandons picking (thats is draws out and releases), they need to come back together.

What is the best way to go about building this?

xoail
  • 2,978
  • 5
  • 36
  • 70

1 Answers1

2

I do not know about a "best way", but one way would be to have a place holder node, and when your card is not at (0,0) on the place holder, send it back.

example:

class PlayingCard : SKSpriteNode
{
   override var position : CGPoint
   {
       didSet
       {
           if self.position != CGPoint.zero, actionForKey("reset") == nil
           {
               self.run(SKAction.moveTo(CGPoint.zero,duration:0.5).withKey:"reset")
           }
       }
   }
}

To use:

class GameScene
{
    .....
    //however you are setting this up

    let placeHolder = SKNode()
    placeHolder = placeHolderPosition

    let card = PlayingCard()
    placeHolder.addChild(card)
    self.addChild(placeHolder)
    ....
}

Now keep in mind, depending on how you are moving your card, the didSet may not be getting called on it, so you may have to do this in the update, or you can cheat and in update just do:

func update()
{
   ...
// do my stuff
  ...
   //maintain an array of all your playing cards
   playingCards.forEach{$0.position = $0.position}      

}
Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44