Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Need help running an action between three categories

I am currently at a loss on to figuring out how to implement an action when certain requirements occur.

The situation is that I have three categories, the player category, the enemy category, and the detection category. I created a method which creates the enemy with it having the enemy category, as well as a invisible contact line attached to the enemy through an SKPhysicsJointFixed method with the detection category.

If the player makes contact with the detection line, the enemy is supposed to run an action which moves it towards the player, however I am having trouble figuring out on how to implement such a thing.

Any information would be great!

// Returns a node that matches the specified category.
static inline SKSpriteNode *nodeFromBody(SKPhysicsBody *body1, SKPhysicsBody *body2,    uint32_t category) {
SKSpriteNode *node = nil;
if (body1.categoryBitMask & category) {
    node = (SKSpriteNode *)body1.node;
}
else if (body2.categoryBitMask & category) {
    node = (SKSpriteNode *)body2.node;
}
return node;
}

The contact method

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

SKPhysicsBody *firstBody, *secondBody;
SKSpriteNode *player = nil;
SKSpriteNode *enemy = nil;
SKSpriteNode *triggerBody = nil;

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

player = nodeFromBody(firstBody, secondBody, fPlayerCategory);
enemy = nodeFromBody(firstBody, secondBody, fEnemyCategory);
triggerBody = nodeFromBody(firstBody, secondBody, fTriggerLineCategory);

if (player && enemy) {

    gameOver = YES;
    if (currentScore > bestScore) {
        bestScore = currentScore;
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:bestScore] forKey:@"highScore"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

if (player && triggerBody) {

[triggerBody runAction:removeFromParent];
[enemy runAction:Ignition];

    }
}

Comments