I'm working on making an RPG in Xcode, and I'm having a major gameplay issue when it comes to having my character swing his sword in a way that is realistic and gameplay compatible.

Right now, when the player taps the screen and the sword is in one of the player's hand, it rotates the upright sword 90 degrees. The sword which is a parent of the player floats in front of the player because of a collision issue

I'm looking for any advice anyone can give on how to add a sword to the game and have it swing in a way that looks somewhat realistic and can damage enemies that are more than a single pixel away from the player

  • 1
    \$\begingroup\$ I assume this is a top-down game, as your description would be odd otherwise. Do you mean the sword is the child of the player? Would an animation not serve your purposes, rather than a rotating sprite? Have you looked at the sample SpriteKit game, called "Adventure"? \$\endgroup\$ – Attackfarm Jul 19 '14 at 2:57
  • 1
    \$\begingroup\$ Also, do you have any idea why your sword is only damaging enemies a single pixel away? That seems like odd behavior that I can only imagine would result from a very small or misaligned SKPhysicsBody \$\endgroup\$ – Attackfarm Jul 19 '14 at 3:20
  • 1
    \$\begingroup\$ It is top down. I was trying to use animation, but the size of the player remained constant so it just made a swinging motion. What I meant was one pixel away was that running the swing animation would make the swinging motion, but unless an enemy was literally on top of me, there would be no collision like you should get with a long sword that can reach far out. \$\endgroup\$ – jking14 Jul 19 '14 at 4:23
  • \$\begingroup\$ Basically, I want the sword to swing beyond the player's body \$\endgroup\$ – jking14 Jul 19 '14 at 4:26
  • \$\begingroup\$ How large is the sprite before the animation, and how large afterwards? \$\endgroup\$ – Attackfarm Jul 19 '14 at 4:28

I think most of your problems might be solved by understanding the use of category masks. Simply set the masks to ignore contact and collision between the sword and player categories.

Specifically, this is done by assigning unsigned integers to the categoryBitMask, collisionBitMask, and contactBitMask properties of the SKPhysicsBody of the SKSpriteNode in question.

  • categoryBitMask is the category of the physics body (e.g. swordCategory)
  • contactBitMask is the product of the ORed categories of the types of nodes you want to test for contact (e.g. enemyCategory | breakableCategory | wallCategory)
  • collisionBitMask is the product of the ORed categories of the types of nodes you want to resolve collision (e.g. enemyCategory | wallCategory)
  • \$\begingroup\$ I am well aware of category masks and have directed both not to interact with one another \$\endgroup\$ – jking14 Jul 19 '14 at 4:18
  • \$\begingroup\$ I assumed they were because you said the sword "floats" because of a collision issue. If the problem isn't that the sword is colliding with the player, what is the "collision issue" you're having? \$\endgroup\$ – Attackfarm Jul 19 '14 at 4:29
  • \$\begingroup\$ Ok for some reason it seems to act as if the monster's bit mask is the same as the players \$\endgroup\$ – jking14 Jul 19 '14 at 4:55
  • \$\begingroup\$ Make sure you remember to set every physics body's category, or it might default to a specific category, which in your case might be the player's category \$\endgroup\$ – Attackfarm Jul 19 '14 at 5:00
  • \$\begingroup\$ I'm checking each one (and there are quite a few), but they all seem to be set so far \$\endgroup\$ – jking14 Jul 19 '14 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.