2017-10-21 145 views
0

我正在爲一個精靈製作動畫,當它觸及另一個精靈時,會調用一個物理接觸函數。在這個函數中,我試圖讓這個精靈碰到另一個。它的身體是bodyA,但它是一個SKPhysicsBody,並且它不能轉換爲SKSpriteNode。你有什麼想法? 這個函數在接觸時被正確地調用,我只是試着去得到身體進行接觸的精靈。最後的想法是獲取我正在尋找的精靈附加的動作,但我想當你有精靈時很容易。將精靈附加到SKPhysicsBody

let shootCategory: UInt32 = 0x1 << 0 
    let enemyCategory: UInt32 = 0x1 << 1 


    // Declaration of the SKPhysicsBody of the sprite wich will touch the other one 
    sprite.physicsBody = SKPhysicsBody(circleOfRadius: (20)) 
    sprite.physicsBody?.usesPreciseCollisionDetection = true 
    sprite.physicsBody?.categoryBitMask = shootCategory 
    sprite.physicsBody?.collisionBitMask = shootCategory | enemyCategory 
    sprite.physicsBody?.contactTestBitMask = shootCategory | enemyCategory 
    sprite.physicsBody?.affectedByGravity = false 

    // Declaration of the SKPhysicsBody of the sprite wich will be touched 
    sprite.run(SKAction.group([moveAction, fadeInAction])) 
    sprite.physicsBody = SKPhysicsBody(circleOfRadius: (20)) 
    sprite.physicsBody?.usesPreciseCollisionDetection = true 
    sprite.physicsBody?.categoryBitMask = enemyCategory 
    sprite.physicsBody?.collisionBitMask = shootCategory | enemyCategory 
    sprite.physicsBody?.contactTestBitMask = shootCategory | enemyCategory 
    sprite.physicsBody?.affectedByGravity = false 


    func didBegin(_ contact: SKPhysicsContact) { 

    if (contact.bodyA.categoryBitMask == enemyCategory) && (contact.bodyB.categoryBitMask == shootCategory){ 

     // I tried this to get the sprite wich the bodyB is attached to but it doesn't even build 
     let sprite: SKSpriteNode = sprite.contact.bodyB 
     contact.bodyA.removeAction(forKey: "moveToAction") 
    } 
} 
+0

蘋果的文件,我會建議使用它們https://developer.apple.com/documentation/spritekit/skphysicsbody/1520049-節點 – Knight0fDragon

回答

0

我終於找到了解決方案是非常簡單的:一個原因存在

let Node = contact.bodyB.node as! SKSpriteNode