如何在 SpriteKit 中重置物理体的对齐方式?

How to reset the alignment of physics bodies in SpriteKit?

我用 SpriteKit 做了一个游戏,不同的砖块从山上掉下来。当这些身体倒下时,它们通常会从山上弹起并改变它们的排列方式(它们会旋转几次)。如果我转换到 GameOver 场景并按重播(回到 GameScene),物理体仍然像我离开场景时一样对齐。但我希望它们像开始时一样水平对齐。

游戏场景:

import SpriteKit
import GameplayKit

 let hillTexture = SKTexture(imageNamed: "HillIllustration")
 let hillIllustration = SKSpriteNode(texture: hillTexture)

 let brickTexture = SKTexture(imageNamed: "BrickIllustration")
 let brick = SKSpriteNode(texture: brickTexture)

 class GameScene: SKScene {

    //Hill

    hillIllustration.setScale(0.7)
    hillIllustration.position = CGPoint(x: self.size.width / 2, y: self.size.height * 0.16)
    hillIllustration.zPosition = 2
    hillIllustration.physicsBody = SKPhysicsBody(polygonFrom: clipPath)
    hillIllustration.physicsBody?.isDynamic = false
    hillIllustration.physicsBody?.categoryBitMask = CollisionBitMask.Hill
    hillIllustration.physicsBody?.affectedByGravity = false
    self.addChild(hillIllustration)

    //The brick is a child of the hill node

    brick.setScale(1)
    brick.position = CGPoint(x: -350, y: self.size.height * 0.5)
    brick.zPosition = 1
    brick.physicsBody = SKPhysicsBody(polygonFrom: clipPath2)
    brick.physicsBody?.isDynamic = true
    brick.physicsBody?.categoryBitMask = CollisionBitMask.Brick
    brick.physicsBody?.affectedByGravity = true
    hillIllustration.addChild(brick)

}

过渡到 GameOver:

let transition = SKTransition.crossFade(withDuration: 0)
let gameScene = GameOver(size: self.size)
self.view?.presentScene(gameScene, transition: transition)

转换回 GameScene:

let transition = SKTransition.crossFade(withDuration: 0)
    let gameScene = GameScene(size: self.size)
    self.view?.presentScene(gameScene, transition: transition)

不知何故,当我转换场景时,砖块如何对齐的信息被保存了。我该如何更改?

如果您希望在重新启动游戏后砖块节点返回开始旋转而不是调用:

brick.zRotation = 0

如果您希望节点在游戏过程中不旋转,您可以将此代码放入所有 spritekit 文件中已有的更新函数中:

override func update(_ currentTime: TimeInterval) {
 brick.zRotation = 0
}

希望对您有所帮助