現在作成中のアプリはほぼ完成しましたが、必要な機能を 1 つ見つけようとして苦労しています。ランダムな間隔と位置で画面にドロップする 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];}
これで私を助けてくれる人がいることを願っています。