ryohei ryohei - 1 year ago 62
Swift Question

How to fire a shot nicely without any problems?

When a button is pressed, a shot node is fired from a character node's position and moves to x direction. If it is collided with a node, then both nodes disappear.

func attackButton() {


_weapon.position = CGPointMake(character.position.x, charater.position.y)

When this function is triggered, normally it works without any problem. However, when I press
, which enables the character to jump to the Y direction, and 'attackButton', the character's coordinates are somehow affected. The character moves and sometimes rotates. From my code, I never wrote anything that causes it to move to different directions. This gets me annoying. I cannot come up with any solution to prevent that from occurring. Is there anyone who knows how to solve this?

Answer Source

You're providing less code than I'm able to work from so I'm going to say what I think that is happening:

The weapon is being spawned at the exact same position as the character but it's being moved out of it really quickly by the physics engine. This positional correction applies impulses to the character body, causing it to rotate.

You can solve this problem in two different ways:

  • Spawn the weapon outside the character, or...
  • Disable collisions between weapon and character.

Try changing your 5th line to something like this:

_weapon.position = CGPointMake(character.position.x + character.size.width / 2, charater.position.y)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download