phaser/tutorial_02 ¤Î¥Ð¥Ã¥¯¥¢¥Ã¥×¥½¡¼¥¹(No.5)


[[Programing]] > [[Phaser]] > ¥Á¥å¡¼¥È¥ê¥¢¥ë2

#title(Phaser¤Î¥²¡¼¥à¤ò²þÎɤ·¤Æ¤ß¤¿)

#ref(Phaser/jumping_dude.png)

* ³µÍ× [#u86e3885]

[[Á°²ó¤Î¥Á¥å¡¼¥È¥ê¥¢¥ë>phaser/tutorial_01]]¤Ï¡¢Phaser¤Ë¤è¤ë´ðËÜŪ¤Ê¥²¡¼¥à¤Îºî¤êÊý¤ò²òÀ⤷¤Æ¤ß¤¿(ËÝÌõ¤À¤±¤É)¡£

º£²ó¡¢¤³¤ì¤òÆȼ«¤Ë²þÎɤ·¤Æ¡¢¤â¤¦¾¯¤·¥²¡¼¥à¤Ý¤¯¤·¤Æ¤ß¤ë¡£

¤¢¤Î¥Á¥å¡¼¥È¥ê¥¢¥ë¤Ç¤ÏÉÔ­¤·¤Æ¤¤¤¿¾ðÊ󤬡¢¤¤¤¯¤Ä¤«Êä´°¤Ç¤­¤Æ¤¤¤ë¤È»×¤¦¡£

¤Þ¤¢¡¢¥½¡¼¥¹¥³¡¼¥É¤ò¸«¤Æ¤â¤é¤¨¤Ðʬ¤«¤ëÉôʬ¤â¤¢¤ë¤È»×¤¦¤±¤É¡¢¤¤¤Á¤ª¤¦´Êñ¤Ë²òÀ⤷¤Æ¤ß¤Þ¤¹¡£

¤³¤³¤Ç¤Ï¡¢½çÈ֤˥½¡¼¥¹¥³¡¼¥É¤òÁȤßΩ¤Æ¤Æ¤¤¤¯¤Î¤Ç¤Ï¤Ê¤¯¡¢´°À®ÈǤÎÁ´ÂÎÁü¤ò¸«¤Æ¤â¤é¤Ã¤Æ¡¢ÆÃħŪ¤Ê¤È¤³¤í¤ò¥Ô¥ó¥Ý¥¤¥ó¥È¤Ç¼è¤ê¾å¤²¤Æ¤¤¤Þ¤¹¡£

- ´°À®Èǥǥ⠡ä http://www.catch.jp/program/phaser/002/index.html
- ¥½¡¼¥¹¥³¡¼¥É > https://github.com/ycatch/games-phaser-jumping-dude

** ÍøÍѾò·ï [#b43143bd]

Copyright 2014 Yutaka Kachi released under MIT license.

´Ö°ã¤¤¤Ê¤É¤¢¤ë¤«¤âÃΤì¤Þ¤»¤ó¡£

¤â¤·¤â¸«¤Ä¤±¤¿¤é¡¢[[Twitter @y_catch:https://twitter.com/y_catch]]¤ØÏ¢Íí¤¤¤¿¤À¤±¤ë¤È½õ¤«¤ê¤Þ¤¹¡£

* Ìܼ¡ [#ccab2b01]

#contents

* ²þÎÉÅÀ [#o061741e]

- ¥ë¡¼¥ë¡ÊÀ±¤¬¤É¤ó¤É¤óÁý¤¨¤Æ¤¤¤¯¡£À©¸Â»þ´ÖÀ©¡Ë
- ¥¿¥¤¥Þ¡¼¤ò»È¤¤¡¢»þ´ÖÀ©¸Â¥²¡¼¥à¤Ë¤·¤¿
- Phaser¤ËƱº­¤ÎBasic Project Template¤ò»È¤¦
- ¥°¥í¡¼¥Ð¥ëÊÑ¿ô¤ò»È¤ï¤º¡¢¥²¡¼¥à¤Î¥¹¥Æ¡¼¥¿¥¹¤¬Á«°Ü¤·¤Æ¤¤¤¯
- ¥á¥¤¥ó¥á¥Ë¥å¡¼¤È¥²¡¼¥à¥ª¡¼¥Ð¡¼²èÌ̤ÎÄɲÃ
- ¸ú²Ì²»

* Project Template [#d7625bd6]

Á°²ó¤Î¥Á¥å¡¼¥È¥ê¥¢¥ë¤Ï¡¢Ã±°ì¤Îindex.htm¥Õ¥¡¥¤¥ë¤Ë100¹Ô¤¯¤é¤¤¤ÎJavascript¥³¡¼¥É¤òËä¤á¹þ¤ó¤Ç¤¤¤¿¡£¤³¤ì¤Ï¡¢Ã±½ã¤À¤·¥µ¥¯¥µ¥¯ºî¤ì¤ë¤ó¤À¤±¤É¡¢°ìÊý¤Ç¡¢¤â¤¦¾¯¤·µ¡Ç½¤Î¿¤¤¥²¡¼¥à¤òºî¤ê¤¿¤¤¾ì¹ç¤È¤«¸«Ä̤·¤¬°­¤¤¤·¡¢¥°¥í¡¼¥Ð¥ëÊÑ¿ô¤ò»È¤¤¤¹¤®¤Æ¤¤¤ë¡£

¤½¤³¤Ç¡¢Phaser¤ËƱº­¤µ¤ì¤Æ¤¤¤ë¡¢Project Template¤Î¡ÖBasic ¡×¤ò»È¤¦¡£

¤³¤ì¤Ï¡¢Phaser¤Îresources/Project Templates ¤Ë¤¢¤ëPhaser¥²¡¼¥à¤Î¹üÁȤߤǡ¢Ê£¿ô¤Î¥Õ¥¡¥¤¥ë¤Ç¹½À®¤µ¤ì¤Æ¤¤¤ë¡£

¾Ü¤·¤¤»È¤¤Êý¤Ï¡¢¥³¥á¥ó¥È¤È¤·¤Æµ­ºÜ¤µ¤ì¤Æ¤¤¤ë¡£

¤Þ¤À¹üÁȤߤÀ¤±¤Ê¤Î¤Ç¡£¥Õ¥¡¥¤¥ë¤ò³«¤¤¤Æ¤â¤Ê¤Ë¤âɽ¼¨¤µ¤ì¤Ê¤¤¡£

* ¥Õ¥¡¥¤¥ë¹½À® [#nbee0dff]

´°À®ÈǤΥ²¡¼¥à¤Î¥Õ¥¡¥¤¥ë¹½À®¤Ï¡¢¤³¤ó¤Ê´¶¤¸¡£

JavaScript¤Ï¡¢Ê£¿ô¤Î¥Õ¥¡¥¤¥ë¤Ëʬ¤«¤ì¤Æ¤¤¤Æ¡¢js¥Õ¥©¥ë¥À¤ËÃÖ¤¤¤Æ¤¢¤ë¡£

¤Û¤ó¤È¤¦¤Ïñ°ì¤Îjs¥Õ¥¡¥¤¥ë¤ËÅý¹ç¤·¤Á¤ã¤¨¤Ð¤¤¤¤¤ó¤À¤±¤ì¤É¡¢³«È¯Ãæ¤Î¸«Ä̤·¤ò¤è¤¯¤¹¤ë¤¿¤áʬ³ä¤·¤Æ¤ª¤¯¡£

 BasicGame
 ¨¢
 ¨¢  index.html
 ¨¢
 ¨§¨¡ audio¡Ê¸ú²Ì²»¡Ë
 ¨¢      idou_ochiru_normal.mp3
 ¨¢      jump08.mp3
 ¨¢
 ¨§¨¡ images¡Ê¥°¥é¥Õ¥£¥Ã¥¯¡Ë
 ¨¢      button.png
 ¨¢      dude.png
 ¨¢      platform320.png
 ¨¢      preloadr_bar.png
 ¨¢      sky-dark.png
 ¨¢      star.png
 ¨¢      timesup.png
 ¨¢      title.png
 ¨¢
 ¨¦¨¡ js¡Ê¥³¡¼¥É¡Ë
        Boot.js
        Game.js
        MainMenu.js
        phaser.min.js
        Preloader.js

* index.html [#p0b42f38]

[[¥½¡¼¥¹¥³¡¼¥É:https://github.com/ycatch/games-phaser-jumping-dude/blob/master/index.html]]

¤³¤Îindex.html¤ò³«¤¯¤È¡¢¥²¡¼¥à¤¬µ¯Æ°¤¹¤ë¡£

¤Þ¤º¡¢<head>Éô¤ÇɬÍפÊJavascript¤òÆɤ߹þ¤à¡£

 <head>
     <meta charset="UTF-8" />
     <title>Jumping Dude</title>
     <script src="js/phaser.min.js"></script>
     <script src="js/Boot.js"></script>
     <script src="js/Preloader.js"></script>
     <script src="js/MainMenu.js"></script>
     <script src="js/Game.js"></script>
 </head>

¤Ç¡¢¥²¡¼¥à¼«ÂΤòɽ¼¨¤¹¤ë¥³¥ó¥Æ¥Ê¤òÍÑ°Õ¤¹¤ë¡£

 <div id="gameContainer"></div>

¡ãscript¡äÉô¤ÎJavascript¤Ç¤Ï¡¢window.close¤Ç¸Æ¤Ó½Ð¤·¤Æ¡¢¥²¡¼¥à¤Î¥¹¥Æ¡¼¥¿¥¹¤òÅÐÏ¿¤¹¤ë¡£¤½¤ì¤«¤éBoot¥¹¥Æ¡¼¥¿¥¹¤òµ¯Æ°¤¹¤ë¡£

 <script type="text/javascript">
 
	window.onload = function() {
 
	// Create your Phaser game and inject it into the gameContainer div.
	// We did it in a window.onload event, but you can do it anywhere (requireJS load, anonymous function, jQuery dom ready, - whatever floats your boat)
	var game = new Phaser.Game(640, 480, Phaser.AUTO, 'gameContainer');
 
	// Add the States your game has.
	// You don't have to do this in the html, it could be done in your Boot state too, but for simplicity I'll keep it here.
	game.state.add('Boot', BasicGame.Boot);
	game.state.add('Preloader', BasicGame.Preloader);
	game.state.add('MainMenu', BasicGame.MainMenu);
	game.state.add('Game', BasicGame.Game);
 
	//	Now start the Boot state.
	game.state.start('Boot');
 };
 </script>

* Boot.js [#r0f64cb0]

[[¥½¡¼¥¹¥³¡¼¥É:https://github.com/ycatch/games-phaser-jumping-dude/blob/master/js/Boot.js]]

ÏÈÁȤߤȤ·¤Æ¤Ï¡¢BasicGame.Boot¤òÄêµÁ¤·¤Æ¡¢prototype¤Çµ¡Ç½¤òÄɲ䷤Ƥ¤¤¯¡£

¤³¤³¤Ç¤Ï¡¢init¡¢preload¡¢create¤Ë´Ø¿ô¤ò³ä¤êÅö¤Æ¤Æ¤¤¤ë¡£

¥²¡¼¥à¥×¥í¥°¥é¥à¤¬µ¯Æ°¤·¤¿¤é¡¢¤Þ¤ºinit¤ÇɬÍ׺Ǿ®¸Â¤ÎÀßÄê¤ò¤·¤Æ¡¢preload¤Ç¥ê¥½¡¼¥¹(Æɤ߹þ¤ßÃæ¤ò¼¨¤¹¥Ð¡¼)¤òÆɤ߹þ¤à¡£

Æɤ߹þ¤ó¤À¤é¡¢create¤ÇPreload¥¹¥Æ¡¼¥¿¥¹¤Ë°ÜÆ°¤¹¤ë¡£

* Preloader.js [#cdddf1e0]

[[¥½¡¼¥¹¥³¡¼¥É:https://github.com/ycatch/games-phaser-jumping-dude/blob/master/js/Preload.j]]

preload´Ø¿ô¤Ç¡¢¥°¥é¥Õ¥£¥Ã¥¯¤È¥µ¥¦¥ó¥É¥Õ¥¡¥¤¥ë¤òÆɤ߹þ¤ó¤Ç¡¢create¤ÇMainMenu¥¹¥Æ¡¼¥¿¥¹¤ËÁ«°Ü¤¹¤ë¡£

** Æɤ߹þ¤ßÃæ [#x122de16]

Æɤ߹þ¤ßÃæ(¥×¥í¥°¥ì¥¹¥Ð¡¼)¤Ï¡¢¤³¤ó¤Ê´¶¤¸¤Çɽ¼¨¤¹¤ë¡£

 // These are the assets we loaded in Boot.js
 // A nice sparkly background and a loading progress bar
 // ÇØ·Ê¿§
 this.stage.backgroundColor = 'navy';
 //ºî¶ÈÃæ¥Ð¡¼¤Î°ÌÃÖ
 this.preloadBar = this.add.sprite((BasicGame.GAME_WIDTH-311)/2, (BasicGame.GAME_HEIGHT-27)/2, 'preloaderBar');
 
 // This sets the preloadBar sprite as a loader sprite.
 // What that does is automatically crop the sprite from 0 to full-width
 // as the files below are loaded in.
 // ºî¶È³«»Ï
 this.load.setPreloadSprite(this.preloadBar);

¤Çºî¶È¤¬´°Î»¤·¤Æ¡¢¼¡¤Î¥¹¥Æ¡¼¥¿¥¹¤ËÁ«°Ü¤¹¤ë¤È¤­¤Ï¡¢create¤Î¤È¤³¤í¤Çºî¶ÈÃæ¥Ð¡¼¤òÄä»ß¤¹¤ë

 this.preloadBar.cropEnabled = false;

** ¥µ¥¦¥ó¥É¥Õ¥¡¥¤¥ë¤ÎÆɤ߹þ¤ß [#o5b6b275]

Æɤ߹þ¤ßÃæ¤Ï¡¢ºî¶ÈÃæ¤Î¥Ð¡¼¤òɽ¼¨¤¹¤ë¡£
²èÁü¤ä¥¹¥×¥é¥¤¥È¤ÈƱ¤¸¤è¤¦¤Ë¡¢¤³¤ó¤Ê¤Õ¤¦¤Ë¤·¤ÆÆɤߤ³¤à¡£

 this.load.audio('getSound', ['audio/jump08.mp3']);
 this.load.audio('jumpSound', ['audio/idou_ochiru_normal.mp3']);

* MainMenu.js [#kc302a28]

[[¥½¡¼¥¹¥³¡¼¥É:https://github.com/ycatch/games-phaser-jumping-dude/blob/master/js/MainMenu.js]]

¥¹¥¿¡¼¥È²èÌ̤ȥ¹¥¿¡¼¥È¥Ü¥¿¥ó¤òɽ¼¨¤¹¤ë¤À¤±¡£

** ¥¹¥¿¡¼¥È¥Ü¥¿¥ó [#wb8e51d7]

¥¹¥¿¡¼¥È¥Ü¥¿¥ó¤Ï¡¢¤³¤ó¤Ê´¶¤¸¡£¥Ü¥¿¥ó¤Ï¡¢¥¹¥×¥é¥¤¥È¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£¤³¤ì¤ò²èÌÌÃæ±û¤ËÇÛÃÖ¡£

¥Ü¥¿¥ó¤¬¥¯¥ê¥Ã¥¯¤µ¤ì¤ë¤È¡¢startGame´Ø¿ô¤¬¸Æ¤Ó½Ð¤µ¤ì¤ë¡£

 this.add.button(BasicGame.GAME_WIDTH/2-43, BasicGame.GAME_HEIGHT-100,'start-button', this.startGame, this, 1, 0, 2);

startGame´Ø¿ô¤Ï¡¢¼¡¤ÎGame¥¹¥Æ¡¼¥¿¥¹¤ËÁ«°Ü¤¹¤ë¡£

* Game.js [#yb1877af]

[[¥½¡¼¥¹¥³¡¼¥É:https://github.com/ycatch/games-phaser-jumping-dude/blob/master/js/Game.js]]

¥²¡¼¥à¼«ÂΤϡ¢Á°¤Î¥Á¥å¡¼¥È¥ê¥¢¥ë¤ÈƱ¤¸¤¯¡¢Áö¤Ã¤Æ¥¸¥ã¥ó¥×¤·¤ÆÀ±¤ò½¸¤á¤ë¡£

** ¥µ¥¦¥ó¥É [#p1dd4e9a]

¤¤¤Á¤ª¤¦¡¢¸ú²Ì²»¤òÉÕ¤±¤Æ¤ß¤¿¡£¥¸¥ã¥ó¥×¤·¤¿¤È¤­¤ÈÀ±¤ò¼è¤Ã¤¿¤È¤­¤Ë¸ú²Ì²»¤¬¤Ê¤ë¡£

¤Þ¤º¡¢create¤Î¤È¤³¤í¤Ç¡¢¤³¤Î¥¹¥Æ¡¼¥¿¥¹¤Ç»È¤¦¸ú²Ì²»¤òÅÐÏ¿¤¹¤ë

 this.getSound = this.add.audio('getSound');
 this.jumpSound = this.add.audio('jumpSound');

¤½¤ì¤«¤é¡¢¼ÂºÝ¤Î¥¢¥¯¥·¥ç¥ó¤Ë¹ç¤ï¤»¤Æ¡¢¸ú²Ì²»¤òºÆÀ¸¤¹¤ë

 this.jumpSound.play();  //¥¸¥ã¥ó¥×

 this.getSound.play();   //À±¥²¥Ã¥È

** »þ´ÖÀ©¸Â [#bc18fe3c]

»þ´ÖÀ©¸ÂÀ©¤Î¥²¡¼¥à¤Ë¤·¤Æ¤ß¤¿¡£

¤½¤Î¤¿¤á¤Ë¡¢¤Þ¤ºcreate¤Î¤È¤³¤í¤Ç¡¢¥¿¥¤¥Þ¡¼¤È¥¿¥¤¥Þ¡¼É½¼¨¤ò½é´ü²½¤¹¤ë

 this._timeCounter = 0;  //¥¿¥¤¥Þ¡¼
 this._leftTime = 30;   //»Ä¤ê»þ´Ö
 this._leftTimeText = this.add.text(300, 16, 'Time: 0', { fontSize: '28px', fill: '#FFF' }); //»þ´Öɽ¼¨

¤Ç¡¢update¤Î¤È¤³¤í¤Ç¡¢¥¿¥¤¥Þ¡¼¤ò¹¹¿·¤¹¤ë

 // update timer every frame
 this._timeCounter += this.time.elapsed;
 // if spawn timer reach one second (1000 miliseconds)
 if(this._timeCounter > 1000) {
     // reset it
     this._timeCounter = 0;
     this._leftTime --;
     this._leftTimeText.text = 'Time: ' + this._leftTime;
 }

»Ä¤ê»þ´Ö¤¬¡¢¥¼¥í¤Ë¤Ê¤Ã¤¿¤é¡¢¥²¡¼¥à¥ª¡¼¥Ð¡¼¤ÇquitGame¤ò¸Æ¤Ó½Ð¤¹¡£

 if(this._leftTime <= 0) {
     this.quitGame();
 }

** ¥²¡¼¥à¥ª¡¼¥Ð¡¼ [#y0fac540]

quitGame¤Ï¡¢¤³¤ó¤Ê´¶¤¸¡£¤Þ¤º¡¢¸ú²Ì²»¤ò»ß¤á¤Æ¡¢Time'sUp(»þ´ÖÀÚ¤ì)¤òɽ¼¨¤¹¤ë

 quitGame: function (pointer) {
     // Here you should destroy anything you no longer need.
     // Stop music, delete sprites, purge caches, free resources, all that good stuff.
     // Then let's go back to the main menu.
     this.getSound.stop();
     this._timesup_title = this.add.sprite((this.world.width - 400) / 2, (this.world.height - 100) / 2, 'timesup');

¤½¤ì¤«¤é¡¢¥²¡¼¥àÁ´ÂΤò°ì»þÄä»ß¤·¤Æ¡¢¥¯¥ê¥Ã¥¯¤·¤¿¤é¥á¥¤¥ó¥á¥Ë¥å¡¼¤ØÌá¤ë¤è¤¦¤Ë¤¹¤ë¡£

     this.game.paused = true;
     var pausedText = this.add.text(160, 360, "Tap anywhere to Menu.", { fontSize: '28px', fill: '#FFF' });
     this.input.onDown.add(function(){
     pausedText.destroy();
     this.game.paused = false;
     this.state.start('MainMenu');
     }, this);
 },


* »²¹Í [#w17363b5]

¤³¤ì¤â¤Þ¤¿Ê̤Υ²¡¼¥à¡Ü¥Á¥å¡¼¥È¥ê¥¢¥ë¡£

- [[Getting Started With Phaser: Building "Monster Wants Candy" - Tuts+ Game Development Tutorial:http://gamedevelopment.tutsplus.com/tutorials/getting-started-with-phaser-building-monster-wants-candy--cms-21723]]

¥È¥Ã¥×   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS