Use Iterative Design to Adjust the Player’s Movement

In Create the Collectible Class, you were introduced to the term, iterative design. If you recall, iterative design is the process by which you prototype, test, analyze, and refine.

Currently, the player moves Blob by tapping either side of the screen. You could alter the code to move Blob faster, but that type of movement is not ideal for this type of game. Instead, you’ll make it so that players can slide Blob around by moving their fingers left and right. Not only will this make for better gameplay, but also you’ll get a chance to see what’s involved with getting a node to follow the touch location.

First, you need two new properties to track whether or not the player is moving and the last known position of the player node. You’ll name these properties movingPlayer and lastPosition, respectively.

Open the GameScene.swift file and add these new properties to the GameScene class below the playerSpeed property:

 // Player movement
 var​ movingPlayer = ​false
 var​ lastPosition: ​CGPoint​?

Using these two properties, you can now track if the player is moving and the last known position of the player node.

Next, you need to override three touch methods. Scroll down to the bottom of the GameScene class and add the following three overrides:

 override​ ​func​ ​touchesMoved​(_ touches: ​Set​<​UITouch​>, with event: ​UIEvent​?) {
 for​ t ​in​ touches { ​self​.​touchMoved​(toPoint: t.​location​(in: ​self​)) }
 }
 
 override​ ​func​ ​touchesEnded​(_ touches: ​Set​<​UITouch​>, with event: ​UIEvent​?) {
 for​ t ​in​ touches { ​self​.​touchUp​(atPoint: t.​location​(in: ​self​)) }
 }
 
 override​ ​func​ ​touchesCancelled​(_ touches: ​Set​<​UITouch​>,
  with event: ​UIEvent​?) {
 for​ t ​in​ touches { ​self​.​touchUp​(atPoint: t.​location​(in: ​self​)) }
 }

When you create a new project, the iOS Game template creates these touch handlers. In Clean Up the Default Template, you removed them for clarity—it’s time to add them back into the code.

For now, ignore the three errors you see about the value of type GameScene not containing these members—you’ll fix those in a moment—and modify the touchDown(atPoint:) method so that it matches this:

 func​ ​touchDown​(atPoint pos: ​CGPoint​) {
 let​ touchedNode = ​atPoint​(pos)
 if​ touchedNode.name == ​"player"​ {
  movingPlayer = ​true
  }
 }

With this code, you get the touched node and check to see if its name property is equal to player. If so, you set the movingPlayer property to true.

It’s time to fix those errors. Immediately below the touchDown(atPoint:) method, add the following two methods:

 func​ ​touchMoved​(toPoint pos: ​CGPoint​) {
 if​ movingPlayer == ​true​ {
 // Clamp position
 let​ newPos = ​CGPoint​(x: pos.x, y: player.position.y)
  player.position = newPos
 
 // Check last position; if empty set it
 let​ recordedPosition = lastPosition ?? player.position
 if​ recordedPosition.x > newPos.x {
  player.xScale = -​abs​(xScale)
  } ​else​ {
  player.xScale = ​abs​(xScale)
  }
 
 // Save last known position
  lastPosition = newPos
  }
 }
 
 func​ ​touchUp​(atPoint pos: ​CGPoint​) {
  movingPlayer = ​false
 }

The first method, touchMoved(pos:), verifies that the player is moving by ensuring that the movingPlayer property is true. It then clamps the player node’s position along the y-axis, and using the last known touch location and the current touch location, the node’s position is updated and the new location stored.

The second method, touchUp(pos:), gets called when the player stops touching the screen, which means the player is also not moving Blob anymore, so it sets movingPlayer to false.

Build and run the project. Touch Blob to get him to track with your finger, moving him as quickly or as slowly as you want.

images/ChainingActionsAndUsingIterativeDesign/spritekit-track-finger.png

For now, leave the level property set to 8, and consider this chapter complete.

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

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