Sprite Kit Tutorial: Making a Universal App: Part 1

Learn how to make a universal app that works on the iPhone, iPad, and retina display in this Sprite Kit tutorial! By Nicholas Waynik.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Popping the Moles

Now that we're sure the moles are in the right place, let's add the code to make them pop out of their holes.

First things first - switch the zPosition of 999 for the mole sprites back to 2 so the moles are underground.

Once that's done, add the following code to your update: method:

for (SKSpriteNode *mole in self.moles) {
    if (arc4random() % 3 == 0) {
        if (!mole.hasActions) {
            [self popMole:mole];
        }
    }
}

If you haven't seen this before, the update method is called once per frame. You want to try popping some moles out of their holes every time the method is called. The new code will loop through each mole and give it a 1 in 3 chance of popping out of its hole. But it will only pop out if it isn't moving already - and one easy way to check for this is to check the return value of the sprite’s hasActions method. If the sprite has running actions then hasActions will return YES.
Next, add the implementation of popMoles:

- (void)popMole:(SKSpriteNode *)mole
{
	SKAction *easeMoveUp = [SKAction moveToY:mole.position.y + mole.size.height duration:0.2f];
    	easeMoveUp.timingMode = SKActionTimingEaseInEaseOut;
    	SKAction *easeMoveDown = [SKAction moveToY:mole.position.y duration:0.2f];
    	easeMoveDown.timingMode = SKActionTimingEaseInEaseOut;
    	SKAction *delay = [SKAction waitForDuration:0.5f];

    	SKAction *sequence = [SKAction sequence:@[easeMoveUp, delay, easeMoveDown]];
    	[mole runAction:sequence];
}

This code uses some Sprite Kit actions to make the mole pop out of it’s hole, pause for half a second, then pop back down. Let's go through this line-by-line to make sure we're on the same page:

  1. Creates an action to move the mole move up along the Y axis as much as the mole is tall. Since you placed the mole right below the hole, it will look right.
  2. To make the movement look more natural, it sets the timingMode of the action to SKActionTimingEaseInEaseOut. This causes the action to go slower at the beginning and end, as if the mole is accelerating/decelerating, as it naturally would.
  3. To create an action to move the mole move back down again, create an action similar to the one used to move the mole up except use the mole’s current y-axis position.
  4. Creates an action to pause for one second after the mole pops out.
  5. Now that the actions are ready to go, it runs them on the mole in a sequence: move up, delay, and finally move down.

That's it! Compile and run the code, and you'll see the moles happily popping out of their holes!

Moles popping out of holes

Where To Go From Here?

Here is a sample project with all of the code we've developed so far in this Sprite Kit tutorial series.

Next check out Part 2, where you'll add some cute animations to the mole as he laughs and gets whacked, add gameplay so you can do the whacking and earn points, and of course add some gratuitous sound effects as usual.

Please add a comment below in if you have any thoughts, advice, or suggestions for future tutorials!

Nicholas Waynik

Contributors

Nicholas Waynik

Author

Over 300 content creators. Join our team.