Ios 碰撞检测不工作?

Ios 碰撞检测不工作?,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,我正在尝试为iOS制作一个非常简单的SpriteKit应用程序,但有一个问题: #import "MyScene.h" static const uint32_t laserCategory = 0x1 << 0; static const uint32_t enemyCategory = 0x1 << 0; @implementation MyScene - (id) initWithSize: (CGSize) size { if (self = [su

我正在尝试为iOS制作一个非常简单的SpriteKit应用程序,但有一个问题:

#import "MyScene.h"

static const uint32_t laserCategory = 0x1 << 0;
static const uint32_t enemyCategory = 0x1 << 0;

@implementation MyScene

- (id) initWithSize: (CGSize) size
{
    if (self = [super initWithSize: size])
    {
        self.backgroundColor = [SKColor colorWithRed: 1.0 green: 1.0 blue: 1.0 alpha: 1.0];
        self.physicsWorld.gravity = CGVectorMake(0,0);

        SKSpriteNode *ship = [SKSpriteNode spriteNodeWithImageNamed: @"Ship"];
        ship.position = CGPointMake (CGRectGetMidX (self.frame), 50);
        [self addChild: ship];

        SKSpriteNode *enemy = [SKSpriteNode spriteNodeWithImageNamed: @"Enemy"];
        enemy.position = CGPointMake (CGRectGetMidX (self.frame), 440);
        [self addChild: enemy];

        enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: enemy.size];
        enemy.physicsBody.dynamic = YES;
        enemy.physicsBody.categoryBitMask = enemyCategory;
        enemy.physicsBody.contactTestBitMask = laserCategory;
        enemy.physicsBody.collisionBitMask = 0;

        SKAction *moveEnemy = [SKAction moveByX: 0.0 y: -500.0 duration: 5.0];
        [enemy runAction: moveEnemy];
    } return self;
}

- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
    SKSpriteNode *laser = [SKSpriteNode spriteNodeWithImageNamed: @"Laser"];
    laser.position = CGPointMake (CGRectGetMidX (self.frame), 100);
    [self addChild: laser];

    laser.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: laser.size];
    laser.physicsBody.dynamic = YES;
    laser.physicsBody.categoryBitMask = laserCategory;
    laser.physicsBody.contactTestBitMask = enemyCategory;
    laser.physicsBody.collisionBitMask = 0;

    SKAction *moveLaser = [SKAction moveByX: 0.0 y: 2000.0 duration: 5.0];
    [laser runAction: moveLaser];
}

- (void) laser: (SKSpriteNode *) laser didCollideWithEnemy: (SKSpriteNode *) enemy
{
    [laser removeFromParent];
    [enemy removeFromParent];
}

- (void) didBeginContact: (SKPhysicsContact *) contact
{
    SKPhysicsBody *firstBody, *secondBody;

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else
    {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }

    if ((firstBody.categoryBitMask & laserCategory) != 0 && (secondBody.categoryBitMask & enemyCategory) != 0)
    {
        [self laser: (SKSpriteNode *) firstBody.node didCollideWithEnemy: (SKSpriteNode *) secondBody.node];
    }
}

@end
#导入“MyScene.h”

静态常数uint32\u t laserCategory=0x1您已为两个不同类别设置了相同的位掩码:

static const uint32_t laserCategory = 0x1 << 0;
static const uint32_t enemyCategory = 0x1 << 0;
我假设您已将委托声明添加到MyScene类:

<SKPhysicsContactDelegate>

谢谢您的输入,但我试过了,但没有成功:(
static const uint32_t laserCategory = 0x1 << 0;
static const uint32_t enemyCategory = 0x1 << 1;
self.physicsWorld.gravity = CGVectorMake(0,0);
self.physicsWorld.contactDelegate = self;
<SKPhysicsContactDelegate>