Last active
November 3, 2017 19:04
-
-
Save retrokid/7d1376608e8a45b6d9d3866833c22d40 to your computer and use it in GitHub Desktop.
Delegate Protocol Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SpriteKit | |
class GameScene : SKScene, PlayerControllerDelegate | |
{ | |
// MARK:- PROPERTIES | |
var playerController : PlayerController = PlayerController() | |
// MARK:- INIT | |
override func didMove(to view: SKView) | |
{ | |
self.playerController.delegate = self | |
} | |
// MARK:- DELEGATE FUNCTIONS | |
func playerDidAttack() | |
{ | |
print("player did attack") | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SpriteKit | |
class Player : SKSpriteNode | |
{ | |
func attack() | |
{ | |
print("player attacks") | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class PlayerController : NSObject | |
{ | |
var player : Player = Player() // create player object | |
var delegate : PlayerControllerDelegate? // create delegate object | |
func attackKeyPressed() | |
{ | |
player.attack() // tell player object to attack | |
delegate?.playerDidAttack // tell delegate to tell scene to run playerDidAttack | |
} | |
} | |
protocol PlayerControllerDelegate | |
{ | |
func playerDidAttack() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment