swift - 围绕非 .anchorPoint 的内部点旋转 SkSpriteNode

标签 swift rotation sprite-kit sprite

所以我使用此代码顺时针或逆时针旋转对象。

    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 的 anchor 创建顺时针或逆时针旋转。

但是,我希望该对象绕 SKSprite 内部的另一个点旋转(类似于 Sprite 图像底部向上 4/5 的位置)。我假设我不能使用通过 CGPointMake 定义的设定点,因为我希望 Sprite 在屏幕上独立移动,但这行不通?

有人有什么建议吗?

最佳答案

您是否尝试过将 Sprite 的 anchorPoint 属性设置为您希望 Sprite 围绕其旋转的点?

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

这是您要找的吗?

关于swift - 围绕非 .anchorPoint 的内部点旋转 SkSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33175072/

相关文章:

ios - 游戏 SKScene 转换 Swift

ios - 在VC上快速拖动两个 View

ios - Swift:以模态方式呈现 View Controller 时背景颜色发生变化

swift - 有没有办法独立于我的应用程序在 macOS 上跟踪鼠标移动?

UINavigationController 中的 UITableView 旋转后进入导航栏

javascript - 如何使用 javascript 将元素设置为旋转和轨道动画

ios - 如何将粒子附加到移动的物体上

swift - SceneKit 和 SpriteKit 结合在一起

ios - 渐变图层在 Xib 文件中的纵向模式下不起作用

cocoa - NSImageViews 中的图像随机颠倒