碰撞检测中的动作 (CGRectIntersectsRect)

发布于 2024-11-05 00:17:58 字数 1574 浏览 1 评论 0原文

我有两个图像。您必须使用acceleremoter来移动人并避免出现大尖峰。这是我的 .h 编码:

IBOutlet UIImageView *rect1;

IBOutlet UIImageView *rect2;

这是我的 .m:

bool CGRectIntersectsRect ( CGRect rect1, CGRect rect2 );

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

.米

#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

I have two images. You have to use a acceleremoter to move a man and avoid a big spike. Here is my .h coding:

IBOutlet UIImageView *rect1;

IBOutlet UIImageView *rect2;

Here is my .m:

bool CGRectIntersectsRect ( CGRect rect1, CGRect rect2 );

I have connected everything in the nib file and I know nothing would happen because there was no error. I need these two things to collide and then generate an end screen. BUT HOW do I put an action in. THANKS!

.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜清冷一曲。 2024-11-12 00:17:58

就在结束之前,

-(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 上充斥大量类似的问题。我写这些是因为我认为它对你有用,而不是让你生气。

Just before the end of

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration

you have to put this code

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

But I also think that you have to buy an Objective-C programming book and read it, instead of spamming StackOverflow with a lot of similar questions. I wrote that because I think it can be useful to you, not to make you angry.

这样的小城市 2024-11-12 00:17:58

一起工作就不要

你可以尝试这个,如果它能与 ImageView 的init

[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         
    }
...

}

You can try this, just donne if its gonna work with ImageView's

init

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

Make a void

- (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         
    }
...

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文