我很抱歉发布此消息,我知道之前有人问过,但我已尝试过以前的解决方案,似乎没有任何工作。
我遇到的问题是我使用了didBeginContact和didEndContact来确定我的精灵是否触地。
精灵有一个名为接地的BOOL ,用于确定此状态。基本上我想知道它在接触平台时是否在地面上。我得到的结果是在与平台建立联系然后立即失去联系之间的持续战斗,这非常令人沮丧。这就是我到目前为止所拥有的:
-(Player *)createPlayer
{
...
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
sprite.physicsBody.mass = 0.02;
sprite.physicsBody.restitution = 0;
sprite.physicsBody.categoryBitMask = playerCategory;
sprite.physicsBody.collisionBitMask = platformCategory;
sprite.physicsBody.contactTestBitMask = platformCategory;
sprite.physicsBody.usesPreciseCollisionDetection = YES;
return sprite;
}
-(SKSpriteNode *)createPlatform
{
SKSpriteNode *platform = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(100,25)];
platform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
platform.dynamic = NO;
platform.physicsBody.categoryBitMask = platformCategory;
platform.physicsBody.collisionBitMask = playerCategory;
platform.physicsBody.contactTestBitMask = playerCategory;
return platform;
}
-(void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody;
if(contact.bodyA.categoryBitMask < contact.B.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if((firstBody.categoryBitMask & playerCategory) != 0)
{
Player *player = firstBody.node;
if((secondBody.categoyBitMask & platformCategoy) != 0)
{
NSLog(@"Connection Established");
player.grounded = YES;
}
}
}
-(void)didEndContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody;
if(contact.bodyA.categoryBitMask < contact.B.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if((firstBody.categoryBitMask & playerCategory) != 0)
{
Player *player = firstBody.node;
if((secondBody.categoyBitMask & platformCategoy) != 0)
{
NSLog(@"Connection Lost");
player.grounded = NO;
}
}
}
基本上,当我检查日志时,最终结果如下:
提前感谢您提供的任何帮助,我完全被难过了。
答案 0 :(得分:-1)
-(void)didEndContact:(SKPhysicsContact *)contact
{
uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
if (collision != (playerCategory | platformCategory)) {
SKNode *node = contact.bodyA.node;
if (contact.bodyB.categoryBitMask == platformCategory) {
node = contact.bodyB.node;
}
NSLog(@"Connection Lost");
player.grounded = NO;
}