spritekit swift碰撞检测不起作用

时间:2015-05-23 20:30:17

标签: swift sprite-kit collision-detection

import SpriteKit
class Scene1: SKScene,
SKPhysicsContactDelegate {
    let collisionBulletCategory: UInt32 = 0x1 << 0
    let collisionHeroCategory: UInt32 = 0x1 << 1
    let spet = SKSpriteNode(imageNamed: "batdinger")
    let spets = SKSpriteNode(imageNamed: "batdinger")
    override func didMoveToView(view: SKView) {

        self.physicsWorld.contactDelegate = self
        let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        // 2. Set the friction of that physicsBody to 0
        borderBody.friction = 0
        // 3. Set physicsBody of scene to borderBody
        self.physicsBody = borderBody

        physicsWorld.gravity = CGVectorMake(-5, 0)
        spets.physicsBody ? = SKPhysicsBody(rectangleOfSize: spet.size)
        spets.physicsBody ? .categoryBitMask = UInt32(collisionHeroCategory)
        spets.physicsBody ? .dynamic = true
        spets.physicsBody ? .contactTestBitMask = UInt32(collisionHeroCategory)
        spets.physicsBody ? .collisionBitMask = 0x0
        spets.physicsBody ? .allowsRotation = false
        spets.physicsBody ? .affectedByGravity = false
        spets.physicsBody ? .friction = 0
        spets.physicsBody ? .restitution = 1
        spets.physicsBody ? .linearDamping = 0
        spets.physicsBody ? .angularDamping = 0
        spets.position = CGPointMake(300, 500)
        spet.physicsBody ? = SKPhysicsBody(rectangleOfSize: spet.size)
        spet.physicsBody ? .categoryBitMask = UInt32(collisionBulletCategory)
        spet.physicsBody ? .dynamic = true
        spet.physicsBody ? .contactTestBitMask = UInt32(collisionBulletCategory)
        spet.physicsBody ? .collisionBitMask = 0x0
        spet.physicsBody ? .allowsRotation = false
        spet.physicsBody ? .affectedByGravity = false
        spet.physicsBody ? .friction = 0
        spet.physicsBody ? .restitution = 1
        spet.physicsBody ? .linearDamping = 0
        spet.physicsBody ? .angularDamping = 0
        spet.position = CGPointMake(200, 300)
        spet.physicsBody ? .dynamic = true
        // spet.physicsBody = SKPhysicsBody(texture: heroSprite.texture, size:     heroSprite.size)
        spet.physicsBody ? .affectedByGravity = false
        spet.physicsBody ? .categoryBitMask = collisionHeroCategory
        spet.physicsBody ? .collisionBitMask = 0x0
        let poso = CGPointMake(400, 200)
        let action = SKAction.rotateByAngle(CGFloat(M_PI), duration: 1)
        //  let mover = SKAction.followPath(x: 150, y: 300, duration: 1.5)
        let mr = SKAction.moveTo(poso, duration: 2.0)
        spets.runAction(SKAction.repeatActionForever(mr))

        //spet.xScale = 0.5
        //spet.yScale = 0.5
        addChild(spet)
        addChild(spets)
        backgroundColor = UIColor.blueColor()
    }

    func didBeginContact(contact: SKPhysicsContact) {
        println("HI")
    }

    override func touchesBegan(touches: Set < NSObject > , withEvent event: UIEvent) {
        let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
        spet.position = CGPointMake(400, 200)
        let scene = GameScene(size: self.scene!.size)
        scene.scaleMode = SKSceneScaleMode.AspectFill

        //self.scene!.view!.presentScene(GameScene(fileNamed: "GameScene"), transition: transition)

    }
}

我已经搜索了谷歌和任何其他资源和教程几个小时,完全跟着它们,甚至将源代码从另一个项目粘贴到我的项目中,我的碰撞检测无法正常工作。

出了什么问题?我的代码错了吗?我没有打电话给&#39; didBeginContact&#39;功能正常吗?

1 个答案:

答案 0 :(得分:0)

您正在将联系人位掩码设置为自己而不是其他对象,请尝试以下更改:

    //Set the hero mask
    spets.physicsBody ? .categoryBitMask = UInt32(collisionHeroCategory)
    //Set the hero collision mask
    spets.physicsBody ? .contactTestBitMask = UInt32(collisionBulletCategory)

    //Set the bullet mask
    spet.physicsBody ? .categoryBitMask = UInt32(collisionBulletCategory)  
    //Set the bullet collision mask 
    spet.physicsBody ? .contactTestBitMask = UInt32(collisionHeroCategory)