简体   繁体   English

碰撞检测不起作用?

[英]Collision detection not working?

I'm trying to make a very simple SpriteKit app for iOS, but there's a problem: 我正在尝试为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

This is my "MyScene.m" file, and everything works just fine. 这是我的“MyScene.m”文件,一切正常。 The only thing that doesn't work is the collision detection between the laser object and the enemy object. 唯一不起作用的是激光对象和敌方物体之间的碰撞检测。 They just pass right through each other. 他们只是穿过对方。 Any solutions? 有解决方案吗 Any other tips are welcome, as well. 欢迎任何其他提示。

Thanks. 谢谢。

You have set the same bit mask for two different category: 您为两个不同的类别设置了相同的位掩码:

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

After that the if statement in didBeginContact: method is wrong: 之后,didBeginContact:方法中的if语句错误:

if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)

you should change one of those to 0x1 << 1: 你应该将其中一个更改为0x1 << 1:

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

//Extended //扩展

You are also missing set up contact delegate to phasic world, add this code after you set up gravity in initWithSize: 您还缺少设置联系委托给phasic world,在initWithSize中设置gravity之后添加此代码:

self.physicsWorld.gravity = CGVectorMake(0,0);
self.physicsWorld.contactDelegate = self;

I assume you added delegate declaration to MyScene class: 我假设您向MyScene类添加了委托声明:

<SKPhysicsContactDelegate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM