How can multiple physics bodies be attached to a sprite?

  • DarkArctic
    Likes 0

    Problem Description

    I’m trying to make a beat ‘em up type game and I’d like to have my sprites to have a hit box (on top of the sprite to capture hits), an attack box (to the right or left to register attacks) and a collision box (near the bottom to stop him when he hits a wall or something).  I think I’d need a separate PhysicsBody for each one since only the “collision box” would be able to do wall and item collisions.  The other two are just there to detect whether they’ve been punched or not.

    The problem I’m running into is no matter how I implement it I have some sort of problem.  For example, I’ve had the “collision box” as the physics body of the sprite and then the “attack box” and “hit box” as child nodes.  But when the “collision box” hits something the “attack box” and “hit box” keep moving since they’re unaffected by physics.

    Is there a good method for having these three PhysicsBody’s on my sprite or is there a better way that I’m not aware of?

  • Sonar Systems admin
    Likes 1

    I don’t think you can attach multiple bodies to a single sprite/node, try asking the Cocos people but i don’t think you can.

    You could achieve this by having multiple sprites but only one visible and attach a body to each one. Not the most elegant solution but a solution nonetheless


    This reply has been verified.
  • DarkArctic
    Likes 1

    I think you’re right.  I took your idea a bit further and just made them child Node’s each with their physics own bodies.  I have to set their position every frame otherwise they keep moving when the sprite collides with something.

    Thanks a lot!


    This reply has been verified.
  • Sonar Systems admin
    Likes 0

    Great to hear you are advancing.

Login to reply