2015-12-25 114 views
1

我正在开发一款游戏,而我正在使用spritekit和Swift。didBeginContact不能在Swift 2中工作+ SpriteKit

这是我的代码:

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

回答

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