2017-10-21 16 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 
関連する問題