开发者

IPhone SDK help with minigame

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


You're testing rectangle intersection, maybe you should do a circle collision?

Distance(C1, C2) <= R1 + R2 means that the circles collided

Depending on how realistic you want the game you could also figure out the movement vector for your ball and reflect it off of the surface normal of the bat.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜