HTML5GAME :: Phaser Js 기본 타이머 사용 설명서

달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

Phaser Js 기본 타이머 사용 설명서 


타이머는 게임 개발의 큰 부분을 합니다. 플레이어가 메시지를 읽을 때까지 몬스터를 공개하거나 게임의 다음 부분이 나타나는 주기를 설정할 수 있습니다. 또한 타이머를 사용하여 카운트 업 또는 카운트 다운 할 수 있습니다.


자 이제 Phaser js에서 사용할수 있는 타이머를 확인해 보겠습니다.


원샷 타이머

원샷 타이머는 지정된 시간 (초) 후에 이벤트를 한 번 실행하는 타이머입니다. 지연 타이머로 생각할 수도 있습니다. 아래의 예제는 5 초 후에 delayOver 함수를 호출합니다.

game.time.events.add (Phaser.Timer.SECOND * 5, this.delayOver, this);



루핑 타이머

루핑 타이머는 원샷과 거의 같은 방식으로 작동하지만 멈출 때까지 기능을 계속 실행합니다. 이 코드는 초마다 addMonster라는 함수를 호출합니다.

game.time.events.loop (Phaser.Timer.SECOND, this.addMonster, this);



타이머 멈춤

타이머를 정지 시키려면 먼저 변수에 할당하여 참조를 추가해야합니다.

this.monsterTimer = game.time.events.loop (Phaser.Timer.SECOND, this.addMonster, this);

그런 다음 타이머를 멈추고 싶을 때 다음과 같이 타이머를 끌 수 있습니다 :

game.time.events.remove (this.monsterTimer);



시간 조정

Phaser 타이머는 밀리 초 단위로 측정됩니다. 1000 밀리 초는 1 초와 같습니다. Phaser.Timer.SECOND를 사용하는 것은 첫 번째 매개 변수로 1000을 배치하는 것과 동일하지만 상수를 사용하면 더 쉽게 읽을 수 있습니다. 이 상수를 곱하거나 나눔으로써 함수가 호출되는 빈도를 조정할 수 있습니다.

Phaser.Timer.SECOND = 1 초

Phaser.Timer.SECOND * 5 = 5 초

Phaser.Timer.SECOND / 2 = 0.5 초 또는 함수를 초당 두 번 호출

Phaser.Timer.SECOND / 10 = 10 분의 1 초



카운트 다운 타이머 만들기

레벨이나 게임을 완료하기 위해 일정 시간 (분 또는 초)을 사용자에게 부여하려고한다고 가정 해보십시오. 게임을 끝내기 위해 원샷 타이머를 설정하면됩니다. 그러나 남은 시간 (초)을 표시하려면 카운트 다운 시계를 설정해야합니다. 우리는 초를 잡아두고 루프 타이머에서 초당 하나를 뺀 변수를 설정함으로써 이것을 할 수 있습니다. 초를 초로 변환 할 수도 있습니다. 다음은 이를 수행 할 수있는 몇 가지 샘플 코드입니다.



var StateMain = {

    preload: function() {},

    create: function() {

        //total time until trigger

        this.timeInSeconds = 120;

        //make a text field

        this.timeText = game.add.text(game.world.centerX, game.world.centerY, "0:00");

        //turn the text white

        this.timeText.fill = "#ffffff";

        //center the text

        this.timeText.anchor.set(0.5, 0.5);

        //set up a loop timer

        this.timer = game.time.events.loop(Phaser.Timer.SECOND, this.tick, this);

    },

    tick: function() {

        //subtract a second

        this.timeInSeconds--;

        //find how many complete minutes are left

        var minutes = Math.floor(this.timeInSeconds / 60);

        //find the number of seconds left

        //not counting the minutes

        var seconds = this.timeInSeconds - (minutes * 60);

        //make a string showing the time

        var timeString = this.addZeros(minutes) + ":" + this.addZeros(seconds);

        //display the string in the text field

        this.timeText.text = timeString;

        //check if the time is up

        if (this.timeInSeconds == 0) {

            //remove the timer from the game

            game.time.events.remove(this.timer);

            //call your game over or other code here!

            this.timeText.text="Game Over";

        }

    },

    /**

     * add leading zeros to any number less than 10

     * for example turn 1 to 01

     */

    addZeros: function(num) {

        if (num < 10) {

            num = "0" + num;

        }

        return num;

    },

    update: function() {}

}


Posted by 마스터킹
|