当您触摸时,我控制了游戏,角色飞向左侧,第二次触摸时,角色飞向右侧,但是当角色在飞行中并且我多次触摸时,角色向不同的方向投掷,他飞起来,如何解决它
我希望玩家飞行时冲动不起作用
enum BodyType: UInt32 {
case playerr = 1
case walll = 2
}
var direction: Direction?
var isInFlight = true
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent? {
if isInFlight == true {
return
} else {
player?.removeAllActions()
switch direction {
case .left:
player?.run(SKAction.moveBy(x: 700, y: 0, duration: 1))
player?.physicsBody?.applyImpulse(CGVector(dx: 700, dy: 500))
direction = .right
case .right:
player?.run(SKAction.moveBy(x: -700, y: 0, duration: 1))
player?.physicsBody?.applyImpulse(CGVector(dx: -700, dy:
500))
direction
}
}
func didBegin(_ contact: SKPhysicsContact) {
let firstBody: SKPhysicsBody
let secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if ( firstBody.categoryBitMask == BodyType.playerr.rawValue && secondBody.categoryBitMask == BodyType.walll.rawValue) {
print("Add")
isInFlight = false
}
}
func didEnd(_ contact: SKPhysicsContact) {
let firstBody: SKPhysicsBody
let secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if (firstBody.categoryBitMask == BodyType.playerr.rawValue && secondBody.categoryBitMask == BodyType.walll.rawValue) {
print("End")
isInFlight = true
}
}
最佳答案
不确定您要使用应用程序完成什么操作,但这是我的意思,这是通过使用isInFlight
和touchesBegan
将touchesEnded
bool设置为true / false来实现的。这个概念很简单,但是您可以根据自己的方式在应用逻辑中实现它。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
isInFlight = false
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
isInFlight = true
}
关于ios - 如何消除无数次冲动(SpriteKit),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48031081/