简体   繁体   English

位掩码不起作用。 SpriteKit Swift

[英]Bit mask not working. SpriteKit Swift

There is a coin and it is only supposed to collide with the player and not push it. 有一个硬币,它只能与玩家碰撞而不是推动它。 Instead the player(finger) gets pushed down and it collides with everything. 相反,玩家(手指)被推下并与所有物体碰撞。 Here is the code: 这是代码:

var coin=SKSpriteNode(imageNamed: "Coin")
coin.position=CGPoint(x: xPos, y: yPos)
coin.physicsBody=SKPhysicsBody(circleOfRadius: 250)
//coin.physicsBody!.mass=CGFloat(mass);
coin.xScale=1
coin.yScale=1

coin.name="coin";
coin.physicsBody?.contactTestBitMask=1
coin.physicsBody!.collisionBitMask=0
coin.physicsBody!.categoryBitMask=0

self.addChild(coin)

Here is the player's settings: 这是播放器的设置:

Finger.physicsBody?.collisionBitMask=0;
Finger.physicsBody?.categoryBitMask=0;
Finger.physicsBody?.contactTestBitMask=1

The problem is that the collisionBitMask of the coin is set to the categoryBitMask of the player. 问题在于硬币的碰撞位掩码设置为玩家的categoryBitMask。 Let me demystify the three collision variables for you: 让我为您揭开这三个碰撞变量的神秘面纱:

x.categoryBitMask: This is a way of giving an object "x" a way to identify it. x.categoryBitMask:这是一种为对象“ x”提供识别它的方法。

x.collisionBitMask: Here you will place the categoryBitMask var of the objects you want "x" to be able to COLLIDE with (crash into) x.collisionBitMask:在这里,您将放置您希望“ x”能够与之碰撞的对象的categoryBitMask变量(崩溃)

x.contactTestBitMask: Here you will place the categoryBitMask var of the object you want to do something for when they make contact with "x". x.contactTestBitMask:在这里,您将放置当对象与“ x”进行接触时要为其执行操作的对象的categoryBitMask var。 The contactTestBitMask will call the apple provided function "didBeginContact", and in there you could code what you want to happen when say the coin touches the player. contactTestBitMask将调用苹果提供的函数“ didBeginContact”,并且您可以在其中编写当硬币碰到玩家时想要发生的事情。

Some code to help you put the pieces together: 一些代码可以帮助您将各个部分组合在一起:

var coinCategory: UInt32 = 1 // categories must be type UInt32
var playerCategory: UInt32 = 2
var nothingCategory: UInt32 = 3 // some other number for not colliding into anything

coin.name="coin";
coin.physicsBody!.categoryBitMask= coinCategory // set category first
coin.physicsBody?.contactTestBitMask = playerContact
coin.physicsBody!.collisionBitMask = nothingCategory // doesn't crash into anything. 
// if you want coins to crash into each other then set their collisionBitMask to the coinCategory  


self.addChild(coin)

and: 和:

Finger.physicsBody?.categoryBitMask= playerCategory;
Finger.physicsBody?.collisionBitMask= nothingCategory;

// This next one only needs to be set on one of the objects when you want the "didBeginContact" function to be called when there is contact
//Finger.physicsBody?.contactTestBitMask = coinCategory

Good Luck! 祝好运!

And it is best practice to not use integer values for the bit masks: 最佳实践是不对位掩码使用整数值:

var coinCategory: UInt32 = 0x1 << 1 
var playerCategory: UInt32 = 0x1 << 2
var nothingCategory: UInt32 = 0x1 << 3 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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