程序详细信息:通过查看这张图片(http://i.stack.imgur.com/QOZ53.png),我想做的是让飞船绕地球旋转。我通过制作线节点并将其锚点更改为顶部,然后将其定位到行星的中心来实现这一点,然后在飞船和行星的撞击下,该线与船成一定角度,然后将船移出从视图中添加并作为子节点添加到线节点,它们一起绕行星旋转,因为船绕行星旋转,它本身的船也当场旋转(希望这有意义)
问题:问题是我没有获得正确的zRotation值。现在,我得到了在其被轻拍的位置绘制另一艘船的代码,并将该图像的zRotation设置为船的zRotation,但是我一直保持不同的角度。 (参考图片)。这是因为我已经将儿童船运添加到了线路节点上吗?我在线路和船上都运行旋转动画。在图中,直线绕行星逆时针旋转,从而将船与船一起拖曳,并且船本身也在当场逆时针旋转。图片中的左船;接触线的是旋转的,右侧的船只是与旋转的船的角度绘制在一起,但是通过查看图片,您可以看到船的角度与左侧的船完全相反。为什么会这样呢?我注意到的是,当飞船在地球的下半部分时,角度很好,但在上半部分时,角度有点相反(请参阅图片)
图片
控制台日志:
我感动-
船用度数: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
温度POS X:136.535125732422 Y:287.094879150391
TEMP ZROTATION: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)")
}
最佳答案
我设法解决了这个问题,我在playerShip.zrotation中添加了line.zrotation,最终效果很好
关于swift - (SWIFT)Sprite在添加后没有给我正确的zRotation值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32327187/