Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-25-2010, 08:47 PM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default Crash Problem

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

Last edited by Kalimba; 03-25-2010 at 08:54 PM. Reason: For readability, added CODE tags around code. Please try to do this in your future posts.
Whitehk is offline   Reply With Quote
Old 04-02-2010, 05:28 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London/Peterborough
Posts: 562
QuantumDoja is on a distinguished road
Default

If you comment out all the code in the methods but leave the method signatures in place, does it still crash? Also post the backtrace so we could have a quick place to start!
QuantumDoja is offline   Reply With Quote
Old 04-02-2010, 05:41 AM   #3 (permalink)
Registered Member
 
alexy's Avatar
 
Join Date: Jul 2009
Location: Melbourne, Aus
Posts: 199
alexy is on a distinguished road
Default

Yeah, a dump would be a great help.

Curious if it still crashes if you don't start your timer in viewDidLoad method (just to begin trying to isolate the problem
alexy is offline   Reply With Quote
Reply

Bookmarks

Tags
crash, crashes, crashing, memory, memory management

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
8 members and 394 guests
iOS.Lover, JackReidy, jeroenkeij, Leslie80, Sami Gh, Wikiboo, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:45 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0