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

时间:2018-06-10 10:48:25

标签: swift sprite-kit skphysicsbody gameplay-kit

我用SpriteKit制作了一个游戏,不同的砖块落在山上。当这些物体掉落时,它们通常会从山上反弹并改变它们的对齐方式(它们会旋转几次)。如果我转换到GameOver场景并按重放(回到GameScene),物理体仍然像我离开场景时那样对齐。但我希望它们像开头一样进行水平对齐。

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)

不知何故,当我转换场景时,砖块如何对齐的信息会被保存。我怎么能改变它?

1 个答案:

答案 0 :(得分:0)

如果您在重新启动游戏后想要,那么砖块节点将返回开始旋转而不是调用:

brick.zRotation = 0

如果你想在游戏中节点不旋转,你可以把这个代码放在已经存在于所有spritekit文件中的更新函数中:

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

希望这有帮助