2012-06-09 25 views
0

我正在开发一个2D游戏,其中在我的游戏屏幕上我必须实现从30秒到0秒的反向计时器,并且如果玩家不移动他的角色,他会否则他会输,比赛将结束。如何执行反向倒计时然后调用方法

这是我的init方法:

-(id)init 
    { 
    if(self==[super init]) 
    { 
     self.isTouchEnabled=YES; 
     self.isAccelerometerEnabled=YES; 
     CGSize size=[[CCDirector sharedDirector] winSize]; 
     screenwidth=size.width; 
     screenheight=size.height; 
     west_pic=[CCSprite spriteWithFile:@"west_pic.png"]; 
     west_pic.anchorPoint=ccp(1, 1); 
     west_pic.scaleX=1.4; 
     west_pic.position=ccp(size.width, size.height); 
     [self addChild:west_pic]; 
     label1=[CCLabelTTF labelWithString:@"Level One" fontName:@"Arial" fontSize:20]; 
     label1.position=ccp(size.width/3.8,size.height/1.2); 
     [self addChild:label1]; 
     label2=[CCLabelTTF labelWithString:@"Lives :" fontName:@"ArcadeClassic" fontSize:18]; 
     label2.position=ccp(size.width/1.5,size.height/8.2); 
     [self addChild:label2]; 
     player=[CCSprite spriteWithFile:@"player.png"]; 
     player.position=ccp(size.width/1.7, size.height/2); 
     player.scale=0.2; 
     player.tag=2; 
     player.anchorPoint=ccp(1, 0); 
     [self addChild:player]; 
     [self schedule:@selector(updateplayer:) interval:1.0f/60.0f]; 

    } 
     return self; 
    } 
+0

..你有什么试过? – skram

+1

你可以从0开始到30,并且以这种方式考虑它,当用户启动它时是30(30-0 = 30),当30秒后它将是0(30-30)。 –

回答

0

例如,你可以有一个实例与秒数,即玩家必须不动的性格,那么你必须有ssome事件的方法,知道什么时候该字符开始和停止移动,创建可更新标签(如果你想显示剩余时间给玩家)和计划计时器,这将减少秒数。这样的水渍

- (void) onEnter 
{ 
    [super onEnter]; 

    // if player character not move on start 
    [self schedule:@selector(reduceRestTime) interval:1.f]; 
} 

- (void) onExit 
{ 
    [self unscheduleAllSelectors]; 
    [super onExit]; 
} 

- (void) onCharacterStop 
{ 
    m_restTime = // your time left. in your case, 30 sec 
    // if you have time label 
    [self updateRestTimeLabel]; 

    [self schedule:@selector(reduceRestTime) interval:1.f]; 
} 

- (void) onCharacterBeginMovement 
{ 
    [self unscheduleSelector:@selector(reduceRestTime)];  
} 

- (void) reduceRestTime 
{ 
    m_restTime--; 

    // if you have time label 
    [self updateRestTimeLabel]; 

    if(m_timeLeft == 0) 
    { 
     [self unscheduleSelector:@selector(reduceRestTime)]; 

     // your win code 
    } 

} 

- (void) updateRestTimeLabel 
{ 
    [m_timeLabel setString:[NSString stringWithFormat:@"Time left: %d", m_timeLeft]]; 
} 
+0

@Moroin - tahnx很多亲爱的,这真的帮了我很多 –

+0

@MoeezAkram欢迎你=) – Morion

相关问题