Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

noahgolm

macrumors member
Original poster
Aug 18, 2010
65
0
Hello. This is my second attempt at doing the iCode blog app tutorial, after trying once without even knowing c. After learning more about both c and objective c, I decided to try again (blessed be the holy "Become an Xcoder" ebook). After finishing the simple AI step, the app was supposed to work, very simply. When I run the program, however, the splash screen shows up, stays there for a while, and crashes. I selected debugging while running, and that showed the message "GDB: Program received signal SIGABRT," which I understand means to abort the operation. Anyone know what's going on? Is it a compatibility issue?
 

fernandovalente

macrumors 6502
Hello. This is my second attempt at doing the iCode blog app tutorial, after trying once without even knowing c. After learning more about both c and objective c, I decided to try again (blessed be the holy "Become an Xcoder" ebook). After finishing the simple AI step, the app was supposed to work, very simply. When I run the program, however, the splash screen shows up, stays there for a while, and crashes. I selected debugging while running, and that showed the message "GDB: Program received signal SIGABRT," which I understand means to abort the operation. Anyone know what's going on? Is it a compatibility issue?

Post your code
 

noahgolm

macrumors member
Original poster
Aug 18, 2010
65
0
Never mind, found the problem. Put a lowercase "i" instead of an uppercase one. Silly trivial typos, always screwing up the world.
 

noahgolm

macrumors member
Original poster
Aug 18, 2010
65
0
New Problem!

Agh! Now that it's working, I have a new problem! When the ball moves past a certain point on the screen it bounce back as if that were the edge of the screen. It never makes it to the yellow paddle, although the opposite side works just fine, as well as the ai. Here's the code:

Code:
#import "iTennisViewController.h"

#define kCompMoveSpeed 15
#define kGameStateRunning 1
#define kGameStatePaused  2

#define kBallSpeedX 10
#define kBallSpeedY 10

@implementation iTennisViewController
@synthesize ball,racquet_yellow,racquet_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 {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (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) 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 < 0) {
			ballVelocity.x = -ballVelocity.x;
		}
		
		if(ball.center.y > self.view.bounds.size.width || ball.center.y < 0) {
			ballVelocity.y = -ballVelocity.y;
		}
	} else {
		if (tapToBegin.hidden) {
			tapToBegin.hidden = NO;
		}
	}
	if(CGRectIntersectsRect(ball.frame,racquet_yellow.frame)) {
		if(ball.center.y <racquet_yellow.center.y) {
			ballVelocity.y = -ballVelocity.y;
			NSLog(@"%f %f",ball.center, racquet_green.center);
		}
	}
	if(CGRectIntersectsRect(ball.frame,racquet_green.frame)) {
		if(ball.center.y >racquet_green.center.y) {
			ballVelocity.y = -ballVelocity.y;
		}
	}
	//Begin Simple AI
	if(ball.center.y <= self.view.center.y) {
		if(ball.center.x < racquet_green.center.x) {
			CGPoint compLocation = CGPointMake(racquet_green.center.x - kCompMoveSpeed, racquet_green.center.y);
			racquet_green.center = compLocation;
		}
		
		if(ball.center.x > racquet_green.center.x) {
			CGPoint compLocation = CGPointMake(racquet_green.center.x + kCompMoveSpeed, racquet_green.center.y);
			racquet_green.center = compLocation;
		}
	}
}

- (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,racquet_yellow.center.y);
	racquet_yellow.center = xLocation;
}
/*
// 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];
	[racquet_green release];
	[racquet_yellow release];
	[player_score release];
	[computer_score release];
	[tapToBegin release];
}

@end
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,419
A sea of green
Code:
#import "iTennisViewController.h"

-(void) gameLoop {
	if(gameState == kGameStateRunning) {
		
		ball.center = CGPointMake(ball.center.x + ballVelocity.x, ball.center.y + ballVelocity.y);
		
		if(ball.center.[COLOR="Blue"]x[/COLOR] > self.view.bounds.size.[COLOR="blue"]width[/COLOR] || ball.center.x < 0) {
			ballVelocity.x = -ballVelocity.x;
		}
		
		if(ball.center.[COLOR="Red"]y[/COLOR] > self.view.bounds.size.[COLOR="Red"]width[/COLOR] || ball.center.y < 0) {
			ballVelocity.y = -ballVelocity.y;
		}
	} else {
		if (tapToBegin.hidden) {
			tapToBegin.hidden = NO;
		}
	}
	if(CGRectIntersectsRect(ball.frame,racquet_yellow.frame)) {
		if(ball.center.y <racquet_yellow.center.y) {
			ballVelocity.y = -ballVelocity.y;
			NSLog(@"%f %f",ball.center, racquet_green.center);
		}
	}
	if(CGRectIntersectsRect(ball.frame,racquet_green.frame)) {
		if(ball.center.y >racquet_green.center.y) {
			ballVelocity.y = -ballVelocity.y;
		}
	}
	//Begin Simple AI
	if(ball.center.y <= self.view.center.y) {
		if(ball.center.x < racquet_green.center.x) {
			CGPoint compLocation = CGPointMake(racquet_green.center.x - kCompMoveSpeed, racquet_green.center.y);
			racquet_green.center = compLocation;
		}
		
		if(ball.center.x > racquet_green.center.x) {
			CGPoint compLocation = CGPointMake(racquet_green.center.x + kCompMoveSpeed, racquet_green.center.y);
			racquet_green.center = compLocation;
		}
	}
}


@end

How similar should the red be to the blue?
 

noahgolm

macrumors member
Original poster
Aug 18, 2010
65
0
How similar should the red be to the blue?

Well, they're basically the exact same, just on different planes. That's not the problem, though. I think it might have to do with the boundaries or something, but I'm not sure.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,419
A sea of green
Look at the red code more closely.

Code:
if(ball.center.[SIZE="4"][COLOR="red"]y[/COLOR][/SIZE] > self.view.bounds.size.[SIZE="4"][COLOR="Red"]width[/COLOR][/SIZE] || ball.center.y < 0)

Y compared to WIDTH? Really?

Shouldn't X and Y be perpendicular, and thus compared to two different dimensions, like maybe WIDTH and HEIGHT?
 

noahgolm

macrumors member
Original poster
Aug 18, 2010
65
0
By God, man. You're right. Thank you! What a simply logical idea! I reviewed the code over and over, but that seems incredibly obvious. Thank you! It works now.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.