将spriteNode缩放为较小(或较大)大小时,默认情况下,spriteNode从中心缩放,通常很好。但是,我需要将精灵从其身体的最左侧缩放。有什么方法可以改变spriteNode的中心点,以便实现这一目标?

我尝试调整了spriteNode的锚点(如下),该锚点确实从主体的左侧开始缩放,但是它也重新定位了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))

    }

最佳答案

尝试删除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))


我没有测试... :)

10-04 23:12