从特定点缩放SpriteNode

时间:2016-05-08 00:38:33

标签: swift sprite-kit scale skspritenode

当将spriteNode缩放到较小的大小(或更大的大小)时,spriteNode默认从中心缩放,这通常很好。但是,我需要我的精灵从它的身体的左边开始缩放。有没有办法改变spriteNode的中心点,以便我能实现这个目标?

我尝试调整spriteNode的锚点(下面),它从body的左边开始缩放,但它也重新定位了我不想要的spriteNode。

import SpriteKit

var mySprite: SKSpriteNode!

class GameScene: SKScene {

    override func didMoveToView(view: SKView) {

        anchorPoint = CGPointMake(0.5, 0.5)
        backgroundColor = UIColor.greenColor()

        mySprite = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100, 50))
        mySprite.anchorPoint = CGPointMake(0.0, 0.5)
        addChild(mySprite)

    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        mySprite.runAction(SKAction.scaleTo(0.5, duration: 1))

    }

我希望得到类似下面的内容(不存在):

mySprite.centerPoint.x = -mySprite.size.width/2

另外,下面得到了我想要的结果,但我不想每次调整anchorPoint时都要调整spriteNode位置:

var mySprite: SKSpriteNode!

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {

        anchorPoint = CGPointMake(0.5, 0.5)
        backgroundColor = UIColor.greenColor()

        mySprite = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100, 50))
        mySprite.anchorPoint = CGPointMake(0.0, 0.5)
        mySprite.position.x = -mySprite.size.width/2
        addChild(mySprite)

    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        mySprite.runAction(SKAction.scaleTo(0.5, duration: 1))

    }

1 个答案:

答案 0 :(得分:0)

尝试删除mySprite.anchorPoint = CGPointMake(0.0, 0.5)didload中的touchesBegan

mySprite.anchorPoint = CGPointMake(0.0, 0.5)
func repositionAncorPoint() {
    mySprite.anchorPoint = CGPointMake(0.5, 0.5)
}
let resetAncor = SKAction.runBlock({ repositionAncorPoint() })
let rescale = SKAction.scaleTo(0.5, duration: 1)
let sequence = [rescale,resetAncor]
mySprite.runAction(SKAction.sequence(sequence))

我没试过它......:)