IPhone SDK help with minigame

Posted by Harry on Stack Overflow See other posts from Stack Overflow or by Harry
Published on 2009-11-05T17:51:57Z Indexed on 2010/04/12 13:03 UTC
Read the original article Hit count: 319

Filed under:

right basically what ive got is an app which is a ball and bat and its how many bounces you can achieve, it works alright but there is one problem,

when the ball hits the side of the bat it throws it off course and its like the frame of the ball is bouncing in the frame of the bat, Here is my code in my mainview.m

#import "MainView.h"
#define kGameStateRunning 1

@implementation MainView
@synthesize paddle, ball;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
CGPoint xLocation = CGPointMake(location.x,paddle.center.y);
paddle.center = xLocation;
}

-(IBAction) play {
pos = CGPointMake(14.0,7.0);
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self     selector:@selector(onTimer) userInfo:nil repeats:YES];
}

-(void) onTimer {
ball.center = CGPointMake(ball.center.x+pos.x,ball.center.y+pos.y);

if(ball.center.x > 320 || ball.center.x < 0)
	pos.x = -pos.x;
if(ball.center.y > 460 || ball.center.y < 0)
	pos.y = -pos.y;
[self checkCollision];
}

-(void) checkCollision {

	if(CGRectIntersectsRect(ball.frame,paddle.frame)) {
			pos.y = -pos.y;

	}
}


@end

can anyone work out the problem here? Thanks Harry

© Stack Overflow or respective owner

Related posts about iphone-sdk