Connect the Attack Button

The last thing you’ll do in this chapter is write the code that handles the player’s attack routine—in this case, Val’s knife throw—which should happen when the player taps the Attack button.

In the Project Navigator, select the GameScene.swift file and modify the touchDown(:_) method to match the following (you’ll get a message about the missing attack() method, but for now, ignore the error):

 func​ ​touchDown​(atPoint pos : ​CGPoint​) {
 let​ nodeAtPoint = ​atPoint​(pos)
 if​ ​let​ touchedNode = nodeAtPoint ​as?​ ​SKSpriteNode​ {
 if​ touchedNode.name?.​starts​(with: ​"controller_"​) == ​true​ {
 let​ direction = touchedNode.name?.​replacingOccurrences​(
  of: ​"controller_"​, with: ​""​)
  player?.​move​(​Direction​(rawValue: direction ?? ​"stop"​)!)
  } ​else​ ​if​ touchedNode.name == ​"button_attack"​ {
  player?.​attack​()
  }
  }
 }

Once again, you’re checking the node name. If it matches button_attack, you’re calling the player’s attack() method, which you still need to write.

Open the Player.swift file and add the following code:

 func​ ​attack​() {
 let​ projectile = ​SKSpriteNode​(imageNamed: ​"knife"​)
  projectile.position = ​CGPoint​(x: 0.0, y: 0.0)
 addChild​(projectile)
 }

This is a good start, but at this point, you have no surefire way (pun intended) to know in which direction the player is currently facing. For that, you can add a new property to the Player class (add it just above the move(_:) method):

 private​ ​var​ currentDirection = ​Direction​.stop

You’ll use this new property to store the last known direction of the player. To do that, you need to update the move(_:) method by adding the following code at the end of the method:

 if​ direction != .stop {
  currentDirection = direction
 }

This saves the last known direction of the player, even after the player stops moving.

Finally, go back to the attack() method and update it to match this (the new code is highlighted):

 func​ ​attack​() {
 let​ projectile = ​SKSpriteNode​(imageNamed: ​"knife"​)
  projectile.position = ​CGPoint​(x: 0.0, y: 0.0)
 addChild​(projectile)
 
»var​ throwDirection = ​CGVector​(dx: 0, dy: 0)
»
»switch​ currentDirection {
»case​ .up:
» throwDirection = ​CGVector​(dx: 0, dy: 300)
»case​ .down:
» throwDirection = ​CGVector​(dx: 0, dy: -300)
»case​ .left:
» throwDirection = ​CGVector​(dx: -300, dy: 0)
»case​ .right, .stop: ​// default pre-movement (throw right)
» throwDirection = ​CGVector​(dx: 300, dy: 0)
»case​ .topLeft:
» throwDirection = ​CGVector​(dx: -300, dy: 300)
»case​ .topRight:
» throwDirection = ​CGVector​(dx: 300, dy: 300)
»case​ .bottomLeft:
» throwDirection = ​CGVector​(dx: -300, dy: -300)
»case​ .bottomRight:
» throwDirection = ​CGVector​(dx: 300, dy: -300)
» }
»
»let​ throwProjectile = ​SKAction​.​move​(by: throwDirection, duration: 0.25)
» projectile.​run​(throwProjectile,
» completion: {projectile.​removeFromParent​()})
 }

Build and run the project. Move the player around in a few different directions and tap the Attack button. Great, the knife travels in the right direction, but it looks a little weird since it’s always pointing up.

images/UsingTheSceneEditorToAddPhysics/scene-edit-phy-build-01.png

You can fix this visual problem by setting the rotation of the projectile node. Update the case statement in the attack() method to match the following code:

 switch​ currentDirection {
 case​ .up:
  throwDirection = ​CGVector​(dx: 0, dy: 300)
  projectile.zRotation = 0
 case​ .down:
  throwDirection = ​CGVector​(dx: 0, dy: -300)
  projectile.zRotation = -​CGFloat​.pi
 case​ .left:
  throwDirection = ​CGVector​(dx: -300, dy: 0)
  projectile.zRotation = ​CGFloat​.pi/2
 case​ .right, .stop: ​// default pre-movement (throw right)
  throwDirection = ​CGVector​(dx: 300, dy: 0)
  projectile.zRotation = -​CGFloat​.pi/2
 case​ .topLeft:
  throwDirection = ​CGVector​(dx: -300, dy: 300)
  projectile.zRotation = ​CGFloat​.pi/4
 case​ .topRight:
  throwDirection = ​CGVector​(dx: 300, dy: 300)
  projectile.zRotation = -​CGFloat​.pi/4
 case​ .bottomLeft:
  throwDirection = ​CGVector​(dx: -300, dy: -300)
  projectile.zRotation = 3 * ​CGFloat​.pi/4
 case​ .bottomRight:
  throwDirection = ​CGVector​(dx: 300, dy: -300)
  projectile.zRotation = 3 * -​CGFloat​.pi/4
 }

Build and run the project again. This time, notice that Val’s knife throwing looks more natural than it did before, thanks to the rotation of the knife.

images/UsingTheSceneEditorToAddPhysics/scene-edit-phy-build-02.png

Ah, if only it was that easy to level-up your knife-throwing skills. C’est la vie.

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

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