2012-07-19 65 views
0

我運行了下面的代碼,期望在不同的時間間隔(例如,在1秒之後,在2.6秒之後等)調度三次後續調用。「displayWarningMessage「但沒有工作(它只在第一次顯示按摩)。Cocos2d:麻煩調度在特定時間間隔多次調用一個方法

我沒有在調度程序中找到一個方法簽名,它可以執行多次顯示並具有特定延遲的工作。任何人都有一些建議?

[self scheduleOnce:@selector(displayWarningMessage) delay:0.7f]; 
[self scheduleOnce:@selector(displayWarningMessage) delay:1.7f]; 
[self scheduleOnce:@selector(displayWarningMessage) delay:3.7f]; 

回答

1

問題在於,當您調用第一個計劃時,它會被成功調度。但接下來的立即通話是拋出警告的東西

CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: X.2 to X.2 

你可以在日誌中看到這個。

你可以做的是當選擇器被調用時,在方法結束時你可以再次安排下一次,直到你完成。您可以通過一個計數器來跟蹤它被調用的次數,將所有間隔放在一個數組中,然後安排下一個選擇器在由計數器識別的特定索引處的間隔。像這樣:

NSArray *intervals = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.7],[NSNumber numberWithFloat:1.7],[NSNumber numberWithFloat:3.7], nil]; 

int counter = 0; 

//schedule it for the first time with object at index counter/index 0 
[self scheduleOnce:@selector(displayWarningMessage) delay:[(NSNumber *)[intervals objectAtIndex:counter]] floatValue]; 

現在在你的選擇,做這樣的事情:

-(void)displayWarningMessage 
{ 
    //do all your stuff here 

    //increment counter 
    counter ++; 

    if(counter < [intervals count]) 
    { 
     //schedule it for the next time with object at index counter/index 
     [self scheduleOnce:@selector(displayWarningMessage) delay:[(NSNumber *)[intervals objectAtIndex:counter]] floatValue]; 
    } 
} 

intervalscounter應該是道高德班。

+0

非常感謝你,是一個不錯的主意,但我嘗試過,並沒有工作。作爲一個證明,在代碼中,你建議我將調用替換爲scheduleOnce的調用,而不是所有控件下面的註釋「在這裏做所有的事情」,它仍然不會調用它。我不確定是否我做錯了什麼或者什麼。我正在使用Cocos2d v2.0和ARC啓用。現在對於你的想法+1投票,但我保持答案不被接受,因爲沒有完全解決問題(但會接受,如果沒有別的出現)。 – mm24 2012-07-19 13:26:37

0

試試這個:

- (void)displayWarningMessage { 
    //Stuff 
} 

- (void)callStuff { 
    CCCallFunc *call = [CCCallFunc actionWithTarget:self selector:@selector(displayWarningMessage)]; 
    CCDelayTime *delay1 = [CCDelayTime actionWithDuration:0.7f]; 
    CCDelayTime *delay2 = [CCDelayTime actionWithDuration:1.7f]; 
    CCDelayTime *delay3 = [CCDelayTime actionWithDuration:3.7f]; 
    CCSequence *actionToRun = [CCSequence actions:delay1, call, delay2, call, delay3, call, nil]; 
    [self runAction:actionToRun]; 
} 

這應該爲你想要做什麼工作,至少這就是我想像這樣做。我相當肯定你可以在一個CCSequence中多次調用這個CCCallFunc,而不必一次創建它三次。當然,如果需要的話,你也可以使這些延遲變化。讓我知道事情的後續。

-1

方法已創建。

[self schedule: @selector(displayWarningMessage:) interval:3.2f]; 

-(void) displayWarningMessage:(ccTime) delta 
{ 

    CCLOG(@"alert........!!!!!!"); 

} 

在未檢測到警告消息時使用調用方法。

+1

請在代碼前使用4個空格將其格式化爲代碼。 – 2016-01-08 13:16:37