didbegincontact不叫spritekit

时间:2017-02-18 21:19:33

标签: sprite-kit swift3

我的代码是这样的

class GameScene: SKScene, SKPhysicsContactDelegate {
    var player: SKSpriteNode!
    var player2: SKSpriteNode!
    var initialplayerposition : CGPoint!

override func didMove(to view: SKView) {
    self.physicsWorld.gravity = CGVector(dx:0, dy:0)
    self.physicsWorld.contactDelegate = self
    addplayer()
    addRow(type: .threeS)
}    
func addRandomRow () {
    let randomNumber = Int(arc4random_uniform(6))
    switch randomNumber {
    case 0 :
        addRow(type: RowType(rawValue:0)!)
        break
    case 1:
        addRow (type: RowType(rawValue: 1)!)
    case 2 :
        addRow(type: RowType(rawValue:2)!)
        break
    case 3 :
        addRow(type: RowType(rawValue:3)!)
        break
    case 4 :
        addRow(type: RowType(rawValue:4)!)
        break
    case 5 :
        addRow(type: RowType(rawValue:5)!)
        break
    default:
        break

    }
}
var lastTimeIntervall = TimeInterval ()
var lastTimeYield = TimeInterval()

func updateWithTimeSinceLastUpdate (timeSinceLastUpdate:CFTimeInterval){
    lastTimeYield += timeSinceLastUpdate
    if (lastTimeYield > 0.6){
        lastTimeYield = 0
        addRandomRow()
    }
}
override func update(_ currentTime: TimeInterval) {
    var timeSinceLastUpdate = currentTime - lastTimeIntervall
    lastTimeIntervall = currentTime
    if timeSinceLastUpdate > 1 {
        timeSinceLastUpdate = 1 / 60
        lastTimeIntervall = currentTime

    }
    updateWithTimeSinceLastUpdate (timeSinceLastUpdate: timeSinceLastUpdate)
    // Called before each frame is rendered
}

 func didBeginContact(contact: SKPhysicsContact) {

    if contact.bodyA.node?.name == "Player" {
        print("gameover")
    }
}}

这是我在另一个文件中创建的元素

func addplayer()  {
    player = SKSpriteNode(color: UIColor.red, size:CGSize(width:50, height:50))
    player.position = CGPoint(x:self.size.width / 2 , y: 350)
    player.name = "PLAYER"
    player.physicsBody?.isDynamic = false
    player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
    player.physicsBody?.categoryBitMask = UInt32(CollisionBitMask.Player)
    player.physicsBody?.collisionBitMask =  0
    player.physicsBody?.contactTestBitMask = UInt32(CollisionBitMask.Obstacle)

这里是障碍代码

   func addObstacle(type:ObstacleType) ->  SKSpriteNode {

    let Obstacle = SKSpriteNode(color: UIColor.white, size: CGSize(width:0, height:30))
    Obstacle.name = "OBSTACLE"
    Obstacle.physicsBody?.isDynamic = true

    switch type {
    case .Small:
        Obstacle.size.width = self.size.width * 0.2
        break
    case.Medium:
        Obstacle.size.width = self.size.width * 0.35
        break
    case .Large:
        Obstacle.size.width = self.size.width * 0.75
        break
    }

    Obstacle.position = CGPoint(x:0, y : self.size.height + Obstacle.size.height)
    Obstacle.physicsBody = SKPhysicsBody(rectangleOf: Obstacle.size)
    Obstacle.physicsBody?.categoryBitMask = UInt32(CollisionBitMask.Obstacle)
    Obstacle.physicsBody?.collisionBitMask = 0


    return Obstacle}

1 个答案:

答案 0 :(得分:1)

这是我的一个愚蠢的错误,在swift3中didBegin函数应该是这样的

func didBegin(_ contact: SKPhysicsContact) {
    if (contact.bodyA.node?.name == "PLAYER"){
        print("gameover")
      }
}