我正在编写的游戏中使用 Sprite Kit 的内置物理引擎。为了检查一个角色是否在地上,我使用's的bodyAlongRayStart()
功能来检查角色左下角和右下角下面的身体(这个函数属于角色的类) :SKScene
physicsWorld
func isInAir(sceneRef:GameScene) -> Bool {
var isInAir:Bool = false
let distance:CGFloat = 32 // how far from the origin we should check
// define where the rays start and end
let bottomLeftRayStart:CGPoint = CGPoint(x: self.position.x - (self.hitbox.size.width/2), y: self.position.y - (self.hitbox.size.height/2))
let bottomLeftRayEnd:CGPoint = CGPoint(x: self.position.x - (self.hitbox.size.width/2), y: (self.position.y - (self.hitbox.size.height/2) - distance))
let bottomRightRayStart:CGPoint = CGPoint(x: self.position.x + (self.hitbox.size.width/2), y: self.position.y - (self.hitbox.size.height/2))
let bottomRightRayEnd:CGPoint = CGPoint(x: self.position.x + (self.hitbox.size.width/2), y: (self.position.y - (self.hitbox.size.height/2) - distance))
// Are there any bodies along the lines?
var bottomLeftBody:SKPhysicsBody! = sceneRef.physicsWorld.bodyAlongRayStart(bottomLeftRayStart, end:bottomLeftRayEnd)
var bottomRightBody:SKPhysicsBody! = sceneRef.physicsWorld.bodyAlongRayStart(bottomRightRayStart, end:bottomRightRayEnd)
// no bodies found
if bottomLeftBody == nil && bottomRightBody == nil {
isInAir = true
} else {
// if one of the rays finds a wall (floor) or slope, we're not in the air
if (bottomLeftBody != nil && (bottomLeftBody!.categoryBitMask == _entityType.wall.rawValue || bottomLeftBody!.categoryBitMask == _entityType.slope.rawValue)) || (bottomRightBody != nil && (bottomRightBody.categoryBitMask == _entityType.wall.rawValue || bottomRightBody!.categoryBitMask == _entityType.slope.rawValue)) {
isInAir = false
}
}
return isInAir
}
这段代码在场景的update()
函数中被调用(虽然我应该把它移到didSimulatePhysics()
......)并且它工作正常!然而,令人讨厌的是,Swift 以其无限的智慧决定在Chance
每次检测到沿射线的物体时打印到控制台。
场景中只有一个角色并不太麻烦,但我很快就会添加敌人(他们的工作方式大致相同,但处理他们的动作会有不同的功能),所有这些println()
调用都会减慢模拟器的速度对了。我看不出有办法抑制这种情况;调用来自我无法更改的类库。
是否有可靠的方法来覆盖标准println()
功能?像这样的东西:
func println(object: Any) {
#if DEBUG
Swift.println(object)
#endif
}
如果是这样,我应该把它放在我的项目中的什么地方?它目前在,但即使我更改标志,AppDelegate.swift
其他库(和函数)仍在打印到控制台......bodyAlongRayStart()