有一个硬币,它只能与玩家碰撞而不是推动它。相反,玩家(手指)被推下并与所有物体碰撞。这是代码:

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)

这是播放器的设置:
Finger.physicsBody?.collisionBitMask=0;
Finger.physicsBody?.categoryBitMask=0;
Finger.physicsBody?.contactTestBitMask=1

最佳答案

问题在于硬币的碰撞位掩码设置为玩家的categoryBitMask。让我为您揭开这三个碰撞变量的神秘面纱:

x.categoryBitMask:这是一种为对象“x”提供识别它的方法。

x.collisionBitMask:在这里,您将放置您希望“x”能够与之碰撞的对象的categoryBitMask变量(崩溃)

x.contactTestBitMask:在这里,您将放置当对象与“x”进行接触时要对其执行操作的对象的categoryBitMask var。 contactTestBitMask将调用苹果提供的函数“didBeginContact”,并且您可以在其中编写当硬币碰到玩家时想要发生的事情。

一些代码可以帮助您将各个部分组合在一起:

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)

和:
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

祝好运!

关于ios - 位掩码不起作用。 SpriteKit Swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28786054/

10-08 20:56