Follow Us on Twitter!

January 30th, 2009 Posted by: brandontreb - posted under:Articles

Stay tuned for the next game programming tutorial.  Should be up soon…

Happy iCoding.

  • http://www.lotofwallpapers.com Chintan

    Following you now on twitter. :)

  • http://www.feroc.de Feroc

    *following*

    Hope you finnished the housecleaning… ;)

  • Ami Schreiber

    I tried this tutorial and it does not work as expected. The computer only sometimes moves and the scores are not updated in their respective labels, rather there is one single score and it gets updated in the middle of the court and then disappears.

    Here is my code:

    //
    // iTennisViewController.m
    // iTennis
    //
    // Created by Ami Schreiber on 10/25/09.
    // Copyright __MyCompanyName__ 2009. All rights reserved.
    //

    #import “iTennisViewController.h”

    #define kGameStateRunning 1
    #define kGameStatePaused 2

    #define kBallSpeedX 20
    #define kBallSpeedY 30
    #define kCompMoveSpeed 30

    #define kScoreToWin 5

    @implementation iTennisViewController

    @synthesize ball,raquet_yellow,raquet_green,player_score,computer_score,gameState,ballVelocity,tapToBegin;

    /*
    // The designated initializer. Override to perform setup that is required before the view is loaded.
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    // Custom initialization
    }
    return self;
    }
    */

    /*
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void)loadView {
    }
    */

    - (void)viewDidLoad {
    [super viewDidLoad];
    self.gameState = kGameStatePaused;
    ballVelocity = CGPointMake(kBallSpeedX, kBallSpeedY);
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
    }

    - (void) gameLoop {
    if (gameState == kGameStateRunning) {

    ball.center = CGPointMake(ball.center.x + ballVelocity.x,ball.center.y + ballVelocity.y);

    if (ball.center.x > self.view.bounds.size.width || ball.center.x self.view.bounds.size.height || ball.center.y < 0) {
    ballVelocity.y = -ballVelocity.y;
    }

    if (CGRectIntersectsRect(ball.frame,raquet_yellow.frame)) {
    if (ball.center.y raquet_green.center.y) {
    ballVelocity.y = -ballVelocity.y;
    }
    }

    } else {
    if (tapToBegin.hidden) {
    tapToBegin.hidden = NO;
    }
    }

    if (ball.center.y <= self.view.center.y) {
    if (ball.center.x raquet_green.center.x) {
    CGPoint compLocation = CGPointMake(raquet_green.center.x + kCompMoveSpeed, raquet_green.center.y);
    raquet_green.center = compLocation;
    }
    }

    if (ball.center.y = kScoreToWin)];
    }

    if (ball.center.y > self.view.bounds.size.height) {
    computer_score_value++;
    [self reset:(computer_score_value >= kScoreToWin)];
    }

    }

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (gameState == kGameStatePaused) {
    tapToBegin.hidden = YES;
    gameState = kGameStateRunning;
    } else if (gameState == kGameStateRunning) {
    [self touchesMoved:touches withEvent:event];
    }
    }

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    CGPoint xLocation = CGPointMake(location.x, raquet_yellow.center.y);
    raquet_yellow.center = xLocation;
    }

    -(void)reset:(BOOL)newGame {
    self.gameState = kGameStatePaused;
    ball.center = self.view.center;
    if (newGame) {
    if (computer_score_value > player_score_value) {
    tapToBegin.text = @”Computer Wins!”;
    } else {
    tapToBegin.text = @”Player Wins!”;
    }

    computer_score_value = 0;
    player_score_value = 0;
    } else {
    tapToBegin.text = @”Tap To Begin”;
    }

    player_score.text = [NSString stringWithFormat:@"%d",player_score_value];
    computer_score.text = [NSString stringWithFormat:@"%d",computer_score_value];
    }

    /*
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    */

    - (void)didReceiveMemoryWarning {
    // Releases the view if it doesn’t have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren’t in use.
    }

    - (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    }

    - (void)dealloc {
    [super dealloc];
    [ball release];
    [raquet_green release];
    [raquet_yellow release];
    [player_score release];
    [computer_score release];
    [tapToBegin release];
    }

    @end