添加/删除父级

时间:2015-08-30 05:10:00

标签: swift sprite-kit position skspritenode addchild

计划详情: 通过观察这张照片(http://i.stack.imgur.com/QOZ53.png),我想要做的就是让宇宙飞船环绕地球。我通过制作一个线节点并将其锚点更改为顶部然后将其定位到行星的中心来实现这一点,然后在太空船和行星撞击时,该线朝向船的角度,然后船被移除从视图中添加到线节点并一起围绕行星旋转。 (希望这很有意义)

问题: 问题是在将船舶添加到线路节点后,宇宙飞船的坐标变得奇怪,我不确定发生了什么但是如果你看一下控制台日志,x位置和y位置完全关闭。第一个" 我已触及 "在开始时激活宇宙飞船,船的最初起点是[100,100],一旦它与行星接触," WE' RE TOUCHING & #34;被激活并且船的位置是合理的[116.000015258789,162.0]。然后船开始绕行星,但是当我打电话给" 我触摸了 "要获得有关船舶位置的更多信息,请完全关闭[4.1961669921875e-05,-59.0515747070312]。此时我不知道发生了什么,有关更多信息,您可以查看代码和控制台日志。

Cosnole日志:

我感动了  船角度:72.9146667862848  球员位置 X:100.0 Y:100.0  *球员轮换:1.27260100841522

WE' RE TOUCHING  行星位置 X:116.000015258789 Y:162.0
 球员位置 X:101.464706420898 Y:104.765426635742  处理角度 RADIANS:-1.81949883426405 DEGRESS:-104.249604032303  *球员轮换:1.27260100841522

我感动了  船角度:10.6651491436781  球员位置 X:7.33137130737305e-05 Y:-59.0515937805176  *球员轮换:0.186141967773438

我感动了  船角度:10.6651491436781  球员位置 X:4.1961669921875e-05 Y:-59.0515747070312  *球员轮换:0.186141967773438

我感动了  船角度:10.6651491436781  球员位置 X:3.4332275390625e-05 Y:-59.0515594482422  *球员轮换:0.186141967773438

我感动了  船角度:10.6651491436781  球员位置 X:2.6702880859375e-05 Y:-59.0515365600586  *球员轮换:0.186141967773438

我感动了  船角度:10.6651491436781  球员位置 X:3.0517578125e-05 Y:-59.0515327453613  *球员轮换:0.186141967773438

这是碰撞代码:

  func didBeginContact(contact: SKPhysicsContact) {

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

         print(" **WE'RE TOUCHING** ")

        moving = false
        touching = true

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

        var rotate = SKAction.rotateByAngle(CGFloat(radians), duration: 0.5)
        var repeat = SKAction.repeatActionForever(rotate)
        playerShip.runAction(repeat, withKey: "rotate")

        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)

        // this sets the angle of the line when the ship and planet collide
        line.zRotation = -(three60 - nintey - angleOfAnchor)

        self.addChild(line)
        playerShip.removeFromParent()
        angle.runAction(repeat, withKey: "rotate")

        line.addChild(playerShip)

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

}

屏幕触摸代码:

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

    print(" I'm Touched ")

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


        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.removeFromParent()
        //self.addChild(playerShip)

        //playerShip.position = CGPoint(x: playerX, y: playerY)


       // playerShip.physicsBody?.angularVelocity = 0 // forgot what this does
        playerShip.removeActionForKey("rotate")

    if moving == true {
        playerShip.physicsBody?.velocity = CGVector(dx: 100*dx, dy: 100*dy)// speed of direction
    }

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




}

0 个答案:

没有答案