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에서 게임을 만들때 우리는 스프라이트, 텍스트 또는 버튼 같은 간단한 객체를 사용하고 있습니다. Phaser에 글쓰기를 정말 좋아하지만 플래시 게임을 만들 때 사용하지 않을 때가 있습니다. 수업 내에서 다양한 요소를 만들 수 있습니다. Phaser로는 똑같은 일을 할 수는 없지만 복잡한 대상을 만드는 함수를 만드는 것이 가능합니다. 복잡한 객체는 단순히 자식 요소가 포함 된 그룹을 설명하는 데 사용하는 구문입니다. 이런 식으로, 나는 기본적으로 내가했던 것과 똑같은 결과를 얻을 수있었습니다.


이 예제에서는 버튼 객체를 만드는 함수를 작성했습니다. 이 경우 Phaser 프레임 워크에 내장 된 버튼이 아니라 텍스트 필드와 스프라이트가 포함 된 그룹입니다. 이것은 필요한 버튼이 무엇인지 모르는 상황에서 유용하기 때문에 한 줄의 코드로 새로운 버튼을 제작 할수 있습니다.


나는 최근에 한 게임에서 우주선 내부의 글자와 다른 게임에서의 거품으로 된 글자로 알파벳을 배우는 아이들을 위해 쓰고있는 몇 가지 게임에 대해이 기술을 사용했습니다. 나는 단순히 makeBubble 또는 makeShip 함수를 작성하고 필요한 모든 것을 만들기 위해 알파벳을 반복했습니다.


이 기술을 사용하려면 4 가지 작업을 수행해야합니다.

1. 스타일 (텍스트, 색상, 크기, 프레임)에 대한 매개 변수가있는 함수 만들기

2. 그 스타일 선택을 반영하는 자식 요소 만들기 (스프라이트, 텍스트 필드)

3. 그룹을 만들고 그 안에 요소들을 넣으십시오.

4. 그 그룹을 객체로 반환한다.


makeButton 함수로이 작업을 수행하고 create 함수에 버튼을 만들었습니다.





var StateMain = {

    preload: function () {

     game.load.spritesheet("colors","images/colors.png",150,50);

    },

    create: function () {      

       var btnYes=this.makeButton("YES",1);

       btnYes.y=game.height*.25;

       btnYes.x=game.world.centerX;

       var btnNo=this.makeButton("NO",0);

       btnNo.y=game.height*.75;

       btnNo.x=game.world.centerX;

       this.statusText=game.add.text(game.world.centerX,game.world.centerY,"");

       this.statusText.fill="#ffffff";

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

    },

    makeButton:function(text,color)

    {

     //create the back for the button

     var back=game.add.image(0,0,"colors");

     back.frame=color;

     //create the label for the button

     //and set the text to the text parameter passed down

     var label=game.add.text(0,0,text);

     back.anchor.set(0.5,0.5);

     label.anchor.set(0.5,0.5); 

     //create the group

     var buttonGroup=game.add.group(); 

     //add the sprite and the label to the group

     buttonGroup.add(back);

     buttonGroup.add(label); 

     //groups can not take input so we need to add the

     //listener for the click

     back.inputEnabled=true;

     back.events.onInputDown.add(this.buttonClicked,this);

     //return the group as the button

     return buttonGroup;

    },

    buttonClicked:function(target)

    {

     //since the target is the sprite

     //we get the parent of the target

     //

     var group=target.parent;

     //the label is the second child we added to the

     //group so we can find it at index 1

     //the back sprite is found at index 0

     var label=group.getChildAt(1); 

     this.statusText.text=label.text; 

    },

    update: function () {

    }

}


Posted by HammerOh
|

모바일의 부상으로, 나는 더 자주 객체의 크기를 조정해야한다는 것을 알게되었습니다. 이것은 화면 크기의 차이를 보완하기위한 것입니다. 높이나 너비와 같은 속성 하나를 변경하는 것은 쉽지만 이미지 비율이 비례해야하는 것이 더 좋으므로 다음과 같은 공식이 필요합니다.

new_width = old_width*(new_height/old_height);


가로 세로 비율 유지하기

Phaser에서 걱정할 필요가 없습니다.스프라이트의 높이나 너비를 변경하면 스케일 객체 속성이 업데이트됩니다. 예를 들어 스프라이트의 너비를 변경하면 scale.x 속성이 변경됩니다.


너비를 변경하면 scale.x를 scale.y로 복사합니다.

mySprite.scale.y=mySprite.scale.x;


높이를 변경하면 반대로 하면됩니다.

mySprite.scale.x=mySprite.scale.y;



실제 샘플 코드 입니다.

var StateMain = {

 

    preload: function () {

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

    },

 

    create: function () {

        

     this.makeDoughnuts();

     game.input.onDown.add(this.makeDoughnuts, this);

    },

    makeDoughnuts:function()

    {

     for (var i = 0; i < 5; i++) {

     var doughnut=game.add.sprite(game.world.randomX,game.world.randomY,"doughnut");

 

     doughnut.width=game.rnd.integerInRange(50, 150);

     //when we change the width, we automatically change the scale.x

     //setting the scale.y to scale.x will make the

     //object proportional

     doughnut.scale.y=doughnut.scale.x;

     }

    },

    update: function () {

 

    }

 

}


'PHASER.JS' 카테고리의 다른 글

Phaser Audio Delay  (0) 2018.10.12
Complex Objects – Phaser  (0) 2018.10.12
Phaser Js 코드 템플레이트 (Text)  (0) 2018.10.12
Phaser Js 코드 템플레이트 (Button)  (0) 2018.10.12
Phaser Js 코드 템플레이트 (Physics)  (0) 2018.10.11
Posted by HammerOh
|

Add Text

설명 : 스테이지에 텍스트 입력란 추가

var myText=game.add.text(x,y,text);


Center text

설명 : x 앵커를 너비의 50 %로 설정하여 텍스트 위치를 가운데로 맞춤

myText.anchor.x = Math.round(myText.width * 0.5) / myText.width;


Text font

설명: 텍스트의 폰트명 설정

myText.font="font_name";


Set text color

설명 : 텍스트 필드 색상을 설정합니다.

myText.fill="#textColor";


Font size

설명 : 텍스트 필드의 글꼴 크기를 설정합니다.

myText.fontSize=64;


Fancy Fonts

설명 : 텍스트 추가 기능의 네 번째 매개 변수를 사용하여 고급 글꼴 속성을 설정합니다.

this.titleText=game.add.text(x,y,text,{ font: "size fontName", fill: "color", stroke: "color", strokeThickness: number, align:string });

Posted by HammerOh
|