我得到了两个对象,一个Human和一个Block。如果人类在区块上,他可以跳跃,如果他在空中,他就不是。我该如何编码,因为CGRectIntersectsRect在SpriteKit中对我不起作用。
-(void)触摸Began:(NSSet*)触摸withEvent:(UIEvent)事件{/触摸开始时调用*/
Human.physicsBody.velocity = CGVectorMake(0, 0);
[Human.physicsBody applyImpulse:CGVectorMake(0, 40)];
}
我已经在分类中得到了Block和Human,用于碰撞检测:
else if ((firstBody.categoryBitMask & HumanCategory) != 0 &&
(secondBody.categoryBitMask & BlockCategory) != 0)
{
}
我应该以某种方式将其用于代码吗?谢谢你的帮助。
为了防止在不接触地面时跳跃,请使用bodyAtPoint:检测节点正下方是否有任何物理体。
如果你的角色下可能有多个物理体,而这些物理体可能不相关,你可以使用enumerateBodiesAtPoint:usingBlock:来确保你只允许在物理体关联节点是Block时跳跃。
更新:
这些方法在SKPhysicsWorld类中可用,您可以通过场景中的physicsWorld属性访问实例。
//Please note that this point is in the scenes coordinate system
CGPoint thePointDirectlyBelowMyNode = ...;
[scene.physicWorld bodyAtPoint:thePointDirectlyBelowMyNode];
在我的游戏中,我使用intersectsNode
来确定两个节点是否相交:
if ( [node1 intersectsNode:node2] )
希望这能帮助
实际上,方法是- (SKNode *)nodeAtPoint:(CGPoint)p;
因此使用[self nodeAtPoint: pointBelowHuman];