Search code examples
iosswiftcocos2d-iphonecocos2d-x-3.0chipmunk

Cocos2d iPHone SDK ccPhysicsCollisionPreSolve crashing while accessing object properties


I am working on Cocos2d iphone SDK and stuck with an issues. Check my code here.

Obstacle Class

@objc class Obstacle: CCNode {
    weak var __pipe: CCSprite!
    var ignoreCollision:Bool = false

    override init!() {
        super.init()
        //NSLog("init plain")
        userInteractionEnabled = true
        ignoreCollision = false
    }

    func didLoadFromCCB() {
        ...
    }
}

The main scene where I have placed collision delegate methods. The method is called once the player object collides with obstacle object.

func ccPhysicsCollisionPreSolve(pair: CCPhysicsCollisionPair!, hero: Player!, platform: Obstacle!) -> ObjCBool {
    if !isGameOn {
        NSLog("PLATFORM: Game finished")
        return false
    }

    if platform.ignoreCollision {
        platform.ignoreCollision = !platform.ignoreCollision
        // For score updates
        hudLayer.updatePlatform(++scorePlatforms)
    }

    return true
}

Now here, I am just trying to use simple Bool property from platform object and what I get is a crash. My app crashes on the if... condition statement where I am using that property. I am unable to get what is with this as I am simply using a property from object.

I checked the object and found platform shows me of type Some instead ob Obstacle. I have tried using

var p: Obstacle = platform as Obstacle

and replaced all platform with p but yet I am facing the crash. I thought the type now shows me some random hex number which might be the issue.

Can anyone help me here as I am unable to find out how I should access property from this platform object in ccPhysicsCollisionPreSolve method?


Solution

  • Sorry guys for the trouble but it was my mistake. I was understanding the same incorrectly.

    The Obstacle class represents the platform as well as its background layer having tripple height of the device screen. But my ball collides only with that __pipe sprite in Obstacle class and I am referring the whole Obstacle class which is wrong.

    I used platform.parent!.ignoreCollision and problem is solved. :) This little miss costed me 3-4 days of R&D and work extra.