2012-02-07 75 views
3

我做了一個簡單的計時器,但在試圖增加定時器的速度隨着時間的推移,所以我基本上希望計時器的間隔爲1.0,每隔10秒我希望計時器的間隔減少0.1Cocos2d進度間隔減少

我該怎麼做呢?

[self schedule:@selector(tick:)interval:1.0]; 

這是init部分

-(void)tick:(ccTime)dt{ 
myTime = myTime+1; 
totalTime = myTime; 

[timeLabel setString:[NSString stringWithFormat:@"%i", totalTime]]; 
} 

這是計時器。

這是一個基本的計時器,它的間隔爲1.0秒,但我希望間隔每10秒減少10%。

回答

2
在h文件

@property(nonatomic,readWrite)CGFloat lastUpdatedInterval; 

in.m文件

,初始化self.lastUpdatedInterval = 1.0;

然後調用

[self schedule:@selector(tick:)interval:self.lastUpdatedInterval]; 

在更新循環,

-(void)tick:(ccTime)dt 
{ 
    if(myTime>9 && (myTime%10 == 0)) 
    { 
     [self unschedule:@selector(tick:)]; 
     self.lastUpdatedInterval = self.lastUpdatedInterval - (self.lastUpdatedInterval*0.1); 
     [self schedule:@selector(tick:)interval:self.lastUpdatedInterval]; 
    } 

myTime = myTime+1; 
totalTime = myTime; 

[timeLabel setString:[NSString stringWithFormat:@"%i", totalTime]]; 

} 
+0

只需添加其他條件,以防止它變爲負值。 – samfisher 2012-02-07 19:04:20

+0

'code'@property(nonatomic,readWrite)CGFloat lastUpdatedInterval; 'code'你能告訴我那是什麼嗎?謝謝 – sahil 2012-02-08 15:01:03

+0

我試過,但它說:屬性'lastUpdatedInterval'需要定義方法'setLastUpdatedInterval:' - 使用(at)合成,(at)動態或提供方法實現。對不起,我是新來的。有沒有更簡單的方法?在.m文件中寫入 – sahil 2012-02-08 16:04:08