IPhone SDK help with minigame
- by Harry
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