上下左右のコントロールボタンで座標を動かすプログラムを作っているのですが、ボタンを長押し(3秒ぐらいの時もあるし15〜20秒ぐらいの時もあります)して放すとtouchesEndedが呼び込まれないバグが時々出て暴走してしまいます。この原因がわかりません。
import
1import GameplayKit 2class GameScene: SKScene { 3 //コントローラー 4 let upButton = SKLabelNode(fontNamed: "Avenir-Black") 5 let downButton = SKLabelNode(fontNamed: "Avenir-Black") 6 let leftButton = SKLabelNode(fontNamed: "Avenir-Black") 7 let rightButton = SKLabelNode(fontNamed: "Avenir-Black") 8 //BOXラベル 9 var boxaddress: [SKLabelNode] = [] 10 var dx:[Int] = [] 11 var dy:[Int] = [] 12 //コントローラースイッチ 13 var isTouchedUp = false 14 var isTouchedDown = false 15 var isTouchedLeft = false 16 var isTouchedRight = false 17 //スイッチ内容ラベル 18 let SWLabel = SKLabelNode(fontNamed: "Avenir-Black") 19 var SW:[SKLabelNode] = [] 20 21 override func didMove(to view: SKView) { 22 23 //コントローラー表示 24 upButton.text = "UP" 25 upButton.fontSize = 30 26 upButton.position = CGPoint(x:375,y:150) 27 downButton.text = "DOWN" 28 downButton.fontSize = 30 29 downButton.position = CGPoint(x:375,y:50) 30 leftButton.text = "LEFT" 31 leftButton.fontSize = 30 32 leftButton.position = CGPoint(x:200,y:100) 33 rightButton.text = "RIGHT" 34 rightButton.fontSize = 30 35 rightButton.position = CGPoint(x:600,y:100) 36 self.addChild(upButton) 37 self.addChild(downButton) 38 self.addChild(leftButton) 39 self.addChild(rightButton) 40 //BOXラベル表示 41 for i in 0...5{ 42 for f in 0...5{ 43 let boxLabel = SKLabelNode(fontNamed: "Avenir-Black") 44 boxLabel.fontSize = 30 45 boxLabel.fontColor = SKColor.red 46 boxLabel.position = CGPoint(x:-200 + 300 * i,y:200 + 100 * f) 47 boxLabel.text = "X:\(Int(boxLabel.position.x)),Y:\(Int(boxLabel.position.y))" 48 dx.append(Int(boxLabel.position.x)) 49 dy.append(Int(boxLabel.position.y)) 50 boxaddress.append(boxLabel) 51 self.addChild(boxLabel) 52 }//for 53 }//for 54 }//did 55 56 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 57 for touch: AnyObject in touches { 58 let location = touch.location(in: self) 59 let touchNode = self.atPoint(location) 60 if touchNode == upButton { 61 isTouchedUp = true 62 } else if touchNode == downButton { 63 isTouchedDown = true 64 } else if touchNode == leftButton { 65 isTouchedLeft = true 66 } else if touchNode == rightButton { 67 isTouchedRight = true 68 } 69 }//for touch 70 }//touchesBegan 71 72 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 73 for touch: AnyObject in touches { 74 let location = touch.location(in: self) 75 let touchNode = self.atPoint(location) 76 if touchNode == upButton { 77 isTouchedUp = false 78 } else if touchNode == downButton { 79 isTouchedDown = false 80 } else if touchNode == leftButton { 81 isTouchedLeft = false 82 } else if touchNode == rightButton { 83 isTouchedRight = false 84 } 85 86 }//for 87 }//touchesEnded 88 89 override func update(_ currentTime: TimeInterval) { 90 for i in 0...35{ 91 if isTouchedUp { 92 dy[i] -= 5 93 boxaddress[i].text = "X:\(dx[i]),Y:\(dy[i])" 94 } 95 if isTouchedDown { 96 dy[i] += 5 97 boxaddress[i].text = "X:\(dx[i]),Y:\(dy[i])" 98 } 99 if isTouchedLeft { 100 dx[i] += 5 101 boxaddress[i].text = "X:\(dx[i]),Y:\(dy[i])" 102 } 103 if isTouchedRight { 104 dx[i] -= 5 105 boxaddress[i].text = "X:\(dx[i]),Y:\(dy[i])" 106 } 107 }//for 108 }//update 109 110}//class 111コード
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。