(SWIFT) 添加后 Sprite 没有给我正确的 zRotation 值
(SWIFT) Sprite not giving me the correct zRotation value after being added
程序详情:看这张图(http://i.stack.imgur.com/QOZ53.png),我要做的就是让宇宙飞船绕地球一周。我通过制作一个线节点并将其锚点更改为顶部然后将其定位到行星的中心来实现这一点,然后在宇宙飞船和行星的影响下,线与船成一定角度,然后船被移除从视图中作为子节点添加到线节点,它们一起绕着地球旋转,当船绕着地球旋转时,它自己的船也在原地旋转(希望这是有意义的)
问题:问题是我没有得到正确的飞船 zRotation 值。现在我得到了代码,可以在它被点击的位置绘制另一艘船,并将该图像的 zRotation 设置为船的 zRotation,但我不断得到不同的角度。 (参考图片)。这是因为我小时候已经将 ship 添加到 line 节点了吗?我运行 一条线和船上的旋转动画。图中,直线绕着行星逆时针旋转,拖着船一起旋转,船本身也在原地逆时针旋转。图中左边的船;接触线的是旋转的,右边的船只是绘制到旋转船的角度但是通过查看图片你可以看到船的角度与左边的完全相反。为什么会这样?我注意到当船在地球的下半部分时,角度很好但是当它到达上半部分时,角度有点相反(参考图片)
图片
控制台日志:
我很感动-
舰船角度:83.6418545381942
玩家位置 X: 100.0 Y: 100.0
玩家旋转: 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
温度旋转:5.491868019104
玩家位置 X: 136.535125732422 Y: 287.094879150391
玩家旋转: 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)")
}
我设法解决了这个问题,我添加了 line.zrotation 和 playerShip.zrotation,它最终完美地工作了
程序详情:看这张图(http://i.stack.imgur.com/QOZ53.png),我要做的就是让宇宙飞船绕地球一周。我通过制作一个线节点并将其锚点更改为顶部然后将其定位到行星的中心来实现这一点,然后在宇宙飞船和行星的影响下,线与船成一定角度,然后船被移除从视图中作为子节点添加到线节点,它们一起绕着地球旋转,当船绕着地球旋转时,它自己的船也在原地旋转(希望这是有意义的)
问题:问题是我没有得到正确的飞船 zRotation 值。现在我得到了代码,可以在它被点击的位置绘制另一艘船,并将该图像的 zRotation 设置为船的 zRotation,但我不断得到不同的角度。 (参考图片)。这是因为我小时候已经将 ship 添加到 line 节点了吗?我运行 一条线和船上的旋转动画。图中,直线绕着行星逆时针旋转,拖着船一起旋转,船本身也在原地逆时针旋转。图中左边的船;接触线的是旋转的,右边的船只是绘制到旋转船的角度但是通过查看图片你可以看到船的角度与左边的完全相反。为什么会这样?我注意到当船在地球的下半部分时,角度很好但是当它到达上半部分时,角度有点相反(参考图片)
图片
控制台日志:
我很感动- 舰船角度:83.6418545381942 玩家位置 X: 100.0 Y: 100.0 玩家旋转: 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 温度旋转:5.491868019104 玩家位置 X: 136.535125732422 Y: 287.094879150391 玩家旋转: 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)")
}
我设法解决了这个问题,我添加了 line.zrotation 和 playerShip.zrotation,它最终完美地工作了