Skip to content
XUEHUANG521 edited this page Oct 20, 2021 · 8 revisions

Introduction

Time limit in the racing game is one of the key function that affect user experience, this is designed to tell the player how much time is left in game they can accomplish the tasks in game.

To facilitate results being displayed to the player, the Timer screen was created. Currently, it displayed as the text at the middle top corner which allow the player to watch time left.

To meet the timeout lose condition, timer reach to end time must be met. This condition is observed inside the class MainGameScreen. and it would determine the appearance of EndGameDisplay, which is one of the UIComponent of EndGameDisplay.

Code Implementation

Creation of MainGameTimerDisplay

Called directly from MainGameScreen, when timer goes to endTime, the MainGameTimerDisplay will trigger LOSS_TIMED condition in EndGameScreen, here is how timer been settle down.

   public void addActors() {
        table = new Table();
        table.setFillParent(true);
        table.top().padTop(30f);

        ServiceLocator.getResourceService().loadTexture(TIMER_BACKGROUND);
        ServiceLocator.getResourceService().loadAll();
        Image timerBackground =
                new Image(
                        ServiceLocator.getResourceService()
                                .getAsset(TIMER_BACKGROUND, Texture.class));

        timerLabel = new Label(getCurrentTime(), skin, "title");
        timerLabel.setAlignment(Align.center);

        table.stack(timerBackground, timerLabel);

        stage.addActor(table);
    }

Once the initial timer text has been set. update method would be called at each second to set time and then tick, updateLabel and checkTimerEnd method would be called step by step to ensure the timer in the MainGameScreen is running fully functionally.

     @Override
    public void update() {
        long currentTime = ServiceLocator.getTimeSource().getTime();
        if (currentTime - lastTime >= TIMER_TICK_RATE) {
            lastTime = currentTime;
            tick();
            updateLabel();
            checkTimerEnd();
        }
    };
     public void tick() {
         if (getMinutes() < 59) {
            timerTime += 1;
          } else {
            timerTime += 41;
            if (getHours() > 23) {
                timerTime = getMinutes();
         }
        }
     };

     public void updateLabel() {
        timerLabel.setText(getCurrentTime());

        int timeUntilEnd = TIMER_END - timerTime;
        if (timerStatus == TimerStatus.NORMAL && timeUntilEnd > 0 && timeUntilEnd <= 100) {
            timerLabel.setColor(255, 0,0, 1f);
            timerLabel.addAction(Actions.alpha(0));
            timerLabel.addAction(Actions.forever(Actions.sequence(
                    Actions.fadeIn(1f),
                    Actions.fadeOut(1f))));
            timerStatus = TimerStatus.FLASHING;
        }
    }

    public void checkTimerEnd() {
        if (timerTime == TIMER_END) {
            logger.debug("Timer has finished");
            entity.getEvents().trigger("timer_ended");
        }
    }

Display in MainGameScreen

![](Screen Shot 2021-10-21 at 12.40.50 am)

Clone this wiki locally