问题描述
我目前正在 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...*
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!