所以我已经按照我想要的方式进行了一段时间的游戏开发,但是现在我被困在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)


任何有任何东西的人都请帮忙,如果您需要更多代码片段,请告诉我,我们将提供更多信息。
提前致谢。

最佳答案

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

首先,您必须插入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
    }
}

关于ios - didBeginContact没有被调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38309228/

10-10 20:49