次は自機の売却やアップグレードをできるようにします。仕様は自機をタッチしたらアップグレードと売却を選択できるようにします。
Char.swiftを以下のように修正してください。levelプロパティを追加して、levelに応じて攻撃力が上がるようにしました。
class Char: SKSpriteNode { override init(texture: SKTexture?, color: UIColor, size: CGSize) { super.init(texture: texture, color: color, size: size) userInteractionEnabled = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } var level = 1 var didTouched: (() -> ())? // 省略 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { didTouched?() } } class Char1: Char { // 省略 override var power: Int { return 1 * level } // 省略 } class Char2: Char { // 省略 override var power: Int { return 2 * level } // 省略 } class Char3: Char { // 省略 override var power: Int { return 3 * level } // 省略 }
続けてSetCharScene.swiftを修正します。
class SetCharScene: SKScene { // 省略 func createChars(charData: [(factory: CharFactory, position: CGPoint)]) { charData.forEach { let char = $0.factory.createChar() char.position = $0.position char.physicsBody = SKPhysicsBody(rectangleOfSize: char.size) addChild(char) self.charData.append($0) // 下の4行を追加 let factory = $0.factory char.didTouched = { self.charDidTouched(char, factory: factory) } } } func charDidTouched(char: Char, factory: CharFactory) { let alert = UIAlertController(title: nil, message: "どのキャラクターを設置しますか?", preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "アップグレード", style: .Default, handler: { _ in self.coin -= char.cost char.level += 1 })) alert.addAction(UIAlertAction(title: "売却", style: .Default, handler: { _ in let index = self.charData.indexOf { $0.factory === factory }! self.charData.removeAtIndex(index) self.coin += char.cost / 2 char.removeFromParent() })) alert.addAction(UIAlertAction(title: "キャンセル", style: .Cancel, handler: nil)) let rootViewController = UIApplication.sharedApplication().delegate?.window??.rootViewController rootViewController?.presentViewController(alert, animated: true, completion: nil) } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { if touches.count == 1, let point = touches.first?.locationInNode(self) { let alert = UIAlertController(title: nil, message: nil, preferredStyle: .Alert) let rootViewController = UIApplication.sharedApplication().delegate?.window??.rootViewController alert.addAction(UIAlertAction(title: "Char1を設置", style: .Default, handler: { _ in let factory = Char1Factory() let char = factory.createChar() char.position = point self.coin -= char.cost self.addChild(char) self.charData.append((factory: factory, position: point)) // 下の3行を追加 char.didTouched = { self.charDidTouched(char, factory: factory) } })) alert.addAction(UIAlertAction(title: "Char2を設置", style: .Default, handler: { _ in let factory = Char2Factory() let char = factory.createChar() char.position = point self.coin -= char.cost self.addChild(char) self.charData.append((factory: factory, position: point)) // 下の3行を追加 char.didTouched = { self.charDidTouched(char, factory: factory) } })) alert.addAction(UIAlertAction(title: "Char3を設置", style: .Default, handler: { _ in let factory = Char3Factory() let char = Char3() char.position = point self.coin -= char.cost self.addChild(char) self.charData.append((factory: factory, position: point)) // 下の3行を追加 char.didTouched = { self.charDidTouched(char, factory: factory) } })) alert.addAction(UIAlertAction(title: "キャンセル", style: .Cancel, handler: nil)) rootViewController?.presentViewController(alert, animated: true, completion: nil) } } }
これで自機のアップグレードや売却ができるようになりました。
Copyright © ITmedia, Inc. All Rights Reserved.