Xcode CGRect 与多个 CGRect 发生碰撞?
我创建了一个游戏,其中的地板由多个高度不同的图像组成。(所以地板不平坦) 而不是打开一百万个 if 语句:
if (CGRectIntersectsRect(ball1.frame, Floor1.frame)) { }
if (CGRectIntersectsRect(ball1.frame, Floor2.frame)) { }
if (CGRectIntersectsRect(ball1.frame, Floor3.frame)) { }
if (CGRectIntersectsRect(ball1.frame, Floor4.frame)) { }
//等等...
。
有没有办法可以使用 1 个 if 语句检查碰撞?所有 UIImageView 地板块均被命名为“floor1、floor2、floor3、floor4 等”,
谢谢!
旁注: 如果我想使用 CGRectIntersect 代码并旋转图像,我会检查交集,因为它似乎仍然只检查图像帧的碰撞,所以无论我旋转它还是不旋转它,它都会在同一位置发生碰撞。我也可以旋转框架来解决这个问题吗?或者我是否需要使用不同的代码,如果是的话,什么代码?
I have created a game where I have a floor made of multiple images varying in heights.. (So the floor isn't flat)
Rather then opening a million if statements:
if (CGRectIntersectsRect(ball1.frame, floor1.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor2.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor3.frame)) {
}
if (CGRectIntersectsRect(ball1.frame, floor4.frame)) {
}
//and so on...
.
Is there a way I can check collision using 1 if statement? All of the UIImageView floor pieces are named, "floor1, floor2, floor3, floor4, and so on"
Thanks!
Side note:
If I want to use the CGRectIntersect code and I rotate the image I'm checking the intersection for it seems to still only check the collision for the image frame, so wether I rotate it or not it collides in the same spot. Can I rotate the frame as well to fix this or do I need to use different code, if so what code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我不太明白你的问题,但我想我可以简化一下。
您不应将图块命名为 Floor1、Floor2...,而应使用数组。
这样,您可以将 if 语句折叠为 3 条语句:
While I don't quite understand your question, I think I can offer a simplification.
Instead of naming the tiles floor1, floor2, ..., you should instead use an array.
In this way, you could collapse the if statements to 3 statements:
一定要改变你的命名方案,当有超过 2 或 3 个项目时,item1、item2、item3 永远不是正确的选择。如果必须的话,只需
在没有人特别建议的代码之前说即可。
Definitely change your naming scheme, item1, item2, item3 is never the right option where there's more than 2 or 3 items. If you have to, just say
Before the code that No One In Particular suggested.