didBeginContact未被调用

时间:2016-07-11 14:16:26

标签: ios iphone swift sprite-kit swift2

所以我一直在这个游戏上工作一段时间都按照我想要的方式工作,但是我已经被困在didBeginContact上了几天而且我无法绕过它,我想它看起来没问题,我已经检查了几个论坛,并且几乎完全相同,所以它适用于我的游戏。但仍然没有:(任何帮助将不胜感激。 所以这是我的didBeginContact:

func didBeginContact(contact: SKPhysicsContact) {
    print("i worked")
    // Every contact has two bodies, we do not know which body is which.
    // We will find which body is the penguin, then use the other body
    // to determine what type of contact is happening.
    let otherBody:SKPhysicsBody
    // Combine the two penguin physics categories into one mask
    // using the bitwise OR operator |
    let bunnyMask = PhysicsCategory.bunny.rawValue | PhysicsCategory.bunnyDamaged.rawValue
    // Use the bitwise AND operator & to find the penguin.
    // This returns a positive number if bodyA’s category is
    // the same as either the penguin or damagedPenguin:
    if (contact.bodyA.categoryBitMask & bunnyMask) > 0 {
        // bodyA is the penguin, we want to find out what bodyB is:
        otherBody = contact.bodyB
    }
    else {
        // bodyB is the penguin, we will test against bodyA:
        otherBody = contact.bodyA
    }
    // Find the contact type:
    switch otherBody.categoryBitMask {
    case PhysicsCategory.grass.rawValue:
        print("nom nom grass")
    case PhysicsCategory.mud.rawValue:
        print("mud hit")
    case PhysicsCategory.stone.rawValue:
        print("ow my head")
    case PhysicsCategory.lava.rawValue:
        print("it burns")
    case PhysicsCategory.hedgehog.rawValue:
        print("stop touching me!")
    default:
        print("Contact with no game logic")
    }
}

这是兔子的代码:

class Bunny : SKSpriteNode, GameSprite{
var currentSpeed: Double = 5

var textureAtlas: SKTextureAtlas = SKTextureAtlas(named: "bunny.atlas")
var moveAnimation = SKAction()
func spawn(parentNode: SKNode, position: CGPoint, size: CGSize = CGSize(width: 25, height: 25)) {
    parentNode.addChild(self)
    createAnimations()
    self.size = size
    self.position = position
    self.runAction(moveAnimation, withKey: "moveAnimation")
    self.physicsBody = SKPhysicsBody(rectangleOfSize: self.size)
    self.physicsBody?.categoryBitMask = PhysicsCategory.bunny.rawValue
    self.physicsBody?.contactTestBitMask = PhysicsCategory.hedgehog.rawValue
    self.physicsBody?.collisionBitMask = PhysicsCategory.hedgehog.rawValue
    self.physicsBody?.affectedByGravity = false
    self.physicsBody?.dynamic = true

}

兔子的GameScene代码:

initialBunnyPosition = CGPoint(x: 25, y: self.size.height)
    bunny.zPosition = 1000
    bunny.spawn(world, position: initialBunnyPosition)

任何有任何帮助的人,如果您需要更多代码剪,请告诉我,我会提供更多信息。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

当您将physicsBodies设置为项目时,您还可以定义角色将要播放和碰撞的边界。

首先,您必须插入SKPhysicsContactDelegate以使用didBeginContact方法并将委托设置如下。

这只是一个可以开始的例子:

class GameScene: SKScene, SKPhysicsContactDelegate { 
    override func didMoveToView(view: SKView) {

        self.physicsWorld.gravity = CGVectorMake(0, -6) //gravity
        self.physicsWorld.contactDelegate = self  //

        // Set physics boundaries
        let fieldBody = SKPhysicsBody.init(edgeLoopFromRect: self.frame)
        self.physicsBody = fieldBody
        self.physicsBody!.affectedByGravity = false
        self.physicsBody!.usesPreciseCollisionDetection = true
        self.physicsBody!.dynamic = true
        self.physicsBody!.mass = 0.8
        self.physicsBody!.friction = 0
        self.physicsBody!.linearDamping = 0
        self.physicsBody!.angularDamping = 0
        self.physicsBody!.restitution = 0
        self.physicsBody!.categoryBitMask = PhysicsCategory.field.rawValue
        self.physicsBody!.contactTestBitMask = PhysicsCategory.hedgehog.rawValue | PhysicsCategory.bunny.rawValue
        //self.physicsBody!.collisionBitMask = 0
    }
}

答案 1 :(得分:0)

康纳尔 - 你说你的dBC现在被召唤了。你有这个工作吗?如果没有,请为didBeginContact尝试此操作,只是为了确定它是否识别出正确的碰撞:

func didBeginContact(contact: SKPhysicsContact) {

    print("i worked")
      let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

       switch contactMask

       case PhysicsCategory.bunny.rawValue | PhysicsCategory.grass.rawValue:
        print("nom nom grass")
    case PhysicsCategory.bunny.rawValue | PhysicsCategory.mud.rawValue:
        print("mud hit")
    case PhysicsCategory.bunny.rawValue | PhysicsCategory.stone.rawValue:
        print("ow my head")
    case PhysicsCategory.bunny.rawValue | PhysicsCategory.lava.rawValue:
        print("it burns")
    case PhysicsCategory.bunny.rawValue | PhysicsCategory.hedgehog.rawValue:
        print("stop touching me!")
    default:
        print("Contact with no game logic")
    }
}

它没有为PhysicsCategory.bunnyDamaged.rawValue做任何事情,但如果我们能够让基本检测有效,我们可以添加它。