練習のつもりでitunesでデベロッパー登録してxcodeのswiftでブロック崩しを制作しています。
開発環境
macbookpro
Xcode Version 11.0 beta 4
Swift version 5.1
参考サイト
ここ
GameScene.swiftに以下のコードを書き込みました。
swift
1import SpriteKit 2import GameplayKit 3class GameScene: SKScene , SKPhysicsContactDelegate { 4 var entities = [GKEntity]() 5 var graphs = [String : GKGraph]() 6 var ball:SKSpriteNode! 7 var paddle:SKSpriteNode! 8 override func didMove(to view: SKView) { 9 //withname にはGameScene.sksで作成した名前 10 ball = self.childNode(withName: "ball") as? SKSpriteNode 11 paddle = self.childNode(withName: "paddle") as? SKSpriteNode 12 //ボールの飛ぶ方向 13 ball.physicsBody?.applyImpulse(CGVector(dx: 5, dy: 5)) 14 //ボールが壁にぶつかった時に跳ねかえる 15 let border = SKPhysicsBody(edgeLoopFrom: (view.scene?.frame)!) 16 border.friction = 0 17 self.physicsBody = border 18 //SKPhysicsContactDelegate をクラス参照に入れてないとエラーとなる 19 self.physicsWorld.contactDelegate = self 20 } 21 //パドル 22 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 23 for touch in touches** 24 { 25 let toucLocation = touch.location(in: self) 26 paddle.position.x = toucLocation.x 27 } 28 } 29 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 30 for touch in touches 31 { 32 let toucLocation = touch.location(in: self) 33 paddle.position.x = toucLocation.x 34 } 35 } 36 //ブロックがボールに当たると消える。 37 func didBegin(_ contact : SKPhysicsContact) 38 { 39 let bodyAName = contact.bodyA.node?.name; 40 let bodyBName = contact.bodyB.node?.name; 41 if bodyAName == "ball" && bodyBName == "brick" || 42 bodyAName == "brick" && bodyBName == "ball" 43 { 44 if bodyAName == "brick" 45 { 46 contact.bodyA.node?.removeFromParent() 47 } 48 else if bodyBName == "brick" 49 { 50 contact.bodyB.node?.removeFromParent() 51 } 52 } 53} 54} 55
するとパドルの2箇所にエラーが表示されます。
error1
1 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
【error message】
- Method does not override any method from its superclass
- Use of undeclared type 'UIEvent'
- Use of undeclared type 'UITouch'
error2
1override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
【error message】
- Method does not override any method from its superclass
- Use of undeclared type 'UIEvent'
- Use of undeclared type 'UITouch'
参考サイトで制作した時とSwiftのバージョンが異なることから定義の仕方が異なるかと思うのですが、現バージョンでの表記の仕方が分かりません。
ご教授いただけないでしょうか。宜しくお願いいたします。
回答1件
あなたの回答
tips
プレビュー