Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在精灵套件游戏中同时检测坠落和跳跃_Ios_Swift_Sprite Kit_Sprite - Fatal编程技术网

Ios 在精灵套件游戏中同时检测坠落和跳跃

Ios 在精灵套件游戏中同时检测坠落和跳跃,ios,swift,sprite-kit,sprite,Ios,Swift,Sprite Kit,Sprite,我正在制作一款精灵套件/雨燕游戏。我希望球在落下和跳跃时做不同的事情。现在,如果它落下,它也会使用底部的代码(跳跃代码) 以下是我正在使用的代码: override func collisionWithBall(ball: SKNode) -> Bool { if ball.physicsBody?.velocity.dy < 0 { ball.physicsBody?.velocity = CGVector(dx: ball.physics

我正在制作一款精灵套件/雨燕游戏。我希望球在落下和跳跃时做不同的事情。现在,如果它落下,它也会使用底部的代码(跳跃代码)

以下是我正在使用的代码:

override func collisionWithBall(ball: SKNode) -> Bool {

        if ball.physicsBody?.velocity.dy < 0 {
            ball.physicsBody?.velocity = CGVector(dx: ball.physicsBody!.velocity.dx, dy: 500.0)

            if platformType == .Normal {
                self.removeFromParent()
            }

            if platformType == .Break {
                self.removeFromParent()
                ball.physicsBody?.velocity = CGVector(dx: ball.physicsBody!.velocity.dx, dy: 800.0)
            }
        }
        if ball.physicsBody?.velocity.dy > 0 {
        if platformType == .Break {
            NSUserDefaults.standardUserDefaults().setInteger(1, forKey: "nextValue")
            self.removeFromParent()
            ball.physicsBody?.velocity = CGVector(dx: ball.physicsBody!.velocity.dx, dy: -800.0)
        }
        }

        return false
    }
override func collisionWithBall(ball:SKNode)->Bool{
如果ball.physicsBody?.velocity.dy<0{
ball.physicsBody?.velocity=CGVector(dx:ball.physicsBody!.velocity.dx,dy:500.0)
如果platformType=.Normal{
self.removeFromParent()
}
如果平台类型==.Break{
self.removeFromParent()
ball.physicsBody?.velocity=CGVector(dx:ball.physicsBody!.velocity.dx,dy:800.0)
}
}
如果ball.physicsBody?.velocity.dy>0{
如果平台类型==.Break{
NSUserDefaults.standardUserDefaults().setInteger(1,forKey:“nextValue”)
self.removeFromParent()
ball.physicsBody?.velocity=CGVector(dx:ball.physicsBody!.velocity.dx,dy:-800.0)
}
}
返回错误
}
如果它跳跃,我希望它只使用这个If ball.physicsBody?.velocity.dy>0,当它落下时,我只想使用这个If ball.physicsBody?.velocity.dy<0


如何使其工作?

对于第二个条件,您必须更改if语句以使用
else if
。像这样:

if ball.physicsBody?.velocity.dy < 0 {
    // Handle falling here...
} else if if ball.physicsBody?.velocity.dy > 0 {
    // Handle jumping here...
}
如果ball.physicsBody?.velocity.dy<0{
//把手落在这里。。。
}否则如果ball.physicsBody?.velocity.dy>0{
//在这里处理跳跃。。。
}

如果您的dy>0,则需要执行其他操作,或者在if中返回。另一方面,你的速度是<0,然后你把它改成500,也就是>0,你就陷入了第二种状态。现在它工作得很好。