简体   繁体   中英

Temporarily deactivate physical collision between two Nodes(Swift 2, IOS)

I'm using SpriteKit , and I'm having trouble getting one of my objects to shoot out another object, without colliding with it at the moment that it shoots it. However, as soon as they are no longer within range of contact each-other, I want them to be able to contact with each-other for the rest of the game.

Here is what I have tried-

    var allCategory: UInt32  = 1;
    var nillCategory: UInt32  = 2;
    var bufferNode: SKNode?

    bufferNode = self.childNodeWithName("player")
    bufferNode!.physicsBody!.collisionBitMask = nillCategory;
    bufferNode!.physicsBody!.categoryBitMask = nillCategory;
    shootNewPlayer(touchLocation)
    runAction(SKAction.sequence([SKAction.waitForDuration(1),SKAction.runBlock(removeBuffer)]))
}
func removeBuffer(){
    bufferNode!.physicsBody!.collisionBitMask = allCategory;
    bufferNode!.physicsBody!.categoryBitMask = allCategory;
}

By default, all my objects in the scene have a collisionBitMask and categoryBitMask of "allCategory." My solution was to temporarily change its categories to nill. This had absolutely no affect. I also want to avoid this solution since for 1 second the player would no longer interact with objects, which could cause bugs (With the code above it still interacts).

Any ideas on how I can get the object to shoot a new player without it flying off in a bazaar direction? Thanks!

Turn off object2 collision by default. (Do this with collisionBitMask = 0)

Enable object contactBitMask with the flag for object1.

On the didEndContact Method, enable the collision object2 for object1 when the condition of object1 contacting object2 is met.

This will allow you to avoid things like timers and checking the update loop constantly.

In English, you are saying: When object 2 no longer is touching object 1, object 2 is now able to collide with object 1.

Depending on circumstance, you may want to remove the contact test after you enable collision

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