【问题标题】:SpriteKit didBeginContact not being calledSpriteKit didBeginContact 未被调用
【发布时间】:2014-12-21 03:32:14
【问题描述】:

我希望你能提供帮助,让一个方块穿过它并跳跃以避开障碍物并收集硬币。 它与障碍物的碰撞工作正常,下面的gameOver工作正常。

-(void)didBeginContact:(SKPhysicsContact *)contact
{
    if ([contact.bodyA.node.name  isEqualToString:@"coins"] || [contact.bodyB.node.name  isEqualToString:@"coins"]) {
        [self coinCollected];   //THIS IS NOT WORKING
        NSLog(@"contacted"); //THIS IS NOT WORKING
    }
    else if ([contact.bodyA.node.name  isEqualToString:@"ground"] || [contact.bodyB.node.name  isEqualToString:@"ground"]) {
        [hero land];
    }
    else {
        NSLog (@"dead");
        [self gameOver];
            [self runAction:[SKAction playSoundFileNamed:@"gameover.wav" waitForCompletion:NO]];
    }
}

我的 PMWorldGenerator 文件如下所示:

#import "PMWorldGenerator.h"
@interface PMWorldGenerator ()
@property double currentGroundX;
@property double currentObstacleX;
@property double coinX;
@property SKNode *world;

@end


@implementation PMWorldGenerator

static const uint32_t obstacleCategory = 0x1 << 1;
static const uint32_t groundCategory = 0x1 << 2;
static const uint32_t coinCategory = 0x1 << 3;

+ (id)generatorWithWorld:(SKNode *)world {
    PMWorldGenerator *generator = [PMWorldGenerator node];
    generator.currentGroundX = 0;
    generator.currentObstacleX  = 400;
    generator.coinX = 50;
    generator.world = world;
    return generator;
}

 -(void)populate
{
    for (int i = 0; i <3; i++)
        [self generate];
}


-(void)generate
{
    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(self.scene.frame.size.width, self.scene.frame.size.height/2.7)];
        ground.name = @"ground";
    ground.position = CGPointMake(self.currentGroundX, -self.scene.frame.size.height/2 + ground.frame.size.height/2);
    ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
    ground.physicsBody.categoryBitMask = groundCategory;
    ground.physicsBody.dynamic = NO;
    [self.world addChild:ground];
    self.currentGroundX += ground.frame.size.width;

    SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(10,10)];
        obstacle.name = @"obstacle";
    obstacle.position = CGPointMake(self.currentObstacleX/5, ground.position.y + ground.frame.size.height/2 + obstacle.frame.size.height/2 + 5);
    obstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle.size];
    obstacle.physicsBody.dynamic = NO;
    obstacle.physicsBody.categoryBitMask = obstacleCategory;
    [self.world addChild:obstacle];
    self.currentObstacleX += 550 ;

    SKSpriteNode *coins = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(4, 4)];
    coins.name = @"coins";
    coins.position = CGPointMake(self.coinX+80, ground.position.y + ground.frame.size.height/2 + obstacle.frame.size.height/2 + 25);
    coins.physicsBody.categoryBitMask = coinCategory;
    coins.physicsBody.dynamic = YES;
    SKAction *revolution = [SKAction rotateByAngle:M_PI_4*10 duration:3];
    SKAction *repeatRotate = [SKAction repeatActionForever:revolution];
    [coins runAction:repeatRotate];
    [self.world addChild:coins];
    self.coinX += 550;

}

最后是我的 PMHero 文件:

#import "PMHero.h"
@interface PMHero ()
@end


@implementation PMHero
static const uint32_t heroCategory = 0x1 << 0;
static const uint32_t obstacleCategory = 0x1 << 1;
static const uint32_t groundCategory = 0x1 << 2;
static const uint32_t coinCategory = 0x1 << 3;


+(id)hero
{
    PMHero *hero = [PMHero spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(12,12)];
    hero.name = @"hero";
    hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hero.size];
    hero.physicsBody.categoryBitMask = heroCategory;
    hero.physicsBody.contactTestBitMask = obstacleCategory | groundCategory | coinCategory;
    return hero;
}

我已经为 "coins" 的障碍物和地面所做的完全一样,但它在我的 didBeginContact 中没有检测到与它们的任何碰撞

【问题讨论】:

  • 如果你尝试记录身体的名字NSLog (@"%@",contact.bodyA.node.name);会得到什么
  • BodyA 英雄的障碍 @jammycoder
  • 所以硬币联系人根本不打电话给didBeginContact??或者它是否使 if 语句失败?
  • @jammycoder 不,其他 if 语句可以正常工作,只是不是硬币
  • 添加这个有用吗? coins.physicsBody.contactTestBitMask = heroCategory;

标签: ios objective-c sprite-kit collision


【解决方案1】:

你还没有为硬币添加物理体

SKSpriteNode *coins = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(4, 4)];
coins.name = @"coins";
coins.position = CGPointMake(self.coinX+80, ground.position.y + ground.frame.size.height/2 + obstacle.frame.size.height/2 + 25);

coins.physicsBody = [SKPhysicsBody bodyWith...//need code here

coins.physicsBody.categoryBitMask = coinCategory;
coins.physicsBody.dynamic = NO;
coins.physicsBody.collisionBitMask = 0;
SKAction *revolution = [SKAction rotateByAngle:M_PI_4*10 duration:3];
SKAction *repeatRotate = [SKAction repeatActionForever:revolution];
[coins runAction:repeatRotate];
[self.world addChild:coins];

【讨论】:

  • 上面试过了,但我担心硬币现在掉在地上,英雄无法穿过它,接触它仍然不会导致调用didBegincontact。跨度>
  • 你需要硬币是动态的吗??
  • 现在似乎正在建立联系。唯一的问题是硬币是固体,我怎样才能穿过它而不是撞到它?
  • 我在使用 spritekit 时总是遇到这个问题,它往往需要大量的试验和错误,才能把东西放在正确的地方。尝试在两个身体上将碰撞位掩码设置为 0(不是零,正如我之前所说)
  • 谢谢@jammycoder,我终于明白了。英雄碰撞Bitmast = groundCategory |障碍类别;和coins.physicsBody.collisionsBitMask = 0;和动态 0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多