尝试使用Cocos2D集合检测并提出一些问题。首先,一些背景知识:
这是我向游戏添加新项目的方法,该游戏位于与游戏层不同的类别中。它位于我的Item-class:
-(void) addItem:(NSString *) theFileName: (NSMutableArray *) theArray{
CCSprite *item = [CCSprite spriteWithFile:theFileName
rect:CGRectMake(0, 0, 50, 50 )];
//Positions
int minX = 160;
int maxX = 360;
int xRange = maxX - minX;
int xCord = (arc4random() % xRange) + minX;
item.position = ccp(xCord, -5);
[self addChild:item];
[theArray addObject:item];
然后我在游戏图层中使用此方法,使用对名为ItemManager的Item类的引用:
[ItemManager addItem:@"box.png" :itemList];
如果我想检测两个精灵之间的碰撞,在这种情况下是一个盒子和其他东西,我需要能够使用addItem方法中创建的box rect。
for(CCSprite *newItem in itemList){
//(if box rectangle collides with my players or whatever
}
那么如何才能访问原始方法中一直创建的矩形?
谢谢。
答案 0 :(得分:0)
在这种情况下,我建议使用CCSprite的boundingBox属性进行冲突检测:
示例:
for(CCSprite *newItem in itemList)
{
// if box rectangle collides with my players or whatever
if (CGRectContainsRect([newItem boundingBox], [player boundingBox]))
// do something
}