WEBGL :: WEBGL

달력

22025  이전 다음

  • 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

Phaser Js 로 게임 개발을 할때, Start 부분에서 사용되는 기본적인 코드에 대해서 알아봅시다.


Blank State

설명 : 필수 기능이있는 빈 상태 템플릿입니다.

var StateMain={      

   preload:function()

    {      

    },  

    create:function()

    {      

    },  

    update:function()

    {              

    }      

}


Switch states

설명 : 상태를 변경합니다. 예를 들어 게임이 종료 된 경우 game.state.start ( "StateGameOver")를 호출 할 수 있습니다.

game.state.start(state_name);


Start for Desktop

설명 : Phaser Js를 시작하는 데 필요한 기본 파일입니다.

var game;

window.onload = function()

{

    game=new Phaser.Game(480,640,Phaser.AUTO,"ph_game");

    game.state.add("StateMain",StateMain);

    game.state.start("StateMain");

}


Start for Dekstop/Mobile

설명 : 모바일을 지원하는 Phaser Js를 시작하는 데 필요한 기본 파일입니다.

var game;

var useLandscape=false;

window.onload = function () {

var isMobile=navigator.userAgent.indexOf("Mobile");

if (isMobile>-1)

     {

        isMobile=true;

     }

     else

     {

        isMobile=false;

     }

    if (isMobile==false) {

        //desktop laptop

        if (useLandscape == true) {

            game = new Phaser.Game(640, 480, Phaser.AUTO, "ph_game");

        } else {

            game = new Phaser.Game(480, 640, Phaser.AUTO, "ph_game");

        }

    } else {

        //mobile device

        game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, "ph_game");

    }

    game.state.add("StateMain",StateMain);

    game.state.start("StateMain");

}


Basic HTML

설명: Phaser Js 용 html

<!DOCTYPE html>

<html lang="">

<head>

    <meta charset="UTF-8">

    <title>Game Title Here</title>

    <script src="js/phaser.min.js">

    </script>

    <script src="js/main.js"></script>  

</head>

<body>

</body>

</html>


Full Screen Meta Tag

설명: 모바일 용 전체 화면에 html을 삽입하는 메타 태그

<meta name="viewport" content="user-scalable=0, initial-scale=1,minimum-scale=1, maximum-scale=1, width=device-width, minimal-ui=1">


Sample Init State

설명 :이 샘플을 사용하여 init 상태를 만듭니다. 초기화 상태는 실행을위한 베어 본 코드를 설정하고 로드 상태를 준비합니다.

var StateInit = {


    preload: function () {

        game.load.image("loadingEmpty", "images/loading/progress_none.png");

        game.load.image("loadingFull", "images/loading/progress_all.png");

        game.scale.forceOrientation(true, false);

        game.scale.enterIncorrectOrientation.add(this.wrongWay, this);

        game.scale.leaveIncorrectOrientation.add(this.rightWay, this);

    }

    , create: function () {

        game.state.start("StateLoad");

    }

    , update: function () {

    }

    , rightWay: function () {

        document.getElementById("wrong").style.display = "none";

    }

    , wrongWay: function () {

        document.getElementById("wrong").style.display = "block";

    }

}


Samepl Load State

설명 :로드 상태는 게임을 시작하기 전에 모든 것을 미리로드 할 수있게합니다.

var StateLoad = {

    preload: function () {

        var empty = game.add.image(0, 0, "loadingEmpty");

        var full = game.add.image(0, 0, "loadingFull");

        empty.x = game.world.centerX;

        empty.y = game.world.centerY;

        empty.anchor.set(0.5, 0.5);

        full.anchor.set(0, 0.5);

        full.x = game.world.centerX - empty.width / 2;

        full.y = empty.y;

        game.load.setPreloadSprite(full);

        //PRELOAD EVERYTHING HERE

    },

    create: function () {

        game.state.start("StateTitle");

    },

    update: function () {

    }

}

Posted by HammerOh
|

phaser를 배우는 개발자가 가장 먼저 배워야 할 것은 phaser에 이미지를 추가하는 방법입니다. 그리고 그과정은 정말 쉽습니다. 이미지를 라이브러리에로드 한 다음 스테이지에 배치하는 2 단계 프로세스입니다. 저는 그것을 초보자를위해 4단계 과정으로 정리해 보겠습니다.


1 단계. 기본 템플릿 다운로드

템플릿 폴더의 이름을 프로젝트 이름 (예 : ImageTest)으로 바꾸고 좋아하는 편집기로 폴더를 엽니다.


2 단계. 프로젝트에 이미지 추가하기

프로젝트 내부에 폴더를 만들고 내부에로드 할 이미지를 추가하십시오.

















3 단계. 이미지를 라이브러리에 로드하십시오.

이 코드를 stateMain.js 파일의 preload 함수 안에 넣으십시오.

game.load.image("monster","images/monster.png");


game.load.image는 다음과 같이 작동합니다.

game.load.image(unique_key,path_to_image);


4 단계. 이미지를 캔버스에 놓습니다.

create 함수 내에서 game.add.sprite 코드를 사용하십시오.

game.add.sprite(100,200,"monster");


game.load.sprite는 다음과 같이 작동합니다.

game.add.sprite(x,y,unique_key);



다음은이 작업을 수행하는 방법을 보여주는 비디오입니다.



Posted by HammerOh
|

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 HammerOh
|