PDA

View Full Version : iCode iTennis App Problem




Fuzzball27
Aug 8, 2011, 05:46 PM
Hi all,
I just recently got the iOS SDK with Xcode 4, and I'm working on getting familiar with building apps. As the title of my thread indicates I'm having a problem with the iCode blog's app tutorial, iTennis. The problem that I'm having is that when I run the app, the "Tap to start" label doesn't disappear, and the ball doesn't move...the user raquette works. I will post the entire program at the end of my thread. All help is greatly appreciated :)
Here is the specific area of gameState coding where I suspect the problem(s) to be:

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

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


Here is the ViewController.h file
#import <UIKit/UIKit.h>

@interface iTennisViewController : UIViewController {
IBOutlet UIImageView *Ball;
IBOutlet UIImageView *Raquet_Green;
IBOutlet UIImageView *Raquet_Yellow;
IBOutlet UILabel *tapToBegin;

IBOutlet UILabel *player_score;
IBOutlet UILabel *computer_score;

CGPoint BallVelocity;
NSInteger gameState;
}

@property(nonatomic,retain) IBOutlet UIImageView *Ball;
@property(nonatomic,retain) IBOutlet UIImageView *Raquet_Green;
@property(nonatomic,retain) IBOutlet UIImageView *Raquet_Yellow;
@property(nonatomic,retain) IBOutlet UILabel *tapToBegin;
@property(nonatomic,retain) IBOutlet UILabel *player_score;
@property(nonatomic,retain) IBOutlet UILabel *computer_score;
@property(nonatomic) IBOutlet CGPoint BallVelocity;
@property(nonatomic) IBOutlet NSInteger gameState;
@end


Here is the ViewController.m file #import "iTennisViewController.h"

#define kGameStateRunning 1
#define kGameStatePaused 2

#define kCompMoveSpeed 15

#define kBallSpeedX 10
#define kBallSpeedY 15

@implementation iTennisViewController
@synthesize Ball,Raquet_Yellow,Raquet_Green,player_score,computer_score,gameState,BallVelocity,tapToBegin;

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

-(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) gameLoop {
if (gameState == kGameStateRunning) {
if (CGRectIntersectsRect(Ball.frame, Raquet_Yellow.frame)) {
if(Ball.center.y < Raquet_Yellow.center.y) {
BallVelocity.y = -BallVelocity.y;
}
}
if (CGRectIntersectsRect(Ball.frame, Raquet_Green.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.x > Raquet_Green.center.x) {
CGPoint compLocation = CGPointMake(Raquet_Green.center.x +kCompMoveSpeed, Raquet_Green.center.y);
Raquet_Green.center = compLocation;
}
}
}
}

-(void) dealloc {
[super dealloc];
[Ball release];
[Raquet_Green release];
[Raquet_Yellow release];
[player_score release];
[computer_score release];
[tapToBegin release];
}

- (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.
}

#pragma mark - View lifecycle

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end