2017-10-21 28 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-node – Knight0fDragon

답변

0

마침내과 솔루션은 정말 간단하다 발견 :

let Node = contact.bodyB.node as! SKSpriteNode