我正在尝试使用cocos2d开发一个具有多个阶段的游戏。例如,在一个级别中,我有4个精灵,2个白色和2个黑色。如果玩家击中黑色精灵,游戏结束,如果他击中白色精灵,他就赢了。我如何实现一个条件,其中如果玩家击中白色精灵,它会检查场景中是否存在其他白色精灵,如果有,则游戏继续。如果没有,那么他去舞台清晰的场景?我尝试将精灵放在两个不同的数组中(arrayBlack和arrayWhite),但我仍然坚持我将如何为白色精灵制作条件。任何人都可以给我一个想法或建议或教程,为此展示一个很好的例子吗?
更新: 我有点想到了自己。这是我的代码:
-(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
}
}}
这看起来不漂亮但是有效。无论如何,如果有更好的方法来实现这个,而不是我目前正在做的事情,请告诉我。
答案 0 :(得分:2)
使您的数组(arrayBlack和arrayWhite)可变。然后,
if(user hit sprite1)
{
if([arrayBlack containsObject:sprite1])
{
[arrayBlack removeObject:sprite1];
// Game over
}
else
{
[arrayWhite removeObject:sprite1];
if(arrayWhite.count>0)
{
// Continue game
}
else
{
// Stage clear scene
}
}
}