2015-10-16 57 views
0

因此,我使用此代碼順時針或逆時針旋轉對象。圍繞不是.anchorPoint的內部點旋轉SkSpriteNode

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

    let touch = touches.first as UITouch! 
    let touchPosition = touch.locationInNode(self) 
    let newRotationDirection : rotationDirection = touchPosition.x < CGRectGetMidX(self.frame) ? .clockwise : .counterClockwise 

    if currentRotationDirection != newRotationDirection && currentRotationDirection != .none { 
     reverseRotation() 
     currentRotationDirection = newRotationDirection 
    } 
    else if (currentRotationDirection == .none) { 
     setupRotationWith(direction: newRotationDirection) 
     currentRotationDirection = newRotationDirection 
    } 
} 



func reverseRotation(){ 
    let oldRotateAction = ship.actionForKey("rotate") 
    let newRotateAction = SKAction.reversedAction(oldRotateAction!) 
    ship.runAction(newRotateAction(), withKey: "rotate") 
} 



func stopRotation(){ 
    ship.removeActionForKey("rotate") 
} 



func setupRotationWith(direction direction: rotationDirection){ 
    let angle : CGFloat = (direction == .clockwise) ? CGFloat(M_PI) : -CGFloat(M_PI) 
    let rotate = SKAction.rotateByAngle(angle, duration: 2) 
    let repeatAction = SKAction.repeatActionForever(rotate) 
    ship.runAction(repeatAction, withKey: "rotate") 
} 

我的問題是,這會產生繞SKSpriteNode的定位點順時針或逆時針旋轉。

但是,我希望對象繞SKSprite內的另一個點旋轉(類似於從Sprite圖像底部開始的4/5)。我想我不能使用通過CGPointMake定義的設置點,因爲我希望精靈在屏幕上獨立移動,這不起作用?

任何建議任何人?

回答

1

您是否嘗試將您的精靈的anchorPoint屬性設置爲您想讓精靈旋轉的點?

ship.anchorPoint = CGPoint(x: 0, y: 4/5) 

這是您要尋找的?

+0

不幸的是,這導致我的精靈完全消失? –

+0

Oups,我有多愚蠢:anchorPoint從0變爲1,因此使用4/5是正確的方式......對不起! (我已經更正了我的答案。) – RoberRM

+0

Aaaaand已經完全運作了,太棒了,非常感謝! –