问题描述
我想用cocos2d创建一个2DiPhone游戏,需要设定一个定时器,应该如何进行操作呢?
原问题:How can I create a count down timer for cocos2d?
问题解答
回答1:答:JustinB(最佳答案)就这个问题本身来讲,不应该用NSTimer进行操作。Coco2d框架可以提供一种调度程序(scheduler),将停止/继续这种指令融进游戏中,使用方法如下:
-(id) init{ if( ! [super init] )return nil; // schedule timer [self schedule: @selector(tick:)]; [self schedule: @selector(tick2:) interval:0.5]; return self;}-(void) tick: (ccTime) dt{ // bla bla bla}-(void) tick2: (ccTime) dt{ // bla bla bla}
答:seenu在cocos2d中,定时器有一种默认更新的功能,所以可以尝试如下方法:
[self schedule:@selector(update:)];- (void)update:(ccTime)dt {}
答:Ziminji如果不用'schedule'的方法,而是想用NSTimer创建定时器的话,你可以按照下述方法创建一个类(class):ZIMCountdownTicker.h
#import <Foundation/Foundation.h>extern NSString * const ZIMCountdownTickerTickAction;extern NSString * const ZIMCountdownTickerResetAction;@protocol ZIMCountdownTickerProtocol;/*! @class ZIMCountdownTicker @discussionThis class creates a countdown ticker. @updated 2011-03-05 */@interface ZIMCountdownTicker : NSObject { @privateNSTimer *_timer;id<ZIMCountdownTickerProtocol> _delegate;NSTimeInterval _interval;double _period;double _value;}/*! @methodinitWithDelegate:withTimeInterval:forTimePeriod: @discussion This method instantiate an instance of this class with the specified parameters. @param delegateA reference to a class that has implemented ZIMCountdownTickerProtocol. @param intervalThe time interval in seconds to be used when running the countdown ticker. @param period The time period in seconds for which countdown ticker will run. @updated 2011-03-05 */- (id) initWithDelegate: (id<ZIMCountdownTickerProtocol>)delegate withTimeInterval: (NSTimeInterval)interval forTimePeriod: (double)period;/*! @methodstart @discussion This method will start the countdown ticker. @updated 2011-03-05 */- (void) start;/*! @methodstop @discussion This method will stop the countdown ticker. @updated 2011-03-05 */- (void) stop;/*! @methodreset @discussion This method will reset the countdown ticker. @updated 2011-03-06 */- (void) reset;/*! @methodvalue @discussion This method will return the countdown ticker’s current value; however, using this method will cause the ticker to stop. @returnThe countdown ticker’s current value. @updated 2011-03-05 */- (double) value;@end@protocol ZIMCountdownTickerProtocol <NSObject>@optional/*! @methodcountdownTicker:didUpdateValue:withAction: @discussion This method will notify the delegate with the current value. @param ticker A reference to tiggering ticker. @param value The current value. @param action The action that tiggered this method. @updated 2011-03-05 */- (void) countdownTicker: (ZIMCountdownTicker *)ticker didUpdateValue: (double)value withAction: (NSString *)action;/*! @methodcountdownTickerDidFinish: @discussion This method will notify the delegate that the countdown ticker finished. @param ticker A reference to tiggering ticker. @updated 2011-03-05 */- (void) countdownTickerDidFinish: (ZIMCountdownTicker *)ticker;@end
ZIMCountdownTicker.m
// Ziminji Classes#import 'ZIMCountdownTicker.h'NSString * const ZIMCountdownTickerTickAction = @'ticker.tick';NSString * const ZIMCountdownTickerResetAction = @'ticker.reset';/*! @category ZIMCountdownTicker (Private) @discussionThis category defines the prototypes for this class’s private methods. @updated 2011-03-05 */@interface ZIMCountdownTicker (Private) /*! @method countdown: @discussionThis method is called by the timer to decrement the counter’s value and will send the delegate the updated value. @param timer The timer currently in use. @updated 2011-03-06 */ - (void) countdown: (NSTimer *)timer;@end@implementation ZIMCountdownTicker- (id) initWithDelegate: (id<ZIMCountdownTickerProtocol>)delegate withTimeInterval (NSTimeInterval)interval forTimePeriod: (double)period { if (self = [super init]) {_delegate = delegate;_interval = interval;_period = period;_value = period;_timer = nil; } return self;}- (void) start { if (_timer == nil) {_timer = [NSTimer scheduledTimerWithTimeInterval: _interval target: self selector: @selector(countdown:) userInfo: nil repeats: YES]; }}- (void) stop { if (_timer != nil) {[_timer invalidate];_timer = nil; }}- (void) reset { [self stop]; _value = _period; if ((_delegate != nil) && [_delegate respondsToSelector: @selector(countdownTicker:didUpdateValue:withAction:)]) {[_delegate countdownTicker: self didUpdateValue: _value withAction: ZIMCountdownTickerResetAction]; }}- (double) value { [self stop]; return _value;}- (void) countdown: (NSTimer *)timer { _value -= 1; if ((_delegate != nil) && [_delegate respondsToSelector: @selector(countdownTicker:didUpdateValue:withAction:)]) {[_delegate countdownTicker: self didUpdateValue: _value withAction: ZIMCountdownTickerTickAction]; } if (_value <= 0) {[self stop];if ((_delegate != nil) && [_delegate respondsToSelector: @selector(countdownTickerDidFinish:)]) { [_delegate countdownTickerDidFinish: self];} }}- (void) dealloc { if (_delegate != nil) {[_delegate release]; } if (_timer != nil) {[_timer invalidate]; } [super dealloc];}@end
答:lostInTransit(反面教材)
-(id) init{ if( ! [super init] )return nil; // schedule timer [self schedule: @selector(tick:)]; [self schedule: @selector(tick2:) interval:0.5]; return self;}-(void) tick: (ccTime) dt{ //some function here}-(void) tick2: (ccTime) dt{ //some function here}