I have 6 different boxes and number of items (fruits and vegetables) for particular box, like all fruits will go in read box and all vegebles will be dropped in green box, so I have made a class (inherit the CCSprite) so that I can move the objects, now how can I detect the boxes, while boxes are changing their positions?
Asked
Active
Viewed 192 times
0
-
you might want to change the title of our question... 'you want to detect detection'? ... also, i think using standard uikit and core animation would be much easier than cocos2d for such a simple game/app... Inspire48's answer is crrect. – Swapnil Luktuke Jul 04 '11 at 13:03
2 Answers
2
CCSprite *red=[CCSprite spriteWithFile:@"red.png"];
red.tag=3;
[self addChild:red];
CCSprite *blue=[CCSprite spriteWithFile:@"blue.png"];
blue.tag=4;
[self addChild:blue];
then while u create the red and blue body u have
b2BodyDef bd;
bd.type=b2_dynamicBody;
bd.position.Set(w/PTM_RATIO,h/PTM_RATIO);
bd.userData=red;
//in ur step function for each m_body
b2Fixture *f=m_body->GetFixtureList();
b2Vec2 locationWorld = b2Vec2(desiredlocationX/PTM_RATIO, desiredlocationY/PTM_RATIO);
if (f->TestPoint(locationWorld)) {
CCSprite *actor = (CCSprite*)m_body->GetUserData();
if ([actor tag] == 3) {
//red box
}
else if([actor tag] == 4){
//blue box
}
}

Vijay-Apple-Dev.blogspot.com
- 25,682
- 7
- 67
- 76
0
Because they are boxes, you could just create a CGRect for them, and simply use the x and y coordinates of the CGRect to track them.
Every CCSprite has a boundingBox property that is its CGRect. See this SO thread for more info:
how to obtain a CCSprite's width and height in cocos2d for iphone
-
but boxes are in one class and Paddle is different class which in handling the movement(drag and drop), so how to I ? – Chatar Veer Suthar Jul 04 '11 at 12:59
-
If I will call a method which will take paddle as argument and then do operation, then have to create object of that class(where boxes are) for calling method, so If I will make objects again and again, it will create new class which is first layer, – Chatar Veer Suthar Jul 04 '11 at 13:00
-
I know this, but both classes are different, not 1, boxes are showing as on background in one layer, then objects are moving in another class, – Chatar Veer Suthar Jul 04 '11 at 13:04
-
I'd create a view controller that encapsulates the entire game's view. Then you have a common measuring platform. `[[CCDirector sharedDirector] openGLView]` will get you the game's OpenGL view which you can then set as your view controller's view. Then save the location of your boxes, get the position of the moving objects, and you can check for a CGRectIntersectsRect() every frame (it's not really expensive) for the collision. – FeifanZ Jul 04 '11 at 13:16