2

I'm using Sprite Kit's built-in physics engine in a game I'm writing. In order to check whether or not a character is on the ground, I'm using the bodyAlongRayStart() function of the SKScene's physicsWorld to check for bodies underneath the bottom-left and bottom-right corners of the character (this function belong's to the character's class):

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
}

This code is called in the scene's update() function (though I should probably move it to didSimulatePhysics()...) and it works fine! What's annoying, however, is that Swift in its infinite wisdom decides to print Chance to the console every time it detects a body along the ray.

With only one character in the scene it's not too much of a bother, but I'll be adding enemies soon (who work in much the same way, but will have different functions for handling their movement) and all those println() calls will slow the simulator right down. I can't see a way to suppress this; the call is coming from the class library which I can't alter.

Is there a reliable way to override the standard println() function, gloabally? Something like this:

func println(object: Any) {
    #if DEBUG
        Swift.println(object)
    #endif
}

And if so, where in my project should I put it? It's currently in AppDelegate.swift but other libraries (and the bodyAlongRayStart() function) are still printing to the console even when I change the flag...

MassivePenguin
  • 3,701
  • 4
  • 24
  • 46

1 Answers1

3

I submitted this bug to Apple. They told me it's fixed in the next version of Xcode. If it's really bugging you, you can download Xcode 6.3 beta.

hamobi
  • 7,940
  • 4
  • 35
  • 64