Notice that we determined the position of the balloon by asking for at child node named mouth
. That’s possible because we explicitly added a node named mouth
as a child of each cannon to define where the balloon should appear. This approach freed us from having to calculate the position, and if we wanted to reposition where the balloons first appear, we could do that directly in Xcode, without changing the code.
To actually fire the balloon, we apply an impulse to its physics body to move it across the scene. An impulse is an instantaneous change to the body’s velocity. By applying an impulse to a body, SpriteKit pushes it in the direction specified (by an impulse vector). We’ve based the direction on the rotation of the firing cannon.
Finally, we wrap creation, displaying, and firing of a balloon in a single function that we can call later.