You can do it by changing the anchorPoint property of the sprite to CGPoint(x:0.5, y:0.0)
(this will bottom-align sprite's texture) and scaling the sprite's height using yScale property, like this:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let rightlegs = SKSpriteNode(color: .yellowColor(), size: CGSize(width: 10, height: 50))
override func didMoveToView(view: SKView) {
rightlegs.anchorPoint.y = 0.0
rightlegs.position = CGPointMake(self.frame.width / 2 + 20, self.frame.height / 2 - 40)
self.addChild(rightlegs)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Without animation
rightlegs.yScale += 0.2
}
}
Or you can make it animated using some of the SKAction scaling methods:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//With animation
rightlegs.runAction(SKAction.scaleYTo(rightlegs.yScale + 0.2, duration: 0.2))
}
About anchorPoint
from docs:
Defines the point in the sprite that corresponds to the node’s
position.
You specify the value for this property in the unit coordinate space.
The default value is (0.5,0.5), which means that the sprite is
centered on its position.
Basically what anchorPoint
does is that it defines how texture will be drawn relative to node's position and affects on rotation and scaling of a node...
A good explanation about how anchor point works and what it does can be found in Apple's Working with Sprites section of documentation.