swift初心者です。
下記のコードでは素早くドラッグした際にnodeがついてきません。
内にか解決法はないでしょうか?
swift
1import SpriteKit 2import GameplayKit 3 4class GameScene: SKScene, SKPhysicsContactDelegate { 5 6 var red: SKSpriteNode! 7 var blue: SKSpriteNode! 8 9 let redMask:UInt32 = 0b0001 10 let blueMask:UInt32 = 0b0010 11 12 override func didMove(to view: SKView) { 13 14 self.scaleMode = .aspectFill 15 16 let border = SKPhysicsBody(edgeLoopFrom: self.frame) 17 border.friction = 0 18 self.physicsBody = border 19 20 self.red = SKSpriteNode(imageNamed: "red") 21 self.red.scale(to: CGSize(width: 150, height: 150)) 22 self.red.position = CGPoint(x: 0, y: frame.height / 4) 23 self.red.physicsBody = SKPhysicsBody(circleOfRadius: self.red.frame.height / 2) 24 self.red.physicsBody?.categoryBitMask = redMask 25 self.red.physicsBody?.contactTestBitMask = 0b1111 26 self.red.physicsBody?.affectedByGravity = false 27 self.red.physicsBody?.restitution = 1.0 28 self.red.physicsBody?.linearDamping = 0.0 29 self.red.physicsBody?.friction = 0.0 30 addChild(self.red) 31 32 self.blue = SKSpriteNode(imageNamed: "blue") 33 self.blue.scale(to: CGSize(width: 100, height: 100)) 34 self.blue.position = CGPoint(x: frame.width / 4 - 30, y: -frame.height / 4) 35 self.blue.physicsBody = SKPhysicsBody(circleOfRadius: self.blue.frame.height / 2) 36 self.blue.physicsBody?.categoryBitMask = blueMask 37 self.blue.physicsBody?.contactTestBitMask = 0b0001 38 self.blue.physicsBody?.affectedByGravity = false 39 addChild(self.blue) 40 41 self.red.physicsBody?.applyImpulse(CGVector(dx: 500, dy: 500)) 42 } 43 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 44 let location = touches.first!.location(in: self) 45 let node = atPoint(location) as? SKSpriteNode 46 if node?.physicsBody != nil { 47 node?.position = location 48 } 49 } 50 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 51 52 } 53}
質問方法に至らない点がありましたらすみません。
回答1件
あなたの回答
tips
プレビュー