碰撞检测中的操作(CGRectIntersectsRect)

时间:2011-05-01 01:13:27

标签: objective-c collision-detection

我有两张照片。你必须使用acceleremoter来移动一个男人并避免大飙升。这是我的.h编码:

IBOutlet UIImageView *rect1;

IBOutlet UIImageView *rect2;

这是我的.m:

bool CGRectIntersectsRect ( CGRect rect1, CGRect rect2 );

我已经连接了nib文件中的所有内容,我知道没有任何事情会发生,因为没有错误。我需要碰撞这两件事,然后生成一个结束屏幕。但是 HOW 我是否已经采取行动了。谢谢!

的.m

#import "GameScreen.h"


@implementation GameScreen
@synthesize ball, delta;


-(void)viewDidLoad {


    UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
    accel.delegate = self;
    accel.updateInterval = 1.0f / 60.0f;

    [super viewDidLoad];


}

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    NSLog(@"x : %g", acceleration.x);
    NSLog(@"y : %g", acceleration.y);
    NSLog(@"z : %g", acceleration.z);

    delta.y = acceleration.y * 70;
    delta.x = acceleration.x * 70;

    ball.center = CGPointMake(ball.center.x + delta.x, ball.center.y + delta.y);

    // Right
    if(ball.center.x < 0) {
        ball.center = CGPointMake(320, ball.center.y);
    }

    // Left

    if(ball.center.x > 320) {
        ball.center = CGPointMake(0, ball.center.y);

    }

    // Top

    if(ball.center.y < 0) {
        ball.center = CGPointMake(ball.center.x, 460);

    }

    // Bottom
    if(ball.center.y > 460){
        ball.center = CGPointMake(ball.center.x, 0);

    }


    }




bool CGRectIntersectsRect ( CGRect rect1, CGRect rect2 );



-(void)dealloc {
    [super dealloc];
}






@end

2 个答案:

答案 0 :(得分:2)

结束之前
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration

你必须把这段代码

if(CGRectIntersectsRect(ball.bounds , man.bounds))
{
    UIImage *image = [UIImage imageNamed:@"endScreenImage.png"];
    UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
    [self.view addSubView:imageview];
    [imageview release];
}

但我也认为你必须购买一本Objective-C编程书并阅读它,而不是用很多类似的问题向StackOverflow发送垃圾邮件。我写的是因为我觉得它对你有用,不会让你生气。

答案 1 :(得分:0)

你可以尝试这个,如果它可以使用ImageView

就完成了

初​​始化

[self schedule:@selector(update:)];

虚空

- (void)update:(ccTime)dt {
...

CGRect absoluteBox = CGRectMake(ball.position.x, ball.position.y, [ball boundingBox].size.width, [ball boundingBox].size.height);
if (CGRectIntersectsRect([ball boundingBox], [man boundingBox])) {
      // SOME CODE HERE         
    }
...

}