###概要
Swift3,Xcode8,SpriteKitを使用してパズルゲームのようなものを作っています。
ストーリーボードで設定したUIGestureRecognizerをGameViewControllerに適応し、ドラッグアンドドロップでブロックを動かせるようにしています。
ブロックを揃えて消し、制限時間内のスコアを競います。
時間制限が過ぎたら、画面の操作を中止し、TimeUpとハイスコアをラベルで表示し、リトライボタンを設置します。
リトライボタンを押された場合、presentSceneでそのシーン自身に遷移させることでゲーム開始時の状態に戻しています。
###発生している問題
リトライボタンを押した際、画面はゲーム開始時の状態に戻りますが、UIGestureRecognizerが反応しなくなり、ドラッグアンドドロップができない状態になります。
遷移後もドラッグアンドドロップできる状態にするにはどうしたらよいのでしょうか。
GameViewController
var panPointReference: CGPoint? @IBAction func didPan(_ sender: UIPanGestureRecognizer) { if scene.status == "wait"{ scene.timerStart() } if scene.timeLimit == 0 { return } let currentPoint = sender.translation(in: self.view) if panPointReference != nil { // print("currentPoint: \(currentPoint)") scene.moving(p:currentPoint) } else if sender.state == .began { // print(".Began") panPointReference = currentPoint } if sender.state == .ended { // print(".Ended") panPointReference = nil scene.movingEnd(vector: scene.vector) scene.vector = "" } }
GameScene
func timeUp(){ status = "timeUp" timer.invalidate() let retryButton = Button(text: "retry?", rect: CGRect(x:200,y:40,width:100,height:30), afterTap:{[weak self] in let scene = GameScene(size: (self?.scene!.size)!) scene.status = "wait" self?.isPaused = false scene.scaleMode = .resizeFill self?.view?.presentScene(scene) }) addChild(retryButton) }
ButtonClass
class Button: SKSpriteNode { let afterTap:() ->() init (text: String,rect: CGRect, afterTap: @escaping () -> ()) { self.afterTap = afterTap super.init(texture: nil, color: UIColor.clear, size: rect.size) position = rect.origin isUserInteractionEnabled = true let rectplus = SKShapeNode(rect: CGRect(origin: CGPoint(), size:rect.size),cornerRadius:4.0) rectplus.fillColor = UIColor.clear rectplus.strokeColor = UIColor.clear addChild(rectplus) let pluslabel = SKLabelNode(fontNamed: "MarkerFelt-Thin") pluslabel.text = text pluslabel.fontSize = CGFloat(30) pluslabel.fontColor = UIColor.black pluslabel.position = CGPoint(x:rect.width / 2, y: rect.height / 2 - pluslabel.frame.height / 2) pluslabel.zPosition = 7 addChild(pluslabel) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func touchesEnded(_ touches:Set<UITouch>,with event:UIEvent?){ afterTap() } }