0
votes

Need a Quick Help. I'm creating my first game Cocos 2D and Box 2D and would require help on how to display text during collision. Criteria is very simple. Using a stone and a slingshot i need to hit the objects falling from above. Each object has its own points and features. For one of them i need to implement, if the stone destroys more than 3 objects, the word "combo" should come up on screen and gradually fade out. Will be eagerly waiting for your reply and suggestions.

Regards,

Karthik

2
u implemented slingshot like angry bird game. if yes means plz share u r code. - Srinivas

2 Answers

1
votes

for example in cgintersectrect is detect your collision in update there you have to write text like these.

Example code:-

if (CGRectIntersectsRect(projectileRect, targetRect)) {
                [targetsToDelete addObject:target];
                CCMenuItem *pause_menu = [CCMenuItemImage itemFromNormalImage:@"pause.png" selectedImage:@"pause.png" target:self selector:@selector(pauseGame:)];
                CCMenu *menu = [CCMenu menuWithItems: pause_menu, nil];
                menu.position = ccp(460, 15);
                [self addChild:menu ];
            }   
0
votes

Seems fairly straightforward and simple:

// use this to keep track of how many stones are destroyed, 
// every time a stone is destroyed, increment it by 1
int count = 0;

To display the "combo", you would want to use a graphic or a label - this is covered in the cocos2d examples, so I won't explain how to display it here.

From here, in your "tick" callback, just check the value of "count", if it is greater than or equal to "3" (your "magic" number) then run the following action sequence on your "combo" node.

CCFadeIn *fadeIn = [CCFadeIn actionWithDuration: 0.25f];
CCFadeOut *fadeOut = [CCFadeOut actionWithDuration: 0.0125f];
CCDelayTime *delay = [CCDelayTime actionWithDuration: 0.5f];
[node runAction: [CCSequence actions: fadeIn, delay, fadeOut, nil];

This set of actions will Fade the node in over a quarter second, wait for half a second, then fade it out over an eighth of a second. You can adjust timing as necessary, for your code. Some additional things you can do, when the counter is > 3 is reposition the "combo" text to the position of the last item destroyed, or randomize it's position so it's not always in the same place, etc ...

The "node" object in the example code is a CCSprite or CCLabel that remains added to your scene, but simply has it's visibility toggled (as this item will appear in the scene more than likely "often" it's better to leave it in the scene and change it's visibility rather than create a new one and destroy it each time - if it's an infrequent item, just create a new one, and destroy it ...