我几乎完成了我目前正在制作的应用程序,但我很难找出我需要的一项功能。我有 5 个不同的精灵以随机的间隔和位置落在屏幕上。我的问题是我需要告诉我的代码在一定时间内,例如 20 秒,一次只会出现 1 个精灵。然后在 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];}
我希望有人能够帮助我解决这个问题。