我为每个我想要交互的不同对象创建了一个结构物理特性

struct PhysicsCatagory {
    static let Blade : UInt32 = 1
    static let Laser : UInt32 = 2

}
在我的课堂游戏场景之上
 class GameScene: SKScene, SKPhysicsContactDelegate {

我用didmovetoView方法初始化了一个skspritenode刀片的物理体。
override func didMoveToView(view: SKView) {
    physicsWorld.contactDelegate = self
    Blade.position = CGPointMake(self.size.width / 2, (self.size.height / 14))
    Blade.anchorPoint = CGPointMake(0.5, -0.13)
    Blade.physicsBody = SKPhysicsBody(rectangleOfSize: Blade.size)
    Blade.physicsBody?.categoryBitMask = PhysicsCatagory.Blade
    Blade.physicsBody?.contactTestBitMask = PhysicsCatagory.Laser
    Blade.physicsBody?.dynamic = false

    self.addChild(Blade)
}

以及一种skspritenode激光器及其物理体在方法射束激光器中的应用
func shootLaser(){
    var Laser = SKSpriteNode(imageNamed: "Laser.png")
    Laser.position = Enemy.position
    Laser.zPosition = -5
    Laser.physicsBody = SKPhysicsBody(rectangleOfSize: Laser.size)
    Laser.physicsBody?.categoryBitMask = PhysicsCatagory.Laser
    Laser.physicsBody?.contactTestBitMask = PhysicsCatagory.Blade
    Laser.physicsBody?.dynamic = false

    let action = SKAction.moveBy(laserVector, duration: 0.7)
    let actionDone = SKAction.removeFromParent()
    Laser.runAction(SKAction.sequence([action,actionDone]))
    self.addChild(Laser)
}

但是,当它们在模拟中发生碰撞时,不会调用didbegincontact方法,也不会打印“hello”。
func didBeginContact(contact: SKPhysicsContact) {
NSLog("Hello")
}

为什么当didbegincontact方法发生冲突时不被调用?提前感谢(:

最佳答案

Sprite工具包不会检查非动态物理体之间的接触,因为它们不会移动。如果你不想让你的精灵因为重力而从屏幕上掉下来,那么把物理体的属性设置为affectedByGravity并设置为false

关于swift - didBeginContact无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31388144/

10-12 22:42