【发布时间】:2017-12-30 04:53:33
【问题描述】:
我控制了游戏,当你触摸时,角色飞向左侧,第二次触摸时角色飞向右侧,但是当角色在飞行中并且我多次触摸时,角色抛出在不同的方向,他飞起来,如何解决它
我希望玩家在飞行时不产生冲动
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
}
}
【问题讨论】:
-
1) 永远不要使用图像来共享代码,SO 旨在将代码格式化以便人们可以使用它。 2)你正在做动作和物理,这真的让两个引擎都搞砸了。二选一
标签: ios swift sprite-kit touchesbegan