How to detect contact on different areas of a physicsbody

后端 未结 2 2032
长情又很酷
长情又很酷 2021-01-22 01:50

I would like to achieve the classic \'headshot\' effect in a sprite-kit game.

My ideas and attempts:

  1. Create separate \'body\' and \'head\' physicsBodies

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-22 02:36

    I use this setup in a lot of places. It scales pretty well if you find yourself wanting to add more parts, or weapons, and it lets you visually hide any of those parts or weapons easily. The use of userData can come in handy if you have a lot going on and different enemies have different "things", or you're doing crazy stuff like parenting child sprites to different entities based on gameplay, etc.

    -(SKSpriteNode*)makeAnEnemy
    {
    
        SKSpriteNode *mainEnemy;
        SKSpriteNode *enemyHead;
        //setup mainEnemy physics body
        //setup enemyHead physics body
        [mainEnemy addChild:enemyHead];
    
        // depending on your plans, on hit you could use the following code later:
        SKSpriteNode *theEnemyThatWasHit = (SKSpriteNode*)enemyHead.parent;
    
        //or you can use userdata and set it all up here
        [mainEnemy.userData setObject:enemyHead forKey:@"myHead"];
        [enemyHead.userData setObject:mainEnemy forKey:@"myBody"];
    
        return mainEnemy;
    }
    

提交回复
热议问题