我试图创建一个游戏,其中玩家旋转spritenode以使用正确的颜色击中右侧spritenode。我放置一个固定在主屏幕上的spritenode(node1),并在每个方向上固定4个spritenode。 (redPoint,bluePoint,greenPoint和brownPoint)。当玩家触摸node1精灵时,它应该旋转90度。颜色精灵应该保持锁定在node1上并向正确的方向移动。
几乎所有东西都应该工作,颜色精灵被固定在node1上的位置。然而,整个小组并没有像它应该的那样旋转90度。它旋转但不是90度。
如果我注释掉除node1之外的所有内容并测试触摸精灵它确实旋转了90度。
有人知道我可以做些什么来解决这个问题吗? 或者我做错了什么或有人有不同的方法?
我附上了我使用过的代码:
class GameScene: SKScene, SKPhysicsContactDelegate {
var node1 = SKSpriteNode()
var redPoint = SKSpriteNode()
var bluePoint = SKSpriteNode()
var greenPoint = SKSpriteNode()
var brownPoint = SKSpriteNode()
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
playerTri()
}
func playerTri(){
node1 = SKSpriteNode(color: .black, size: CGSize(width: self.size.width * 0.2, height: self.size.width * 0.2))
redPoint = SKSpriteNode(color: .red, size: CGSize(width: node1.size.width * 0.5, height: node.size.width * 0.5))
bluePoint = SKSpriteNode(color: .blue, size: CGSize(width: node1.size.width * 0.5, height: node1.size.width * 0.5))
greenPoint = SKSpriteNode(color: .green, size: CGSize(width: node1.size.width * 0.5, height: node1.size.width * 0.5))
brownPoint = SKSpriteNode(color: .brown, size: CGSize(width: node1.size.width * 0.5, height: node1.size.width * 0.5))
node1.position = CGPoint(x: self.frame.midX, y: self.frame.minY + node1.size.height)
node1.zPosition = 2
node1.physicsBody = SKPhysicsBody(rectangleOf: node1.size)
node1.physicsBody?.affectedByGravity = false
node1.physicsBody?.pinned = true
node1.zRotation = 0.0
redPoint.position = CGPoint(x: node1.frame.minX, y: node1.frame.midY)
redPoint.zPosition = 3
redPoint.anchorPoint = node1.anchorPoint
redPoint.physicsBody = SKPhysicsBody(rectangleOf: redPoint.size)
redPoint.physicsBody?.affectedByGravity = false
bluePoint.position = CGPoint(x: node1.frame.maxX, y: node1.frame.midY)
bluePoint.zPosition = 3
bluePoint.physicsBody = SKPhysicsBody(rectangleOf: bluePoint.size)
bluePoint.physicsBody?.affectedByGravity = false
greenPoint.position = CGPoint(x: node1.frame.midX, y: node1.frame.maxY)
greenPoint.zPosition = 3
greenPoint.physicsBody = SKPhysicsBody(rectangleOf: greenPoint.size)
greenPoint.physicsBody?.affectedByGravity = false
brownPoint.position = CGPoint(x: node1.frame.midX, y: node1.frame.minY)
brownPoint.zPosition = 3
brownPoint.physicsBody = SKPhysicsBody(rectangleOf: greenPoint.size)
brownPoint.physicsBody?.affectedByGravity = false
let joint = SKPhysicsJointFixed.joint(withBodyA: node1.physicsBody!, bodyB: redPoint.physicsBody!, anchor: CGPoint(x: node1.frame.minX, y: node1.frame.midY))
let joint2 = SKPhysicsJointFixed.joint(withBodyA: node1.physicsBody!, bodyB: bluePoint.physicsBody!, anchor: CGPoint(x: node1.frame.maxX, y: node1.frame.midY))
let joint3 = SKPhysicsJointFixed.joint(withBodyA: node1.physicsBody!, bodyB: greenPoint.physicsBody!, anchor: CGPoint(x: node1.frame.midX, y: node1.frame.maxY))
let joint4 = SKPhysicsJointFixed.joint(withBodyA: node1.physicsBody!, bodyB: brownPoint.physicsBody!, anchor: CGPoint(x: node1.frame.midX, y: node1.frame.minY))
addChild(node1)
addChild(redPoint)
addChild(bluePoint)
addChild(greenPoint)
addChild(brownPoint)
scene?.physicsWorld.add(joint)
scene?.physicsWorld.add(joint2)
scene?.physicsWorld.add(joint3)
scene?.physicsWorld.add(joint4)
}
func rotate(){
let rotate1 = SKAction.rotate(byAngle: .pi / 2, duration: 0.1)
node1.run(rotate1)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {return}
let location = touch.location(in: self)
let tappedNodes = nodes(at: location)
if tappedNodes.contains(node1){
rotate()
}
}
}