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

时间:2022-06-27 16:53:09

Details of program: by looking at this picture(http://i.stack.imgur.com/QOZ53.png), what I'm trying to do is have the spaceship circle the planet. I achieved this by making a line node and changes its anchor point to the top and then position it to the centre of the planet which then upon impact of the spaceship and planet, the line is angled towards the ship in which then the ship is removed from the view and added to the line node as a child and together they rotate around the planet, as the ship rotates around the the planet, the ship it self also rotates on the spot (hopefully that makes sense)

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

Problem: the problem is im not getting the correct zRotation value of the ship. Right now I got the code to draw another ship at the location where it was tapped and set the zRotation of that image to the zRotation of the ship but i keep getting different angles. (refer to picture). Is this because I have ship added to line node as a child? Im running a rotating animation on both the line and the ship. In the picture, the line is rotating around the planet counter clockwise dragging the ship along with it and the ship itself is also rotating counter clockwise on the spot. In the picture the left ship; the one that's touching the line is the one thats rotating, the ship on the right is just drawn to angle of the rotating ship but by looking at the picture you can see the angle of the ship is pretty opposite of the one on the left. Why is this so? what I noticed is that when the ship is at the bottom half of the planet, the angles are fine but when it comes to the top half, the angles are kinda opposite(refer to picture)

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

Picture

图片

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

Console Log:

控制台日志:

I'm Touched - ship angle in degrees: 83.6418545381942 PLAYER POSITION X: 100.0 Y: 100.0 PLAYER ZROTATION: 1.45982575416565

我感动了 - 船角度:83.6418545381942球员位置X:100.0 Y:100.0球员自杀:1.45982575416565

WE'RE TOUCHING - PLANET POSITION X*: 120.0 Y: 230.000015258789
PLAYER-SHIP POSITION X: 107.998710632324 Y: 171.783294677734 ANGLE OF LINE RADIANS*: -1.77409685090117 DEGRESS: -101.648262004087 PLAYER-SHIP ROTATION: 1.57079637050629

我们接触 - 行星位置X *:120.0 Y:230.000015258789玩家船位置X:107.998710632324 Y:171.783294677734线辐射角*: - 1.78409685090117度数:-101.648262004087玩家 - 船只旋转:1.57079637050629

I'm Touched - ship angle in degrees: 314.660859137531 TEMP POS X: 136.535125732422 Y: 287.094879150391 TEMP ZROTATION: 5.491868019104 PLAYER POSITION X: 136.535125732422 Y: 287.094879150391 PLAYER ZROTATION: 5.491868019104

我感动了 - 船角度:314.660859137531 TEMP POS X:136.535125732422 Y:287.094879150391 TEMP ZROTATION:5.491868019104 PLAYER POSITION X:136.535125732422 Y:287.094879150391 PLAYER ZROTATION:5.491868019104

Collision Code:

碰撞代码:

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)")
    }

}

Screen Touch Code:

屏幕触摸代码:

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 个解决方案

#1


0  

I managed to solve this problem, I added the line.zrotation with the playerShip.zrotation and it ended up working perfectly

我设法解决了这个问题,我在player.hip.zrotation中添加了line.zrotation并且它最终完美地工作了

#1


0  

I managed to solve this problem, I added the line.zrotation with the playerShip.zrotation and it ended up working perfectly

我设法解决了这个问题,我在player.hip.zrotation中添加了line.zrotation并且它最终完美地工作了