简体   繁体   中英

SpriteKit - Detect when two SKShapeNodes “pass” each other

It's might look long but most of it is the same code :P hope i did well explaining my self :) i hope i could explain my self. This is the case : i have a SKShapeNode lets call him X. This is how i declared X( ONLY ONCE IN didMoveToView ):

 var X = SKShapeNode(circleOfRadius: 15)
        X.position = CGPointMake(100  , 100)
        X.fillColor = SKColor.yellowColor()
        X.physicsBody = SKPhysicsBody(circleOfRadius: coin.frame.width/2)
        X.physicsBody?.dynamic = true
        X.physicsBody?.linearDamping = 1        
        X.physicsBody?.affectedByGravity = false
        X.physicsBody?.categoryBitMask = coincategory
        X.physicsBody?.contactTestBitMask = coincategory
        self.addChild(coin)

Now my second SKShapeNode , lets call him Y is a little tricky,for my Game reasons, i need his size to be dynamic , so this what i did and it's working , First, Declared him in didSimulatePhysics() like this :

 override func didSimulatePhysics() {
        let pixel = self.childNodeWithName(X.name!)
        var pathtodraw : CGMutablePathRef!
        pathtodraw = CGPathCreateMutable()
        CGPathMoveToPoint(pathtodraw, nil , pixel!.position.x, pixel!.position.y)
        CGPathAddLineToPoint(pathtodraw, nil, 0, 0)
        CGPathCloseSubpath(pathtodraw)
        Y.path = pathtodraw

        self.addChild(Y)

{

And than for his size to be dynamic ( the game include movment of X) , i remove him everytime inside the update method , so that the path could keep update itself:

 override func update(currentTime: NSTimeInterval) {


        Y.removeFromParent()


}

What i am trying to achieve : every time object X pass over Y println("They Just Passed each other"), i dont want them to react to the contact , or bounch from each other , they should be invisible to each other , but i need to detect when they pass each other.

What is the problem : When i'm adding Physics to Y , the node dosent delete him self , he keep generating more nodes and fill the screen with him self. and if left it the way it his above, it's works perefectly , it delete him self and update the size and always keep it only 1 node. this is what tried adding to Y :

override func didSimulatePhysics() {
        let pixel = self.childNodeWithName(X.name!)
        var pathtodraw : CGMutablePathRef!
        pathtodraw = CGPathCreateMutable()
        CGPathMoveToPoint(pathtodraw, nil , pixel!.position.x, pixel!.position.y)
        CGPathAddLineToPoint(pathtodraw, nil, 0, 0)
        CGPathCloseSubpath(pathtodraw)
        Y.path = pathtodraw

        self.addChild(Y)

        **linenode = SKShapeNode(path: pathtodraw)
        linenode.physicsBody = SKPhysicsBody(edgeChainFromPath: pathtodraw)**

      {

It Keep generates alot of nodes and dosent delete them . Any idea what can i do to accomplish that? Thanks guys !

Since a shape node's path can be updated without removing the node from the scene and then adding it back, I suggest you add Y to the scene in didMoveToView and delete the addToChild and removeFromParent in the didSimulatePhysics and update methods. Also, add the physics body to Y instead of creating a new shape node. Lastly, you are creating X and then adding coin to the scene.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM