Modify the GameScene Class to Handle Paid Continues

Similar to how you added the numberOfFreeContinues property in Save and Load the Number of Continues, you need to add a property to track the number of paid continues; one that uses a custom getter and setter.

Open the GameScene.swift file, and below the code that adds the numberOfFreeContinues property, add the following code:

 var​ numberOfPaidContinues: ​Int​ {
 get​ {
 var​ qty: ​Int​ = 0
 for​ product ​in​ ​GameData​.shared.products {
 if​ product.id.​contains​(​"continue"​) {
  qty += product.quantity
  }
  }
 return​ qty
  }
 set​(newValue) {
 let​ product = ​GameData​.shared.products.​filter​(
  {$0.id.​contains​(​"continue"​)}).first
  product?.quantity = newValue
 updateContinueButton​()
  }
 }

The getter and setter for the new numberOfPaidContinues property filters on the product ID using the string continue. The getter adds the quantity and returns the value. The setter grabs the first matching product and updates its quantity.

To present the player with an accurate total number of continues, you need to combine the number of free continues with the number of purchased continues. For that, you’ll use another computed property. Add the following code below the code you just added:

 var​ numberOfContinues: ​Int​ {
 get​ {
 return​ numberOfFreeContinues + numberOfPaidContinues
  }
 }

With the new properties in place, you’re ready to update the code that handles displaying and using continues.

Find the updateContinueButton() method—it should be near the bottom of the file—and modify it to match this:

 func​ ​updateContinueButton​() {
 if​ numberOfContinues > maxNumberOfContinues {
 let​ texture = ​SKTexture​(imageNamed: ​"continueRemaining-max"​)
  continueGameButton.texture = texture
  } ​else​ {
 let​ texture = ​SKTexture​(imageNamed:
 "continueRemaining-​​(​numberOfContinues​)​​"​)
  continueGameButton.texture = texture
  }
 }

While you’re here, update the useContinue() method to pull from either the free continues or the paid continues, depending on which one has a continue available, like so:

 func​ ​useContinue​() {
 /* Verify the player has at least 1 continue.
  If so, reduce the continues by 1, first by checking the free
  continues. If no free continues exist, check the paid continues. */
 
 if​ numberOfContinues > 0 {
 
 // Check from where to pull
 if​ numberOfFreeContinues > 0 {
  numberOfFreeContinues -= 1
  } ​else​ ​if​ numberOfPaidContinues > 0 {
  numberOfPaidContinues -= 1
  }
 
 // Continue game
  isContinue = ​true
 spawnMultipleGloops​()
  }
 }

This new code first checks the quantity of the free continues. If none are available, it then pulls from the paid continues.

The next step is to prepare the shop UI.

Setting up the Shop Interface

In the Project Navigator, select the ShopScene.sks file and you’ll see the following scene in the Scene Editor:

images/MonetizingYourGamesWithInAppPurchases/iap-missing-resources.png

Whoa, that’s a lot of Xs. If this were a treasure hunt, you’d be in for one heck of a haul—but it’s not, so put away that shovel and get back to work.

The problem here is that you added the scene but didn’t add the resources the scene uses.

Open the Assets.xcassets asset catalog and create two new sprite atlases. Name the first shop_ui and the second shop_iaps.

The shop_ui sprite atlas will hold the buy, exit, price, restore, and shop image sets:

images/MonetizingYourGamesWithInAppPurchases/iap-shop-ui.png

The shop_iaps sprite atlas will hold the continue-1, continue-3, continue, default, and removeads image sets:

images/MonetizingYourGamesWithInAppPurchases/iap-shop-iaps.png

Additional Image Resources

images/aside-icons/info.png

Note that the continue-3 and continue image resources aren’t used in this chapter, but they’re included in case you want to play around with some additional product options on your own.

After creating the new sprite atlases, copy the image files from the resources/shop_ui folder into the shop_ui sprite atlas, and copy the image files from the resources/shop_iaps folder into the shop_iaps sprite atlas. (Don’t forget to delete the default Sprite image set from each new atlas.)

Back in Xcode, select the ShopScene.sks file in the Project Navigator and you’ll see something more like this:

images/MonetizingYourGamesWithInAppPurchases/iap-fixed-resources.png

With the resources added to your project, and the shop scene looking less like a treasure map and more like an in-game shop, you’re ready to configure the scene (and see what makes it tick).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset