(SWIFT) Sprite 在添加后没有给我正确的 zRotation 值

Posted

技术标签:

【中文标题】(SWIFT) Sprite 在添加后没有给我正确的 zRotation 值【英文标题】:(SWIFT) Sprite not giving me the correct zRotation value after being added 【发布时间】:2015-09-01 08:42:42 【问题描述】:

程序详情:通过看这张图片(http://i.stack.imgur.com/QOZ53.png),我想做的就是让宇宙飞船绕地球一圈。我通过制作一个线节点并将其锚点更改为顶部,然后将其定位到行星的中心来实现这一点,然后在宇宙飞船和行星撞击时,线与船成角度,然后船被移除从视图上看,作为一个孩子添加到线节点,它们一起绕着行星旋转,当船绕着地球旋转时,它自己的船也在原地旋转(希望这是有道理的)

问题:问题是我没有得到正确的船的 zRotation 值。现在我得到了代码来在它被点击的位置绘制另一艘船,并将该图像的 zRotation 设置为船的 zRotation,但我不断得到不同的角度。 (参考图片)。这是因为我小时候已经将船添加到线节点吗?我在线路和船上运行旋转动画。在图片中,这条线绕着行星逆时针旋转,拖着船跟着它,船本身也在原地逆时针旋转。图中左边的船;接触线的是旋转的那艘,右边的船只是根据旋转船的角度绘制的,但是通过查看图片,您可以看到船的角度与左边的船完全相反。为什么会这样?我注意到的是,当船在地球的下半部分时,角度很好,但到上半部分时,角度有点相反(参考图片)

图片

控制台日志:

我很感动 - 以度为单位的船舶角度:83.6418545381942 球员位置 X: 100.0 Y:100.0 玩家 ZROTATION: 1.45982575416565

我们正在接触 - 行星位置 X*:120.0 Y:230.000015258789玩家-船位置 X:107.998710632324 Y:171.783294677734 线的角度弧度*:-1.77409685090117 度数:-101.648262004087 玩家船轮换:1.57079637050629

我很感动 - 以度为单位的船舶角度:314.660859137531 TEMP POS X:136.535125732422 Y:287.094879150391 TEMP ZROTATION:5.491868019104 球员位置 X: 136.535125732422 Y: 287.094879150391 玩家 ZROTATION: 5.491868019104

碰撞码:

func didBeginContact(contact: SKPhysicsContact) 

    if contact.bodyA.categoryBitMask == planetGroup || contact.bodyB.categoryBitMask == planetGroup 


         print(" WE'RE TOUCHING ")

        moving = true
        touching = true

        let degrees = 45.0
        let radians = degrees * M_PI / 180.0

        //rotate Line
        var rotateLine = SKAction.rotateByAngle(CGFloat(radians), duration: 0.5)
        var repeatLine = SKAction.repeatActionForever(rotateLine)

        //rotates Ship
        var rotateShip = SKAction.rotateByAngle(CGFloat(radians), duration: 0.4)
        var repeatShip = SKAction.repeatActionForever(rotateShip)



        playerShip.physicsBody?.velocity = CGVector(dx: 0, dy: 0)


        planetNode = contact.bodyA.node as! SKSpriteNode


        planetX = planetNode.position.x
        planetY = planetNode.position.y

        playerX = playerShip.position.x
        playerY = playerShip.position.y


        var angleOfAnchor = AngleBetweenPoints(planetNode.position, endPoint: playerShip.position)
        var three60 = 360 * CGFloat(M_PI) / 180.0
        var nintey = 90 * CGFloat(M_PI) / 180.0
        var inDegree = angleOfAnchor * 180.0 / CGFloat(M_PI)

        var shipPlanetDistance = SDistanceBetweenPoints(planetNode.position, p2: playerShip.position)


        line = SKSpriteNode(color: UIColor.blackColor(), size: CGSize(width: 2, height: planetNode.size.height))
        line.anchorPoint = CGPoint(x: 0.5, y: 1)
        line.position = CGPoint(x: planetX, y: planetY)

        line.zRotation = -(three60 - nintey - angleOfAnchor)

        self.addChild(line)
        tempShip = playerShip
        playerShip.removeFromParent()
        line.runAction(repeatLine, withKey: "rotateLine")


        //playerShip.position = CGPoint(x: playerX, y: playerY)
        line.addChild(playerShip)
        playerShip.zRotation = (90 * CGFloat(M_PI) / 180.0)

        playerShip.runAction(repeatShip, withKey: "rotateShip")

        print("*PLANET POSITION* X: \(planetX) Y: \(planetY)  \r *PLAYER-SHIP POSITION* X: \(playerX) Y: \(playerY) \r *ANGLE OF LINE* RADIANS: \(angleOfAnchor) DEGRESS: \(inDegree) *PLAYER-SHIP ROTATION: \(playerShip.zRotation)")
    


屏幕触摸代码:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) 
    /* Called when a touch begins */

    print(" I'm Touched ")

    var radians:CGFloat = playerShip.zRotation
    var degrees = radians * 180.0 / CGFloat(M_PI)
    var dx = cos(radians)
    var dy = sin(radians)
    print(" ship angle in degrees: \(degrees) ")

    var tempAngle = playerShip.zRotation

    var shipPosition = convertPoint(playerShip.position, fromNode: line)

    playerX = shipPosition.x
    playerY = shipPosition.y



    if startMove == true 

        playerShip.removeActionForKey("rotateShip")

        playerShip.physicsBody?.velocity = CGVector(dx: 100*dx, dy: 100*dy)// speed of direction
        startMove = false

    

    if moving == true

        //playerShip.removeActionForKey("rotateShip")

        //playerShip.removeFromParent()
        //self.addChild(playerShip)


        var radians:CGFloat = playerShip.zRotation
        var degrees = radians * 180.0 / CGFloat(M_PI)
        var dx = cos(radians)
        var dy = sin(radians)
        print(" ship angle in degrees: \(degrees) ")



        //playerShip.zRotation = tempShip.zRotation
        //playerShip.position = CGPoint(x: playerX, y: playerY)
        //playerShip.physicsBody?.velocity = CGVector(dx: 100*dx, dy: 100*dy)// speed of direction


        // this is the ship that gets drawn
        var temp = SKSpriteNode(imageNamed: "img/ship/2.png")
        temp.position = CGPoint(x: playerX, y: playerY)
        temp.zRotation  = playerShip.zRotation
        self.addChild(temp)



        //moving = false
        print("*TEMP POS* X: \(temp.position.x) Y: \(temp.position.y) *TEMP ZROTATION*: \(temp.zRotation)")



    

    print("*PLAYER POSITION* X: \(playerX) Y: \(playerY) *PLAYER ZROTATION: \(playerShip.zRotation)")



【问题讨论】:

【参考方案1】:

我设法解决了这个问题,我在 playerShip.zrotation 中添加了 line.zrotation,它最终完美运行

【讨论】:

以上是关于(SWIFT) Sprite 在添加后没有给我正确的 zRotation 值的主要内容,如果未能解决你的问题,请参考以下文章

设置约束后 Swift 获取实际视图大小

sprite-kit swift字符选择菜单?

Swift Sprite Kit 应用内购买

如何将 2D Sprite 统一添加到材质中?

pod 安装后 Alamofire 出现错误

在 cocos2d 中停止 Sprite 动画