(SWIFT)Sprite在添加

时间:2015-09-01 08:42:43

标签: swift rotation sprite-kit angle skspritenode

计划详情:通过查看此图片(http://i.stack.imgur.com/QOZ53.png),我想要做的就是拥有宇宙飞船圈子。我通过制作一个线节点并将其锚点更改为顶部然后将其定位到行星的中心来实现这一点,然后在太空船和行星撞击时,该线朝向船的角度,然后船被移除从视图中看并作为孩子一起添加到线节点,它们一起围绕行星旋转,当船围绕行星旋转时,它自己的船也在现场旋转(希望这是有意义的)

问题:问题是我没有获得正确的船舶zRotation值。现在我得到的代码是在其被轻敲的位置绘制另一艘船并将该图像的zRotation设置为船的zRotation但我不断获得不同的角度。 (参见图片)。这是因为我作为一个孩子将船舶添加到线路节点?我在线路和船上运行旋转动画。在图片中,线绕着行星逆时针旋转,随着船一起拖着船,船本身也在现场逆时针旋转。在图片左边的船;接触线的那个是旋转的那个,右边的船只被拉到旋转船的角度但是通过观察图片你可以看到船的角度与那个相反。在左边。为什么会这样?我注意到的是,当船在行星的下半部分时,角度很好但是当它到达上半部分时,角度有点相反(参见图片)

图片

enter image description here

控制台日志:

我感动了 船舶角度(度):83.6418545381942 球员位置X: 100.0 Y :100.0 PLAYER ZROTATION: 1.45982575416565

WE' RE TOUCHING - 行星位置 X *:120.0 Y :230.000015258789
PLAYER-SHIP POSITION X :107.998710632324 Y :171.783294677734 线的角度 RADIANS *: - 1.78409685090117 DEGRESS : - 101.648262004087 PLAYER-SHIP ROTATION :1.57079637050629

我感动 - 船舶角度(度):314.660859137531 TEMP POS X :136.535125732422 Y :287.094879150391 TEMP ZROTATION :5.491868019104 球员位置X :136.535125732422 Y :287.094879150391 PLAYER 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 个答案:

答案 0 :(得分:0)

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