PDA

View Full Version : iTennis Tutorial problem




noahgolm
Aug 20, 2010, 03:27 PM
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
Aug 20, 2010, 03:58 PM
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
Aug 20, 2010, 03:59 PM
Never mind, found the problem. Put a lowercase "i" instead of an uppercase one. Silly trivial typos, always screwing up the world.

noahgolm
Aug 20, 2010, 04:46 PM
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:


#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
Aug 20, 2010, 06:00 PM
#import "iTennisViewController.h"

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


@end


How similar should the red be to the blue?

lloyddean
Aug 20, 2010, 07:00 PM
Never mind, found the problem. Put a lowercase "i" instead of an uppercase one. Silly trivial typos, always screwing up the world.

But typo's don't happen without people, so

noahgolm
Aug 20, 2010, 07:16 PM
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
Aug 20, 2010, 08:10 PM
Look at the red code more closely.

if(ball.center.y > self.view.bounds.size.width || 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
Aug 20, 2010, 08:23 PM
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.

dejo
Aug 20, 2010, 08:49 PM
Shouldn't X and Y be perpendicular, and thus compared to two different dimensions, like maybe WIDTH and HEIGHT?
Unless, of course, the view is square. ;)

I reviewed the code over and over, but that seems incredibly obvious.
It's important to type exactly what the tutorial provides and only alter it if you run into problems. :)