2016-05-08 73 views
0

當將spriteNode縮放爲更小的尺寸(或更大尺寸)時,默認情況下,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 

此外,下面的得到我想要的結果,但我真的不希望有調整spriteNode位置,每次我調整anchorPoint:

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

根據你的精靈,你不妨用中心矩形縮放在您的精靈的中間部分是一樣的,當你縮放,所以保持邊緣和角落,但擴大中間。 [蘋果在此描述](https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Sprites/Sprites.html#//apple_ref/doc/uid/TP40013043-CH9-SW10)。 – Gliderman

回答

0

嘗試在didloadtouchesBegan刪除mySprite.anchorPoint = CGPointMake(0.0, 0.5)

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)) 

我沒有測試它... :)