Rachel
Rachel

Reputation: 13

Scaling a node based on position

I was wondering if there is a way to scale a sprite node based on its position?

So for example when a characters y position increases, you want him to appear smaller like he is walking further away from you. And when his y position decreases you want him to appear larger as he is closer to you.

let swipeUp = UISwipeGestureRecognizer()

override func didMoveToView(view: SKView) {

    swipeUp.addTarget(self, action: "swipedUp")
    swipeUp.direction = .Up
    self.view!.addGestureRecognizer(swipeUp)

    swipeDown.addTarget(self, action: "swipedDown")
    swipeUp.direction = .Down
    self.view!.addGestureRecognizer(swipeDown)

    addSprite()
}

func swipedUp(){


  let amountToMove: CGFloat = 100
  let move: SKAction = SKAction.moveByX(0, y: amountToMove, duration: 0.1)

  character.runAction(move)

}

func swipedDown(){

  let amountToMove: CGFloat = 100
  let move: SKAction = SKAction.moveByX(0, y: -amountToMove, duration: 0.1)

  character.runAction(move)

}

func addSprite(){
     let sprite = SKSpriteNode(imageNamed: "testImage")
     sprite.position = CGPoint(x: xPos, y: yPos)
}

Upvotes: 1

Views: 88

Answers (1)

Knight0fDragon
Knight0fDragon

Reputation: 16837

override the sprite position property and set the scale factor in it. Here is a very basic example, you will need to determine what your scale factor actually is based on y.

override var position{
    didSet{
      self.setScale((screen.height - y) / (screen.height / 2))
    }
}

Upvotes: 1

Related Questions