如何延迟精灵的出现?

时间:2013-01-24 07:47:50

标签: objective-c xcode cocos2d-iphone

我差不多已经完成了我正在制作的应用程序,但是我很难找到我需要的一个功能。我有5个不同的精灵,随机间隔和位置下降到屏幕上。我的问题是我需要告诉我的代码,在一定的时间内,例如20秒,一次只会出现一个精灵。然后在20秒后,更多会同时开始下降。这是我的方法用于删除精灵的方式:

//The init method
-(id) init
{
if((self=[super init])) {
    //Enable touch
    self.isTouchEnabled = YES;

    //Allocate and initialise
    sprites = [[NSMutableArray alloc]init];

    CGSize winSize = [[CCDirector sharedDirector]winSize];

    //Add the sprites
    [self schedule:@selector(scheduleDrop:)interval:2.0];
}
return self;
}

//Method to drop sprites
-(void)spriteDrop
{
    CGSize winSize = [[CCDirector sharedDirector]winSize];

int RandomX = (arc4random() % 200);

NSString *strSprite = @"1.png";

switch(arc4random() % 5){
    case 1:
        strSprite = @"1.png";
        break;
    case 2:
        strSprite = @"2.png";
        break;
    case 3:
        strSprite = @"3.png";
        break;
    case 4:
        strSprite = @"4.png";
        break;
    case 5:
        strSprite = @"5.png";
        break;
}

sprite = [CCSprite spriteWithFile:strSprite];
sprite.position = ccp(RandomX, 500);
sprite.scaleX = 40 / sprite.contentSize.width;
sprite.scaleY = 150 / winSize.height;

int posMinY = sprite.contentSize.width / 2;
int posMaxY = winSize.height - sprite.contentSize.height / 2;
int range = posMaxY - posMinY;
int actual = (arc4random()%range);
currentPos = actual;

[self addChild:baby];

int minDur = 2.0;
int maxDur = 5.0;
int rangeDur = maxDur - minDur;
int actualDur = (arc4random()%rangeDur) + minDur;

id drop = [CCMoveTo actionWithDuration:actualDur position:ccp(actual, -sprite.contentSize.height/2)];
id dropDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteDropDone:)];
[sprite runAction:[CCSequence actions:drop, dropDone, nil]];

sprite.tag = 1;
[sprites addObject: sprites];
}

//This is the method that schedules the drop
-(void)scheduleDrop:(ccTime)dt
{
[self spriteDrop];}

我希望有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

不确定您是否希望更改精灵的数量,或者(视觉上)类型随时间变化。

一种解决方案是使用ivar _numberOfSpritesToBeDropped。这可以在由调度器以所请求的时间间隔(例如,20秒)控制的方法中增加。然后你就可以在控制精灵分散的方法中使用这个ivar ......

如果你只想每20岁添加不同种类的精灵。您只需将随机语句更改为

即可
switch(arc4random() % _numberOfSpritesToBeDropped)...

旁注:你的spriteDrop方法似乎不仅仅是简单地删除精灵,而且肯定会分成两种不同的方法。

根据我们下面的讨论,这可能是这样的:

NSInteger _maximumCurrentNumberOfSpritesAllowed;

-(id) init{
    self = [super init];

    if(self) {
        [self setup];
    }
    return self;
}


-(void) setup{
    self.isTouchEnabled = YES;
    sprites = [[NSMutableArray alloc]init];
    _maximumCurrentNumberOfSpritesAllowed = 1;
    [self schedule:@selector(scheduleDrop:)interval:2.0];
}


-(void)spriteDrop{
    if (sprites.count < _maximumCurrentNumberOfSpritesAllowed) {

        CGSize winSize = [[CCDirector sharedDirector]winSize];

        NSString *spriteName = [self randomSpriteName]; // I'd consider doing something similar with the position etc. as well
        sprite = [CCSprite spriteWithFile:spriteName];

        int RandomX = (arc4random() % 200);
        sprite.position = ccp(RandomX, 500);
        sprite.scaleX = 40 / sprite.contentSize.width;
        sprite.scaleY = 150 / winSize.height;

        int posMinY = sprite.contentSize.width / 2;
        int posMaxY = winSize.height - sprite.contentSize.height / 2;
        int range = posMaxY - posMinY;
        int actual = (arc4random()%range);
        currentPos = actual;

        [self addChild:baby];

        int minDur = 2.0;
        int maxDur = 5.0;
        int rangeDur = maxDur - minDur;
        int actualDur = (arc4random()%rangeDur) + minDur;

        id drop = [CCMoveTo actionWithDuration:actualDur position:ccp(actual, -sprite.contentSize.height/2)];
        id dropDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteDropDone:)];
        [sprite runAction:[CCSequence actions:drop, dropDone, nil]];

        sprite.tag = 1;
        [sprites addObject: sprites];
    }
}


-(NSString *)randomSpriteName{
    NSString *strSprite; 
    switch(arc4random() % 5){
        case 1:
            strSprite = @"1.png";
            break;
        case 2:
            strSprite = @"2.png";
            break;
        case 3:
            strSprite = @"3.png";
            break;
        case 4:
            strSprite = @"4.png";
            break;
        case 5:
            strSprite = @"5.png";
            break;
        default:
            strSprite = @"1.png";
            break;
    }

    return strSprite;
}


// Call this method with a scheduler at whatever interval you'd like
-(void) increaseMaximumNumberOfSpritesAllowed{
    _maximumCurrentNumberOfSpritesAllowed++;
}

//This is the method that schedules the drop
-(void)scheduleDrop:(ccTime)dt{
    [self spriteDrop];
}