Experimenting with Cocos2D collection detection and have some questions. First, some background:
This is my method for adding a new item to my game, located in a different class than my game layer. This is located in my 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];
Then I use this method in my game layer, using a reference to the Item-class called ItemManager:
[ItemManager addItem:@"box.png" :itemList];
If I want to detect collision between two sprites, in this case a box and something else, I need to be able to use the box rect created in the addItem-method.
for(CCSprite *newItem in itemList){
//(if box rectangle collides with my players or whatever
}
So how can I acces the rectangle created all the way back in the original method?
Thank you.
In this case, I would suggest using the boundingBox property of CCSprite for collision detection:
Example:
for(CCSprite *newItem in itemList)
{
// if box rectangle collides with my players or whatever
if (CGRectContainsRect([newItem boundingBox], [player boundingBox]))
// do something
}