当两个图像发生冲突时如何生成最终屏幕? [英] How to generate an end screen when two images collide?

查看:59
本文介绍了当两个图像发生冲突时如何生成最终屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当两个图像碰撞时如何生成结束屏幕.我正在用火柴人制作一个应用,您用非常灵敏的加速度计来移动.因此,如果它碰到了这些尖峰(UIImages),它将生成最终屏幕.如何使应用程序检测到此碰撞,然后生成结束屏幕.

how to generate an end screen when two images collide. I am making an app with a stickman you move with a very sensitive acceremeter. SO if it hits these spikes, (UIImages) it will generate the end screen. How do I make the app detect this collision and then generate an end screen.

推荐答案

您没有声明任何rect的事实并不重要.您需要使用rect进行碰撞检测.我假设您至少有火柴人的x和y坐标,并且您应该对火柴人的高度和宽度有所了解.从问题标题来看,似乎您正在使用图像绘制要检查碰撞的对象,因此您应该知道所使用图像的高度和宽度.如果您没有此信息,则无法在正确的位置绘制对象,当然也无法检查碰撞.

The fact that you haven't declared any rects doesn't matter. You need rects for collision detection. I assume that you at least have x and y coordinates for the stickman and you should have some kind of idea of his height and width. Judging from the question title it seems like you're using images to draw the objects you want to check for collision, so you should know the height and width of the images you're using. If you don't have this info you can't draw the objects in the right place and you certainly can't check for collisions.

您基本上希望使用与绘制对象相同的矩形.

You basically want to use the same rects that you use for drawing the objects.

一些代码示例:

如果坐标指向火柴人的中间,则将使用以下内容:

If your coordinates point to the middle of the stickman you would use something like the following:

if (CGRectIntersectsRect(CGRectMake(stickman.x-stickman.width/2,
                                    stickman.y-stickman.height/2,
                                    stickman.width,
                                    stickman.height),
                         CGRectMake(spikes.x-spikes.width/2,
                                    spikes.y-spikes.height/2,
                                    spikes.width,
                                    spikes.height))) {
    // Do whatever it is you need to do. For instance:
    [self showEndScreen];
}

如果坐标指向火柴人的左上角,则应使用:

If your coordinates point to the top left corner of your stickman you would use:

if (CGRectIntersectsRect(CGRectMake(stickman.x,
                                    stickman.y,
                                    stickman.width,
                                    stickman.height),
                         CGRectMake(spikes.x,
                                    spikes.y,
                                    spikes.width,
                                    spikes.height))) {
    // Do whatever it is you need to do. For instance:
    [self showEndScreen];
}

如果我可以建议您,建议将坐标和尺寸存储在CGRect中,这样您就不必在每次检查碰撞时都创建一个新的CGRect.

If I might give you a suggestion, I would suggest storing the coordinates and sizes in a CGRect, so that you don't have to create a new CGRect every time you're checking for collision.

这篇关于当两个图像发生冲突时如何生成最终屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆