2016-11-27 20 views
2

我遵循了很多不同的碰撞教程,並創建了我自己的遊戲,我想要聲明硬幣和玩家之間的碰撞。但是,在執行衝突代碼之後,我的兩個節點沒有對衝突做出響應......請有人幫我解決這個問題嗎?我的節點沒有碰撞,我在代碼中丟失了什麼?

import SpriteKit 
import GameplayKit 

// Collision categories 

enum ColliderType: UInt32 { 
case playerCase = 1 
case coinCase = 2 
case borderCase = 3 
} 

class GameScene: SKScene, SKPhysicsContactDelegate { 

let player = SKSpriteNode(imageNamed:"block") 
let buttonDirLeft = SKSpriteNode(imageNamed: "left") 
let buttonDirRight = SKSpriteNode(imageNamed: "right") 
let coins = SKSpriteNode(imageNamed: "coins") 
let background = SKSpriteNode(imageNamed: "background") 
var pressedButtons = [SKSpriteNode]() 

override func didMove(to view: SKView) { 

self.physicsWorld.contactDelegate = self 

//score label 
let points = SKLabelNode(text: "0") 
points.position = CGPoint(x: 530, y: 260) 
points.zPosition = 6 
points.fontColor = UIColor.black 
points.fontSize = 50 
addChild(points) 


//Set Background 
background.zPosition = 1 
background.position = CGPoint(x: frame.size.width/2, y: frame.size.height/2) 
background.size.width = 580 
background.size.height = 320 

addChild(background) 

// Player 
player.position = CGPoint(x: 250, y: 40) 
player.zPosition = 2 
player.texture?.filteringMode = .nearest 
// player!.collisionBitMask = 0 // 
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.height/2.0) 
player.physicsBody?.isDynamic = false 
player.physicsBody?.allowsRotation = false 
player.physicsBody?.affectedByGravity = false 
player.physicsBody!.categoryBitMask = ColliderType.playerCase.rawValue 
player.physicsBody!.contactTestBitMask = ColliderType.coinCase.rawValue 
player.physicsBody!.collisionBitMask = ColliderType.coinCase.rawValue 

self.addChild(player) 

// button left 
buttonDirLeft.position = CGPoint(x: 30, y: 35) 
buttonDirLeft.zPosition = 4 
buttonDirLeft.size.width = 270 
buttonDirLeft.size.height = 320 
buttonDirLeft.alpha = 0.0 
self.addChild(buttonDirLeft) 

// button right 
buttonDirRight.position = CGPoint(x: 530, y: 35) 
buttonDirRight.zPosition = 4 
buttonDirRight.size.width = 270 
buttonDirRight.size.height = 320 
buttonDirRight.alpha = 0.0 
self.addChild(buttonDirRight) 


// setting border around game 
let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame) 
borderBody.friction = 0 
self.physicsBody = borderBody 

//ENEMY SETTINGS START 

//repeat coing spawning 
run(SKAction.repeatForever(
    SKAction.sequence([ 
     SKAction.run(spawnCoins), 
     SKAction.wait(forDuration: 1.0)]))) 


} 
//coin settings 
func random() -> CGFloat { 
    return CGFloat(Float(arc4random())/0xFFFFFFFF) 
} 

func random(min: CGFloat, max: CGFloat) -> CGFloat { 
    return random() * (max - min) + min 
} 

//spawn coins 
func spawnCoins() { 
    // 2 
    let coins = SKSpriteNode(imageNamed: "coins") 
    coins.zPosition = 2 
    coins.size.width = 40 
    coins.size.height = 40 

    let action = SKAction.moveTo(y: -350, duration: TimeInterval(random(min: 1, max: 5))) 

    let remove = SKAction.run({coins.removeFromParent(); print("coins removed from scene")}) 

    let sequence = SKAction.sequence([action,remove]) 

    coins.physicsBody = SKPhysicsBody(rectangleOf: coins.size) 
    coins.physicsBody?.isDynamic = false 
    coins.physicsBody!.affectedByGravity = false 
    coins.physicsBody!.categoryBitMask = ColliderType.coinCase.rawValue 
    coins.physicsBody!.contactTestBitMask = ColliderType.playerCase.rawValue 
    coins.physicsBody!.collisionBitMask = ColliderType.playerCase.rawValue 

    coins.run(sequence) 


    coins.size.width = 20 
    coins.size.height = 20 
    coins.name = "coins" 
// coins.physicsBody!.collisionBitMask = 0 
    coins.position = CGPoint(x: frame.size.width * random(min: 0, max: 1), y: frame.size.height + coins.size.height/2) 
    addChild(coins) 

} 


override func update(_ currentTime: TimeInterval) { 
    // Called before each frame is rendered 
    /* Called before each frame is rendered */ 
    if pressedButtons.index(of: buttonDirLeft) != nil { 
     player.position.x -= 3.0 
    } 
    if pressedButtons.index(of: buttonDirRight) != nil { 
     player.position.x += 3.0 
    } 

    // Update entities  

} 
//MOVEMENT FUNCTIONS START HERE 
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let location = touch.location(in: self) 
     let previousLocation = touch.previousLocation(in: self) 

     for button in [buttonDirLeft, buttonDirRight] { 
      // I check if they are already registered in the list 
      if button.contains(location) && pressedButtons.index(of: button) == nil { 
       pressedButtons.append(button) 
      } 
     } 
    } 
} 


override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let location = touch.location(in: self) 
     let previousLocation = touch.previousLocation(in: self) 

     for button in [buttonDirLeft, buttonDirRight] { 
      // if I get off the button where my finger was before 
      if button.contains(previousLocation) 
       && !button.contains(location) { 
       // I remove it from the list 
       let index = pressedButtons.index(of: button) 
       if index != nil { 
        pressedButtons.remove(at: index!) 
       } 
      } 

       // if I get on the button where I wasn't previously 
      else if !button.contains(previousLocation) 
       && button.contains(location) 
       && pressedButtons.index(of: button) == nil { 
       // I add it to the list 
       pressedButtons.append(button) 

      }}}} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let location = touch.location(in: self) 
     let previousLocation = touch.previousLocation(in: self) 

     for button in [buttonDirLeft, buttonDirRight] { 
      if button.contains(location) { 
       let index = pressedButtons.index(of: button) 
       if index != nil { 
        pressedButtons.remove(at: index!) 
       } 
      } 
      else if (button.contains(previousLocation)) { 
       let index = pressedButtons.index(of: button) 
       if index != nil { 
        pressedButtons.remove(at: index!) 
       } 
      } 
     } 
    } 
} 


override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let location = touch.location(in: self) 
     let previousLocation = touch.previousLocation(in: self) 

    for button in [buttonDirLeft, buttonDirRight] { 
     if button.contains(location) { 
      let index = pressedButtons.index(of: button) 
      if index != nil { 
       pressedButtons.remove(at: index!) 
      } 
     } 
     else if (button.contains(previousLocation)) { 
      let index = pressedButtons.index(of: button) 
      if index != nil { 
       pressedButtons.remove(at: index!) 
      } 
     } 
    } 
} 
} 

func didBeginContact(contact: SKPhysicsContact){ 
print("colliding!") 
} 
+0

爲什麼要問這個問題兩次?我發佈了你問的另一個答案。 – crashoverride777

+0

@ crashoverride777我的節點仍然沒有識別到​​碰撞。玩家的硬幣反彈,但它不打印「你好」..任何想法爲什麼?我實現了你的代碼 – Andrew

回答

2

聯繫會發生只有在至少一個身體是動態的。因此,要麼將您的播放器或硬幣設置爲動態,並且如果其他設置正確(如果類別設置正確並且方法實施正確),您的身體現在將衝突/進行聯繫。如果您只對聯繫人感興趣,請將衝突位掩碼設置爲0.

相關問題