iTennis Tutorial problem

Discussion in 'iOS Programming' started by noahgolm, Aug 20, 2010.

  1. noahgolm
    Expand Collapse
    macrumors member

    Joined:
    Aug 18, 2010
    #1
    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?
     
  2. fernandovalente
    Expand Collapse
    macrumors 6502

    fernandovalente

    Joined:
    Aug 29, 2008
    Location:
    Third world **** hole, I mean, Brazil
    #2
    Post your code
     
  3. noahgolm
    Expand Collapse
    thread starter macrumors member

    Joined:
    Aug 18, 2010
    #3
    Never mind, found the problem. Put a lowercase "i" instead of an uppercase one. Silly trivial typos, always screwing up the world.
     
  4. noahgolm
    Expand Collapse
    thread starter macrumors member

    Joined:
    Aug 18, 2010
    #4
    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
    
     
  5. chown33
    Expand Collapse
    macrumors 604

    Joined:
    Aug 9, 2009
    #5
    How similar should the red be to the blue?
     
  6. lloyddean
    Expand Collapse
    macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    But typo's don't happen without people, so …
     
  7. noahgolm
    Expand Collapse
    thread starter macrumors member

    Joined:
    Aug 18, 2010
    #7
    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.
     
  8. chown33
    Expand Collapse
    macrumors 604

    Joined:
    Aug 9, 2009
    #8
    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?
     
  9. noahgolm
    Expand Collapse
    thread starter macrumors member

    Joined:
    Aug 18, 2010
    #9
    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.
     
  10. dejo
    Expand Collapse
    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Unless, of course, the view is square. ;)

    It's important to type exactly what the tutorial provides and only alter it if you run into problems. :)
     

Share This Page