根据我的理解,您希望一个对象有时会穿过一个对象。
以下代码应该可以工作:
enum CategoryBitMasks{
case player1Collidable = 1 << 0
case player2Collidable = 1 << 1
case player1 = 1 << 2
case player2 = 1 << 3
case oneTimePass = 1 << 4
case unstoppable = 1 << 5
case player = CategoryBitMasks.player1 | CategoryBitMasks.player2
case playerCollidable = CategoryBitMasks.player1Collidable | CategoryBitMasks.player2Collidable
}
//Create an obstacle with category (.player1Collidable | .player2Collidable)
// with contact 0
// with collision 0
//Create a player with category (.player1 | .unstoppable)
// with contact .player1Collidable
// with collision .player1Collidable
func didBeginContact(contact: SKPhysicsContact) {
var obstacle = contact.bodyA.categoryBitMask >= contact.bodyB.categoryBitMask ?contact.bodyA, contact.bodyB
var player = contact.bodyB == obstacle ? contact.bodyA, contact.bodyB
//Only allows the player to pass through 1 object
if player.categoryBitMask || CategoryBitMasks.oneTimePass && obstacle.categoryBitMask || CategoryBitMasks.playerCollidable {
obstacle.categoryBitMask &= ~(player.collisionBitMask & CategoryBitMasks.playerCollidable) //turns player1Collidable off
player.categoryBitMask &= ~(player.categoryBitMask & CategoryBitMasks.oneTimePass) //turns oneTimePass off
}
//Allows player to pass through all objects
if player.categoryBitMask || CategoryBitMasks.unstoppable && obstacle.categoryBitMask || CategoryBitMasks.playerCollidable {
//This will bypass all objects providing their collision is not set to the player
player.collisionBitMask &= ~(player.collisionBitMask & CategoryBitMasks.playerCollidable) //turns player1Collidable off
//This will bypass all objects, but is dangerous because the category is now changed, meaning if a contact does happen, .player can't be used
//player.categoryBitMask &= ~(player.categoryBitMask & CategoryBitMasks.player ) //turns player1Collidable off
}
}
记得在需要时重置面具
这将允许玩家 1 根据您给它的类别穿过对象,同时强制玩家 2 仍然被同一个对象阻挡。
希望这些示例将帮助您调整游戏的需求。