I try to make a transition to another scene when the wheel stops spinning. The code that I have is shown below. I canβt understand how to determine when the speed has reached 0.0?
import SpriteKit import GameplayKit class GameplayScene: SKScene { var player: Player?; override func didMove(to view: SKView) { player = self.childNode(withName: "spinner") as! Player?; } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in touches { let location = touch.location(in: self) if atPoint(location).name == "play_button" { spin() } } } func spin () { let random = GKRandomDistribution(lowestValue: 20, highestValue: 90) let r = random.nextInt() player?.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(self.frame.width)) player?.physicsBody?.affectedByGravity = false player?.physicsBody?.isDynamic = true player?.physicsBody?.allowsRotation = true player?.physicsBody?.angularVelocity = CGFloat(r) player?.physicsBody?.angularDamping = 1.0 } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { } }
So, here I would like to do the following when the wheel stopped spinning:
let play_scene = Questions(fileNamed: "QuestionsScene") play_scene?.scaleMode = .aspectFill self.view?.presentScene(play_scene!, transition: SKTransition.doorsOpenVertical(withDuration: 1))
Now I have edited the class and it looks like this:
import SpriteKit import GameplayKit class GameplayScene: SKScene, SKSceneDelegate { var player: Player? override func didMove(to view: SKView) { self.delegate = self player = self.childNode(withName: "spinner") as! Player?; } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in touches { let location = touch.location(in: self) if atPoint(location).name == "play_button" { spin() } } } func spin () { let random = GKRandomDistribution(lowestValue: 20, highestValue: 90) let r = random.nextInt() player?.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(self.frame.width)) player?.physicsBody?.affectedByGravity = false player?.physicsBody?.isDynamic = true player?.physicsBody?.allowsRotation = true player?.physicsBody?.pinned = true player?.physicsBody?.angularVelocity = CGFloat(r) player?.physicsBody?.angularDamping = 1.0 } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { } override func didSimulatePhysics() { if ((player?.physicsBody?.angularVelocity)! <= CGFloat(0.01)) { print("Got it") } } }
The problem is that I still get the error message in the if statement in the didSimulatePhysics function. The error I get is "Thread 1: EXC_BAD_INSTRUCTION ...."
source share