2

I want to make an SKSpritenode expand only in one direction, in this case only upwards.

override func didMoveToView(view: SKView) {
    var rightlegs = SKSpriteNode()
    rightlegs.size = CGSize(width: 10, height: 50)
    rightlegs.color = SKColor.yellowColor()
    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?) {
   /* Called when a touch begins */

So.. When i press the screen I want the height to expand; however, not from both direction. How do I solve this problem? Thank for your time:)

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
David
  • 23
  • 2
  • Just an idea here. Maybe you could try to update the size of your node while also changing its position by sizeIncrease/2. For example, to expand your node 20px to the top : rightlegs.size = CGSize(width: 10, height: 70); rightlegs.position = CGPointMake(rightlegs.position.width, rightlegs.position + 10). – Heyfara Mar 25 '16 at 11:00
  • Ooh maybe that could work thank you!, i will have to try it out. Can I change the size as a SKAction?@Heyfara – David Mar 25 '16 at 11:03

1 Answers1

1

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.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • A good explanation of `anchorPoint` property can be found [here](http://www.thinkingswiftly.com/anchorpoint-skspritenode-spritekit-swift/) as well. Also, it could be worth of mentioning that `anchorPoint` is purely visual property and doesn't affect on node's physics body (if defined). Physics body of a node will remain centered by default on node's position if not defined differently (using the `bodyWithRectangleOfSize:center:` method). Read more [here](http://stackoverflow.com/questions/22095389/sprite-kit-change-anchorpoint-but-keep-physicsbody-centered) – Whirlwind Mar 25 '16 at 12:40