Cocos2d - 如何检查不同图层中对象之间的交集 [英] Cocos2d - How to check for Intersection between objects in different layers

查看:204
本文介绍了Cocos2d - 如何检查不同图层中对象之间的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为Cocos2d for iPhone开发一个Doodle-jump风格的游戏,并且有一个场景设置有两个不同的图层 - 游戏对象(平台,收藏品等...)和玩家(角色,由玩家控制) 。

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!

推荐答案

Yessir,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

CCNode+CoordHelpers.h

#import "CCNode.h"

@interface CCNode (CoordHelpers)
- (CGRect) worldBoundingBox;
@end

CCNode + CoordHelpers.m

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


$ b

Then, for super simple CGRect collision testing:

if(CGRectIntersectsRect([playerObj worldBoundingBox], [otherObj worldBoundingBox])    
{/*...do stuff...*/}

确保 #importCCNode + CoordHelpers。 h无论您需要使用此方法!

Make sure to #import "CCNode+CoordHelpers.h" wherever you need to use this method!

这篇关于Cocos2d - 如何检查不同图层中对象之间的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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