0

我正在开发一个 2d 游戏,在我的游戏屏幕上,我必须在 30 秒到 0 秒之间实现一个反向计时器,如果玩家不移动他的角色,他将获胜,否则他将失败,游戏将结束。

这是我的初始化方法:

-(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;
    }
4

1 回答 1

0

例如,你可以有一个秒数的实例,该玩家不能移动角色,那么你必须有一些事件方法来知道角色何时开始和停止移动,创建可更新标签(如果你想显示其余的播放时间)和计划计时器,这将减少秒数。像这样

- (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]];
}
于 2012-06-09T12:00:47.087 回答