2013-01-24 39 views
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];} 

我希望有人能夠幫助我。

回答

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]; 
} 
+0

這是精靈下降,我想改變的數量。我仍然在學習cocos2d和objective-c,所以我不懂如何使用伊娃。你能舉出一個例子或者一個有很好例子的鏈接嗎? – user1597438

+0

伊娃是一個類寬變量。簡單地在你的實現文件中定義一個NSInteger,然後像下面這樣:NSInteger _nameOfIvar; –

+0

從我所看到的,根本沒有發送精靈的編號。你只需每隔2秒創建一個新的。哦,spriteDropDone方法做了什麼? –