プログラミングの独学中です。
現在、XcodeのSpritekitをつかって、クリックしたら画面上にボールを出現させる
というプログラムを作成しました。
問題は、TouchesBeganでボールを作成することはできますが、touchesMovedですでに
作成されたボールを移動させることができません。
※新しく作成されるボールだけ移動することができます。
どのようなコードを追加および、現在のコードを改造すればよいでしょうか、ご教示願います。
swift
1import SpriteKit 2import GameplayKit 3import Foundation 4 5class GameScene: SKScene { 6 7 var circle = SKShapeNode(circleOfRadius: 50) 8 var location = CGPoint(x:0,y:0) 9 var animator: UIDynamicAnimator? 10 11 override func didMove(to view: SKView) { 12 } 13 14 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 15 for touch: AnyObject in touches { 16 animator?.removeAllBehaviors() 17 let location = touch.location(in: self) 18 19 self.circle = SKShapeNode(circleOfRadius: 50) 20 self.circle.physicsBody = SKPhysicsBody(circleOfRadius: 50) 21 self.circle.fillColor = SKColor.yellow 22 self.circle.position = location 23 self.circle.physicsBody!.affectedByGravity = false 24 self.circle.physicsBody?.restitution = 0.5 25 self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame) 26 self.addChild(circle) 27 } 28 } 29 30 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 31 let location = touches.first!.location(in: self) 32 let action = SKAction.move(to: CGPoint(x:location.x, y:location.y + 20), duration: 0.1) 33 circle.run(action) 34 } 35 36 37 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 38 self.circle.physicsBody!.affectedByGravity = true 39 physicsWorld.gravity = CGVector(dx:0,dy:-7.0) 40 } 41 42 43 override func update(_ currentTime: TimeInterval) { 44 } 45}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。