Ios 检测旋转何时完成并移动到新场景
我试图在轮子停止旋转时实现移动到另一个场景。我的代码如下所示。我不知道如何检测速度何时达到0.0Ios 检测旋转何时完成并移动到新场景,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我试图在轮子停止旋转时实现移动到另一个场景。我的代码如下所示。我不知道如何检测速度何时达到0.0 import SpriteKit import GameplayKit class GameplayScene: SKScene { var player: Player?; override func didMove(to view: SKView) { player = self.childNode(withName: "spinner") as! Play
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?) {
}
}
我现在编辑了这个类,如下所示:
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")
}
}
}
导入SpriteKit
导入游戏工具包
类游戏场景:SKScene,SKSceneDelegate{
玩家:玩家?
覆盖func didMove(到视图:SKView){
self.delegate=self
player=self.childNode(名称为“微调器”)作为!player?;
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
如果在点(位置)。名称==“播放按钮”{
自旋()
}
}
}
func自旋(){
let random=gk随机分布(最低值:20,最高值:90)
设r=random.nextInt()
player?.physicsBody=SKPhysicsBody(圆圈半径:CGFloat(self.frame.width))
玩家?.physicsBody?.affectedByGravity=假
玩家?.physicsBody?.isDynamic=true
玩家?.physicsBody?.allowsRotation=true
玩家?.physicsBody?.pinned=真
玩家?.physicsBody?.angularVelocity=CGFloat(r)
玩家?.physicsBody?.angularDamping=1.0
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
}
重写func didSimulatePhysics(){
如果((player?.physicsBody?.angularVelocity)!你的轮子的SKPhysicsBody
有一个内置属性,angularVelocity
,它会告诉你它旋转的速度。当你将它设置为r
时,你已经在使用它开始旋转
要观看angularVelocity
,可以使用didSimulatePhysics()
。在物理计算完成后,它每帧被调用一次。它将如下所示:
func didSimulatePhysics() {
if wheelIsSpinning && angularVelocity != nil && angularVelocity! <= CGFloat(0.001) {
wheelIsSpinning = false
// wheel has stopped
// add your code here
}
}
func模拟物理(){
如果车轮正在旋转&&angularVelocity!=零&&angularVelocity!谢谢Robert。我后来发现,即使当我打印速度以进行控制时车轮看起来已经停止了,但它看起来仍然旋转得非常慢。实际上,它从来没有停止过stops@Nick只需检查.angularvocity@Robert是否将场景设置为自己的delegate没有意义。在SKSceneDelegate
协议中找到的所有方法包括didSimulatePhysics()
在SKScene
及其子类上自动调用。设置其他类来实现此协议将更有意义…@Nick除了您不需要该行之外,如果您仍然需要类似的内容,请将self.delegate=self
放在init()中
或其他适当的方法。您不能在类的主体内部使用self。@Nick从场景的定义中删除SKSceneDelegate
一致性(您的场景应如下所示class GameplaySecene:SKScene{}
。从您的代码中删除self.delegate=self
。当您这样做时,请告诉我错误是什么。Robert说的所有其他内容都应该有效。遵守SKSceneDelegate
是不必要的。这就是我的观点。
func didSimulatePhysics() {
if wheelIsSpinning && angularVelocity != nil && angularVelocity! <= CGFloat(0.001) {
wheelIsSpinning = false
// wheel has stopped
// add your code here
}
}