0

我正在尝试使用 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
    }
}}

这看起来不漂亮,但它有效。无论如何,如果有比我目前做的更好的方法来实现它,请告诉我。

4

1 回答 1

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
     }
   }
}
于 2013-01-31T05:04:54.140 回答