I am trying to develop a game with multiple stages using cocos2d. In a level for example, I have 4 sprites, 2 white and 2 black. If the player hits the black sprites, the game is over, if he hits the white sprite, he wins. How can I implement a condition wherein if the player hits the white sprite, it checks if there are other white sprites present on the scene, if there is, the game continues. If there’s none, then he goes to the stage clear scene? I tried putting the sprites in two different arrays (arrayBlack and arrayWhite) but I’m stuck with how I’m gonna make the condition for the white sprites. Can anybody please give me an idea or suggestion or a tutorial that shows a good example for this?
UPDATE:
I kind of figured it out myself. Here’s my code:
-(id) init
{
if( (self=[super init]) ) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
self.isTouchEnabled = YES;
//These are declared in the .h class
blackArray = [[NSMutableArray alloc]init];
whiteArray = [[NSMutableArray alloc]init];
black1 = [CCSprite spriteWithFile:@"b1.png"];
black1.position = ccp(100, 160);
black2 = [CCSprite spriteWithFile:@"b2.png"];
black2.position = ccp(105, 150);
white = [CCSprite spriteWithFile:@"w1.png"];
white.position = ccp(150, 150);
white2 = [CCSprite spriteWithFile:@"w2.png"];
white2.position = ccp(80, 160);
[self addChild:black1 z:1 tag:1];
[self addChild:black2 z:1 tag:2];
[self addChild:white z:1 tag:3];
[self addChild:white2 z:1 tag:4];
[blackArray addObject:black1];
[blackArray addObject:black2];
[whiteArray addObject:white];
[whiteArray addObject:white2];
}
return self;}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
endTouch = location;
posX = endTouch.x;
//Minimum swipe length
posY = ccpDistance(beginTouch, endTouch);
//selectedSprite is a sprite declared in .h file
if(selectedSprite.tag == 1 || selectedSprite.tag == 2)
{
//action here
}
if([whiteArray count] > 0)
{
if(selectedSprite.tag == 3 || selectedSprite.tag == 4)
{
//action here
}
[whiteArray removeObject:selectedSprite];
if([whiteArray count] == 0)
{
//Go to game over
}
}}
This doesn’t look pretty but it works. Anyway, if there’s a better way to implement this than how I am currently doing it please let me know.
Make your arrays (arrayBlack and arrayWhite) mutable. Then,