Hi I created a game app for the ipad and Everytime I switch to this view, it crashes. It's probably a memory problem. I have looked through the code countless times, and can still find nothing wrong with it. So I finally decided to post here. Thanks for the help. Here is my code to examine:
Code:
//
// Easy.m
// App Name
//
// Created by Me on 3/25/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "Easy.h"
#import "OriginalDifficulty.h"
#define kGameStateRunning 1
#define kGameStatePaused 2
#define kBallSpeedX 2
#define kBallSpeedY 3
#define kCompMoveSpeed 1
#define kScoreToWin 5
@implementation Easy
@synthesize backButton,ballEasy,player1Easy,compEasy,player1_score_easy,comp_score_easy,gameStateEasy,ballVelocityEasy,tapToBeginEasy;
- (IBAction)backClicked:(UIButton *)button; {
OriginalDifficulty *originalDifficultyView = [[OriginalDifficulty alloc] initWithNibName:@"OriginalDifficulty" bundle:[NSBundle mainBundle]];
[self.view addSubview:originalDifficultyView.view];
[Easy release];
}
/*
// 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;
}
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(gameStateEasy == kGameStatePaused) {
tapToBeginEasy.hidden = YES;
gameStateEasy = kGameStateRunning;
} else if(gameStateEasy == 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,player1Easy.center.y);
player1Easy.center = xLocation;
}
-(void) gameLoop {
if(gameStateEasy == kGameStateRunning) {
ballEasy.center = CGPointMake(ballEasy.center.x + ballVelocityEasy.x , ballEasy.center.y + ballVelocityEasy.y);
if(ballEasy.center.x > self.view.bounds.size.width || ballEasy.center.x < 0) {
ballVelocityEasy.x = -ballVelocityEasy.x;
}
if(ballEasy.center.y > self.view.bounds.size.height || ballEasy.center.y < 0) {
ballVelocityEasy.y = -ballVelocityEasy.y;
}
if (CGRectIntersectsRect (ballEasy.frame, player1Easy.frame)) {
CGRect frame = ballEasy.frame;
frame.origin.y = player1Easy.frame.origin.y - frame.size.height;
ballEasy.frame = frame;
ballVelocityEasy.y = -ballVelocityEasy.y; // turn ball around if collide
}
if (CGRectIntersectsRect (ballEasy.frame, compEasy.frame)) {
CGRect frame = ballEasy.frame;
frame.origin.y = CGRectGetMaxY(compEasy.frame);
ballEasy.frame = frame;
ballVelocityEasy.y = -ballVelocityEasy.y; // turn ball around if collide
}
// Begin Simple AI
if(ballEasy.center.y <= self.view.center.y) {
if(ballEasy.center.x < compEasy.center.x) {
CGPoint compLocation = CGPointMake(compEasy.center.x - kCompMoveSpeed, compEasy.center.y);
compEasy.center = compLocation;
}
if(ballEasy.center.x > compEasy.center.x) {
CGPoint compLocation = CGPointMake(compEasy.center.x + kCompMoveSpeed, compEasy.center.y);
compEasy.center = compLocation;
}
}
// Begin Scoring Game Logic
if(ballEasy.center.y <= 0) {
player1_score_value_easy++;
[self reset:(player1_score_value_easy >= kScoreToWin)];
}
if(ballEasy.center.y > self.view.bounds.size.height) {
comp_score_value_easy++;
[self reset:(comp_score_value_easy >= kScoreToWin)];
}
} else {
if(tapToBeginEasy.hidden) {
tapToBeginEasy.hidden = NO;
}
}
}
-(void)reset:(BOOL) newGame {
self.gameStateEasy = kGameStatePaused;
ballEasy.center = self.view.center;
if(newGame) {
if(comp_score_value_easy > player1_score_value_easy) {
tapToBeginEasy.text = @"Computer Wins!";
} else {
tapToBeginEasy.text = @"Player Wins!";
}
comp_score_value_easy = 0;
player1_score_value_easy = 0;
} else {
tapToBeginEasy.text = @"Tap To Begin";
}
player1_score_easy.text = [NSString stringWithFormat:@"%d",player1_score_value_easy];
comp_score_easy.text = [NSString stringWithFormat:@"%d",comp_score_value_easy];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.gameStateEasy = kGameStatePaused;
ballVelocityEasy = CGPointMake(kBallSpeedX,kBallSpeedY);
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
[ballEasy release];
[compEasy release];
[player1Easy release];
[player1_score_easy release];
[comp_score_easy release];
[tapToBeginEasy release];
}
@end