1

I created two classes: one to handle projectiles and the main scene itself. When I call the class method to spawn projectiles, in the didMoveToView or any other method, it works and I'm able to see the projectiles. However, when I want the projectiles to spawn after a contact, it does not work. I tested the contacts before and everything is working properly. What am I missing here?

didBeginContact Method

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];
    }
}

The Class Method

+(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;
}
0x141E
  • 12,613
  • 2
  • 41
  • 54
Blank
  • 113
  • 9
  • What am I missing here? You can't say that everything is working properly when you do have an issue. This "everything" I is what I like to hear more about, specifically which parts of the methods run and which don't? Simple breakpoint and logging will tell. Seeing that there is a log, what more can you say about where it does go wrong? – CodeSmile Oct 29 '14 at 23:20
  • @LearnCocos2D The method works exactly the way I want it to, it creates and spawns a new "bomb" that spawns on screen, no matter where I call the method(except didBeginContact)it does the same exact effect. I just revised the code a bit after I read your comment, I added an NSLog, and apparently that method is indeed invoked, however the object itself is not spawned. – Blank Oct 30 '14 at 00:46
  • is the self object that you're passing to pattern1 a SKScene ? – shaish Oct 30 '14 at 01:10
  • @shaish Yep, now I'm gonna add filler comment because of character length requirements. – Blank Oct 30 '14 at 01:43
  • 2
    Try moving patternBomb.position = ... above the statement that creates the physicsBody. – 0x141E Oct 30 '14 at 07:27
  • @0x141E It works fabulously, thank you! – Blank Oct 30 '14 at 10:07

0 Answers0