0
我幾乎完成了我目前正在製作的應用程序,但我很難找出我需要的一個功能。我有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];}
我希望有人能夠幫助我。
這是精靈下降,我想改變的數量。我仍然在學習cocos2d和objective-c,所以我不懂如何使用伊娃。你能舉出一個例子或者一個有很好例子的鏈接嗎? – user1597438
伊娃是一個類寬變量。簡單地在你的實現文件中定義一個NSInteger,然後像下面這樣:NSInteger _nameOfIvar; –
從我所看到的,根本沒有發送精靈的編號。你只需每隔2秒創建一個新的。哦,spriteDropDone方法做了什麼? –