Moving Computer Player in Xcode

Posted by user1631497 on Stack Overflow See other posts from Stack Overflow or by user1631497
Published on 2012-08-28T21:32:49Z Indexed on 2012/08/28 21:38 UTC
Read the original article Hit count: 136

I am working on a 2d game using xcode and objective-c. I am trying to make my enemy player move left if I am left of him, until he is touching my character. The same will go when I am right of him. Basically just trying to get him to move towards me. But my code to do so is not working. So I have my code that says the following

-(void)moveTowardsCharacter
{
    enemyLeftTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(goEnemyLeft) userInfo:nil repeats:YES];
    if (enemyLeftTimer == nil){
        enemyLeftTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(goEnemyLeft) userInfo:nil repeats:YES];
    }
}

-(IBAction)enemyStopLeft
{
    if (CGRectIntersectsRect(bluebox.frame, redbox.frame)) {
        [enemyLeftTimer invalidate];
        enemyLeftTimer = nil;
    }
}

-(void)goEnemyLeft
{
    if (redbox.center.x + [self getRedboxWidthLeft] > bluebox.center.x + [self getBlueboxWidthRight]) {
        redbox.center = CGPointMake(redbox.center.x -5, redbox.center.y);
    }
}

So the first bit is saying for it to set up a timer to continue moving left. The next will stop the timer if my player(bluebox) and the Computer player(redbox) collide. Finally, the thing that actually tells it to go left, but only if the bluebox is left of the redbox. The getRedBoxWidth Left and getBlueBoxWidthRight are just voids that get, well, the left and right edges. If you could, please help try and solve this code.

Thank You.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about xcode