当我的角色遇到界限时,如何让我的游戏结束?

时间:2017-07-24 07:36:14

标签: ios swift xcode

我有一个带有SKSprite节点的游戏,这是我的角色“骑士”,我试图这样做,如果他击中屏幕的任何边界,游戏结束。我已经拥有了它,所以如果他接触到了游戏结束的地面,但是如果他触及任何边界,我希望游戏结束,而不是完全确定我做错了什么。请帮忙!

struct PhysicsCatagory {
static let Knight : UInt32 = 0x1 << 1
static let border : UInt32 = 0x1 << 5
}

var Knight = SKSpritenode()

override functions didMove(to view: SKView) {
Knight = SKSpriteNode(imageNamed: "CJKnightNew")
    Knight.size = CGSize(width: 100, height: 120)
    Knight.position = CGPoint(x: -10, y: 200)
    Knight.zPosition = 7

    Knight.physicsBody = SKPhysicsBody(circleOfRadius: Knight.frame.height / 2)
    Knight.physicsBody?.categoryBitMask = PhysicsCatagory.Knight
    Knight.physicsBody?.collisionBitMask = PhysicsCatagory.land
    Knight.physicsBody?.contactTestBitMask = PhysicsCatagory.land | PhysicsCatagory.Score | PhysicsCatagory.Ground
    Knight.physicsBody?.affectedByGravity = false
    Knight.physicsBody?.isDynamic = true
    Knight.physicsBody?.allowsRotation = false

let border = SKPhysicsBody(edgeLoopFrom: self.frame)
border.friction = 0
self.physicsBody = border
}

func didBegin(_ contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB

if firstBody.categoryBitMask == PhysicsCatagory.Knight && secondBody.categoryBitMask == PhysicsCatagory.border || firstBody.categoryBitMask == PhysicsCatagory.border && secondBody.categoryBitMask == PhysicsCatagory.Knight {

        died = true
    }
}

我有它,所以如果死亡=真的它结束游戏,但由于某种原因,这是行不通的?当骑士击中边界时,它应该使游戏结束。我哪里错了?

0 个答案:

没有答案