1

我创建了两个类:一个处理射弹和主场景本身。当我在 didMoveToView 或任何其他方法中调用类方法来生成弹丸时,它可以工作并且我能够看到弹丸。但是,当我希望弹丸在接触后产生时,它不起作用。我之前测试过触点,一切正常。我在这里想念什么?

didBeginContact 方法

static inline Enemies *nodeFromBody(SKPhysicsBody *body1, SKPhysicsBody *body2, uint32_t category) {
    Enemies *node = nil;
    if (body1.categoryBitMask & category) {
        node = (Enemies *)body1.node;
    }
    else if (body2.categoryBitMask & category) {
        node = (Enemies *)body2.node;
    }
    return node;
}

-(void)didBeginContact:(SKPhysicsContact *)contact {

    SKPhysicsBody *firstBody, *secondBody;

    SKSpriteNode *projectile = nil;
    SKSpriteNode *offScreen = nil;
    Enemies *target = nil;

    firstBody = contact.bodyA;
    secondBody = contact.bodyB;

    projectile = nodeFromBody(firstBody, secondBody, projectileCategory);
    offScreen = nodeFromBody(firstBody, secondBody, offScreenCategory);
    target = nodeFromBody(firstBody, secondBody, targetsCategory);

    if (projectile && target) {

        NSLog(@"firstShot");
        [projectile removeFromParent];
        // Below is the class method being invoked.
        [ProjectilePatterns pattern1:self spawnPoint:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))];
        [target removeFromParent];
    }
}

类方法

+(ProjectilePatterns *)pattern1:(SKScene *)scene spawnPoint:(CGPoint)location {

    ProjectilePatterns *patternBomb = [ProjectilePatterns spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(20, 20)];
    patternBomb.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:patternBomb.size];
    patternBomb.physicsBody.categoryBitMask = projectileCategory;
    patternBomb.physicsBody.contactTestBitMask = targetsCategory | offScreenCategory;
    patternBomb.physicsBody.collisionBitMask = 0;
    patternBomb.physicsBody.friction = 0;
    patternBomb.physicsBody.linearDamping = 0;
    patternBomb.position = CGPointMake(location.x + 20, location.y);
    patternBomb.name = @"p1";
    NSLog(@"works");

    [scene addChild:patternBomb];
    [patternBomb.physicsBody applyImpulse:CGVectorMake(3, 0)];

    return patternBomb;
}
4

0 回答 0