Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)
问题描述
我目前正在 Cocos2d 中为 iPhone 开发一个涂鸦跳跃风格的游戏,并设置了一个包含两个不同层的场景 - 游戏对象(平台、收藏品等)和玩家(角色,由玩家控制).
I'm currently developing a doodle-jump style game in Cocos2d for iPhone and have a scene set up with two different layers - game objects (platforms, collectables etc...) and player (character, controlled by the player).
我将它们放在单独的层中,因为我想在玩家跳跃时向下滚动整个游戏对象层 - 给它一种垂直的、涂鸦跳跃风格的感觉.
I have these in separate layers because I want to scroll the entire game objects layer down when the player jumps up - giving it the vertical, doodle-jump style feel.
问题在于玩家和平台之间不会发生交集,因为它们位于不同的层.
The problem is that intersection between the player and the platforms doesn't occur because they're on different layers.
有谁知道如何解决这个问题?有些人提到了 convertToWorldCoords,但我对此一无所知!
Does anyone know how this can be solved? Some have mentioned convertToWorldCoords but I'm lost with that!
推荐答案
是的,先生,convertToWorldCoords!或者,类似的东西——基本上你想了解你的玩家和游戏对象的位置,并且一种方法是将它们全部转换为世界"坐标.或者,您可以将玩家位置/矩形转换为游戏对象的坐标系.
Yessir, convertToWorldCoords! Or, something like that -- basically you want to have an understanding of your player and game-object positions in relation to each other, and one way to do that is to transform them all to the "world" coordinates. Alternately you could transform the player position/rectangle to be in your game-objects' coordinate system.
想要通过一些 CGRect 交集测试来保持简单吗?用类别扩展 CCNode
:
Want to keep it simple with just some CGRect intersection tests? Extend CCNode
with a category:
CCNode+CoordHelpers.h
#import "CCNode.h"
@interface CCNode (CoordHelpers)
- (CGRect) worldBoundingBox;
@end
CCNode+CoordHelpers.m
#import "CCNode+CoordHelpers.h"
@implementation CCNode (CoordHelpers)
-(CGRect)worldBoundingBox {
CGRect rect = CGRectMake(0, 0, contentSize_.width, contentSize_.height);
return CGRectApplyAffineTransform(rect, [self nodeToWorldTransform]);
}
@end
那么,对于超简单的 CGRect 碰撞测试:
Then, for super simple CGRect collision testing:
if(CGRectIntersectsRect([playerObj worldBoundingBox], [otherObj worldBoundingBox])
{/*...do stuff...*
本文标题为:Cocos2d - 如何检查不同层中对象之间的交集
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01