didBeginContact无法在Swift 2 + SpriteKit中工作

时间:2015-12-25 09:13:49

标签: swift sprite-kit swift2 skspritenode

我正在开发游戏,我正在使用spritekit和Swift。

这是我的代码:

import SpriteKit

struct collision {
    static let arrow:UInt32 = 0x1 << 1
    static let runner:UInt32 = 0x1 << 2
    static let target:UInt32 = 0x1 << 3
    static let targetCenter:UInt32 = 0x1 << 4
}

class GameScene: SKScene, SKPhysicsContactDelegate {

    var person = SKSpriteNode()
    var box = SKSpriteNode()

    var screenSize:CGSize!
    var gameScreenSize:CGSize!

    var gameStarted:Bool = false

    var moveAndRemove = SKAction()

    var boxVelocity:NSTimeInterval = 5.5

    override func didMoveToView(view: SKView) {

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

        screenSize = self.frame.size
        gameScreenSize = view.frame.size

        createPerson()
    }

    func createPerson() -> Void {
        person.texture = SKTexture(imageNamed:"person")
        person.setScale(1.0)
        person.size = CGSize(width: 80, height: 80)
        person.position = CGPoint(x: screenSize.width / 2, y: 150)

        person.physicsBody = SKPhysicsBody(rectangleOfSize: person.size)
        person.physicsBody?.affectedByGravity = false
        person.physicsBody?.dynamic = false

        self.addChild(person)
    }

    func createTarget() -> Void {
        box = SKSpriteNode()

        box.size = CGSize(width: 70, height: 100)
        box.setScale(1.0)

        box.position = CGPoint(x: (screenSize.width / 3) * 2, y: screenSize.height + box.size.height)
        box.texture = SKTexture(imageNamed: "box")

        box.physicsBody? = SKPhysicsBody(rectangleOfSize: box.size)
        box.physicsBody?.categoryBitMask = collision.target
        box.physicsBody?.collisionBitMask = collision.arrow
        box.physicsBody?.contactTestBitMask = collision.targetCenter
        box.physicsBody?.affectedByGravity = false
        box.physicsBody?.dynamic = true
        box.physicsBody?.usesPreciseCollisionDetection = true

        self.addChild(box)

        let distance = CGFloat(self.frame.height - box.frame.height)
        let moveTargets = SKAction.moveToY(-distance, duration: boxVelocity)
        let removeTargets = SKAction.removeFromParent()

        moveAndRemove = SKAction.sequence([moveTargets,removeTargets])
        box.runAction(moveAndRemove)
    }

    func createBall() ->Void {
        let ball = SKSpriteNode()
        ball.size = CGSize(width: 20, height: 22)
        ball.zPosition = 5

        let moveToXY = CGPoint(x: self.size.width, y: self.size.height)

        ball.texture = SKTexture(imageNamed: "ball")
        ball.position = CGPointMake(person.position.x + ball.size.width, person.position.y + ball.size.height)

        ball.physicsBody? = SKPhysicsBody(rectangleOfSize: ball.size)
        ball.physicsBody?.categoryBitMask = collision.arrow
        ball.physicsBody?.collisionBitMask = collision.target
        ball.physicsBody?.affectedByGravity = false
        ball.physicsBody?.dynamic = true
        ball.physicsBody?.usesPreciseCollisionDetection = true

        let action = SKAction.moveTo(moveToXY, duration: 1.5)
        let delay = SKAction.waitForDuration(1.5)
        ball.runAction(SKAction.sequence([action,delay]))

        self.addChild(ball)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if gameStarted == false {
            gameStarted = true

            let spawn = SKAction.runBlock { () in
                self.createTarget()
            }
            let delay = SKAction.waitForDuration(1.5)
            let spawnDelay = SKAction.sequence([spawn, delay])
            let spanDelayForever = SKAction.repeatActionForever(spawnDelay)
            self.runAction(spanDelayForever)
        } else {
            createBall()
            boxVelocity -= 0.1
        }
    }

    func didBeginContact(contact: SKPhysicsContact) {
        print("Detect")
    }

    func didEndContact(contact: SKPhysicsContact) {
        print("end detect")
    }

    override func update(currentTime: CFTimeInterval) {

    }
}

但是当我运行游戏时,对象之间的碰撞不会。我想解决一段时间,但一无所获。有人能帮助我吗?

Project files

1 个答案:

答案 0 :(得分:2)

尝试修改这些:

 box.physicsBody = SKPhysicsBody(rectangleOfSize: box.size)
 box.physicsBody?.categoryBitMask = collision.target
 box.physicsBody?.collisionBitMask = collision.arrow
 box.physicsBody?.contactTestBitMask = collision.targetCenter

ball.physicsBody = SKPhysicsBody(rectangleOfSize: ball.size)
ball.physicsBody?.categoryBitMask = collision.arrow
ball.physicsBody?.collisionBitMask = collision.target
ball.physicsBody?.contactTestBitMask = collision.target

注意没有“?”在您初始化physicsBody和新contactTestBitMask