ããŒã1 » ããŒã2 » ããŒã3 » ããŒã4 » ããŒã5 » ããŒã6 » ããŒã7 //çµäºïŒ
ä»æ¥ã¯ãCocos2d-JSãã©ãããã©ãŒã äžã«æ§ç¯ãããSnakeã²ãŒã ã®ã¢ãŒããã¯ãã£ã«ã€ããŠèª¬æããäœæãéå§ããŸãã Cocos2d-JSã®ããã¥ã¡ã³ããåç §ããŠãã®è³æãèªã¿ã ã²ãŒã ãªãœãŒã¹ã»ãããããŠã³ããŒãããŠãã ããã éçºã§ãããã䜿çšããŸãã
ã²ãŒã ã¢ãŒããã¯ãã£ã®æŠèŠ
Snakeããã¬ã€ããããšããªãå Žåã¯ã å®æãããããžã§ã¯ããã芧ãã ããã ã³ã³ãã¥ãŒã¿ãŒã«ããŠã³ããŒãããIntel XDKã«ã€ã³ããŒãããŠè©ŠããŠãã ããã ãããè¡ãã«ã¯ã[ãããžã§ã¯ã]ã¿ãã«ç§»åããå·Šäžé ã«ãã[Intel XDKãããžã§ã¯ããéã]ãã¿ã³ãã¯ãªãã¯ããŸãã
Intel XDKã®ãããžã§ã¯ãã€ã³ããŒããã¿ã³
ããã¯ã·ã³ãã«ãªã²ãŒã ã§ãã æåã¯ãããã®é ã ããç»é¢ã«è¡šç€ºãããŸãã ãããå¶åŸ¡ãããšãã¯ãç»é¢ã«ã©ã³ãã ã«è¡šç€ºãããé£çšã®ãã®ãçãå¿ èŠããããŸãã ç§ãã¡ã®å Žåãããã¯èµ€ãç¹ã§è¡šããããã¯ãããŒãã§ãã 埡treatèµ°ãèŠã€ããããã¯ãããé£ã¹ããµã€ãºã倧ãããªããŸãã åæã«ããã®ç§»åé床ãå¢å ããŸãã æé·ããããããããããã®ãè¿œããããŠããã£ãããã£ãœã«ã€ããå Žåãã²ãŒã ã¯çµäºããŸãã
ãã®ããŒãžã§ã³ã®ã²ãŒã ã§ã¯ããã£ã©ã¯ã¿ãŒã¯ç»é¢ã®å¢çãè¶ããŠå察åŽã«çŸããããšãã§ããŸãã ã³ã³ãã¥ãŒã¿ãŒã®ã²ãŒã ãå¶åŸ¡ããã«ã¯ãããŒWãSãAãDã䜿çšããŸããããããããããäžäžå·Šå³ã«åããããšãã§ããŸãã ã¿ããã¹ã¯ãªãŒã³ãåããããã€ã¹ã§ã¯ãç®çã®æ¹åã«åŒã£åŒµã£ãŠç»é¢ã«è§Šãããžã§ã¹ãã£ã«ãã£ãŠåãæ©èœãå®è¡ãããŸãã ãšãã¥ã¬ãŒã¿ã§ã¯ãããŠã¹ã§ç»é¢ãã¯ãªãã¯ããå·Šãã¿ã³ãæŸããã«ãããã移åããæ¹åã«ãã€ã³ã¿ããã©ãã°ããããšã§ãããåçŸã§ããŸãã
å®è£
ã²ãŒã ã¯3ã€ã®ã·ãŒã³ã§æ§æãããŠããŸãã ãããã¯ãã¡ãã¥ãŒïŒMenuSceneïŒãã²ãŒã ç»é¢ïŒGameSceneïŒãããã³ã²ãŒã å®äºç»é¢ïŒGameOverïŒã§ãã
ã²ãŒã ãæ§æããã·ãŒã³
MenuSceneã«ã¯ãããã¹ãã衚瀺ãã2ã€ã®ã¹ãã©ã€ããå«ãŸããŠããŸãã ããã¯ã²ãŒã ã®ååã§ãããç»é¢ãã¿ããïŒãŸãã¯ã¯ãªãã¯ïŒããŠã²ãŒã ãéå§ããããã®æåŸ ç¶ã§ãã ãã®ã·ãŒã³ã«ã¯ã¬ã€ã€ãŒããããŸããã GameOverSceneã·ãŒã³ãé 眮ãããŠããŸãã å¯äžã®éãã¯ã3ã€ã®ã¹ãã©ã€ããå«ãŸããŠããããšã§ãã1ã€ã®ããŒã¿ã¯ãåŸç¹ã®æ°ã衚瀺ããGameSceneããååŸãããŸãã
ã¡ã€ã³ã²ãŒã ç»é¢
ã¡ã€ã³ã²ãŒã ç»é¢ã§ããGameSceneã·ãŒã³ã¯ããã²ãŒã ããšåŒã°ããã»ãŒãã¹ãŠãè¡ãããå Žæã§ãã ãã®ã¬ã€ãããã¹ã¿ãŒããŠãããã§éšåçã«ã²ãŒã ãäœæããŸãã
ãããžã§ã¯ããã³ãã¬ãŒãããäžèŠãªãã®ãåé€ãã
Intel XDKãæäŸããæšæºãããžã§ã¯ããã³ãã¬ãŒãã«ã¯ã䜿çšããªãã³ã³ããŒãã³ããå«ãŸããŠããŸãã äžèŠãªãã®ã¯ãã¹ãŠåé€ããŸãã
- ãšã¯ã¹ãããŒã©ãŒãŸãã¯å¥ã®ã·ã¹ãã ãã¡ã€ã«ãããŒãžã£ãŒãéããŸãã
- ãããžã§ã¯ããã©ã«ããŒã«ããã²ãŒã ãªãœãŒã¹ïŒwww /ã¢ã»ããïŒã®ãã©ã«ããŒãå
¥åããŸãã
- ããã«ãããã®ããã¹ãŠåé€ããŸãã
- ãã®ãã©ã«ããŒã«ãã²ãŒã ã®ãªãœãŒã¹ãå«ãã¢ãŒã«ã€ãå
ã®ãã¹ãŠãæœåºããŸãã
- resource.jsãã¡ã€ã«ãéããŸãã
var asset = { HelloWorld_png : "asset/HelloWorld.jpg", CloseNormal_png : "asset/CloseNormal.png", CloseSelected_png : "asset/CloseSelected.png" }; var g_resources = []; for (var i in asset) { g_resources.push(asset[i]); }
- å€ãããŒã¿ãåé€ããŠãæ°ãããªãœãŒã¹ã«é¢ããæ
å ±ãè³ç£å€æ°ã«è¿œå ããŸãã ãã¡ã€ã«å
ã®æ®ãã®ã³ãŒãã¯å€æŽããªãã§ãã ããã
var asset = { SnakeHead_png : "asset/snake_head.png", SnakeBody_png : "asset/snake_body.png", SnakeBiscuit_png : "asset/snake_biscuit.png" };
- app.jsãã¡ã€ã«ãåé€ããŸã
- project.jsonãã¡ã€ã«ãéããŸã
"jsList" : [ "src/resource.js", "src/app.js" ]
- jsListããapp.jsãã¡ã€ã«ã®èª¬æãåé€ããŸã
- game.jsãã¡ã€ã«ã®èª¬æãjsListã«è¿œå ããŸã
"jsList" : [ "src/resource.js", "src/game.js" ]
- index.htmlãã¡ã€ã«ãéããŸã
- ã¿ã°ãèŠã€ãã
- å«ãŸãããã®ãSnakeã«çœ®ãæããŸãã
ãã®çµæããããžã§ã¯ããã¡ã€ã«ã®æ§é ã¯æ¬¡ã®ããã«ãªããŸãã
å€æŽåŸã®ãããžã§ã¯ããã¡ã€ã«æ§é
åé€ããããã¡ã€ã«ããŸã Bracketsãšãã£ã¿ãŒãã¡ã€ã«ãããŒãžã£ãŒãapp.jsã«è¡šç€ºãããŠããå Žåã¯ãXDKãåèµ·åããŸãã
ã²ãŒã ãã¬ãŒã ã®æºåã¯ã§ããŠããŸããããšãã¥ã¬ãŒã¿ã§éããå Žåãäœãæ©èœããŸããã ãããä¿®æ£ããããã«ãGameSceneã®å®è£ ãåãäžããŸãã
GameSceneã®å®è£
- game.js.ãã¡ã€ã«ãäœæããŸã ãããè¡ãã«ã¯ã[éçº]ã¿ãã«ç§»åããŠãã¡ãã¥ãŒã³ãã³ã[ãã¡ã€ã«]> [æ°èŠ]ïŒ[ãã¡ã€ã«]> [æ°èŠ]ïŒãå®è¡ããwww / srcãã©ã«ããŒã«ãã¡ã€ã«ãä¿åããgame.jsãšããååãä»ããŸãã
- ãã¡ã€ã«ã«æ¬¡ã®ã³ãŒããè¿œå ããŸãã
var GameScene = cc.Scene.extend({ onEnter:function () { this._super(); } });
ããã§ã¯ãGameSceneå€æ°ã«ã€ããŠèª¬æããæ°ããã¬ã€ã€ãŒã®èŠªã®onEnterã¡ãœãããåŒã³åºããŸãã
- ãthis._superïŒïŒ;ãã®äžã«ã次ã®ã³ãŒããè¿œå ããŸãã
var GameScene = cc.Scene.extend({ snake_layer: {}, onEnter: function() { this._super(); // this.snake_layer = new SnakeLayer(); this.addChild(this.snake_layer, 0); } });
ããã¯ãåŸã§å®è£ ããSnakeLayerã¬ã€ã€ãŒãäœæãããå Žæã§ãã ãã®ã¬ã€ã€ãŒã¯ãGameSceneã®åå«ãšããŠè¿œå ãããŸãã GameSceneã«ã¯SnakeLayerã¬ã€ã€ãŒã®ã¿ãå«ãŸããŠãããæ¢ã«ã¬ã€ã€ãŒã«ã¯ã²ãŒã ã®ããžãã¯ãå«ãŸããŠããŸãã
ç§ãã¡ã¯ããã®é ãæ±ããŸã
ã²ãŒã ãã£ã©ã¯ã¿ãŒãããã®ããã«èŠããããã«ãé ã«ç¶ãå€ãã®æçããäœæããŸãã ããã¯ãããããµã€ãºã倧ããããŠ90床å転ã§ããå¿ èŠããããšããäºå®ã®ããã«è¡ãããŸãã å€å žçãªSnakeã²ãŒã ã¯ãã®ããã«èšèšãããŠããŸãã ç¬èªã®ããŒãžã§ã³ãäœæããå Žåã確ç«ãããäŒçµ±ã«åŸããŸãã
次ã®ã¯ã©ã¹ãgame.jsã«è¿œå ããŠããã©ã³ã®å®è£ ãé²ããŸãããã
var SnakePart = cc.Sprite.extend({ ctor: function(sprite) { /* , */ this._super(sprite); }, move: function(posX, posY) { /* */ this.x = posX; this.y = posY; }, })
ãã®ã¯ã©ã¹ã¯ã1ã€ã®èã»ã°ã¡ã³ããèšè¿°ããããã«äœ¿çšãããŸãã æ¢ã«moveã¡ãœãããå«ãŸããŠãããããåã»ã°ã¡ã³ããåå¥ã«ç§»åã§ããŸãã
ããèªäœã¯SnakeLayerã¬ã€ã€ãŒã«ãããŸãã ãããç§ãã¡ã®ãã£ã©ã¯ã¿ãŒã®åååã§ããè³ã§ãã
次ã®ã³ãŒããgame.jsã«è¿œå ããŠãã¬ã€ã€ãŒã説æããŸãã
var SnakeLayer = cc.Layer.extend({ ctor: function () { this._super(); } });
ããšãã°ãGameSceneã§åæ§ã®èª¬æãæ¢ã«çºçããŠããŸãã onEnterã¡ãœããã®ä»£ããã«ããªããžã§ã¯ãã®ã³ã³ã¹ãã©ã¯ã¿ãŒã®ã¿ãããã«ãããŸãã ããã§ã®ãã€ã³ãã¯ãïŒå¶åŸ¡ãªããžã§ã¯ãã®æ瀺ã®äžã§ïŒã¬ã€ã€ãŒã«ãåãæ¿ãããã ãã§ãªããäœæããããšã§ãã ããã«ããã®ã¯ã©ã¹ã«ã¯ãããã®äœã®æçãè¡šãããããã£ããããŸãïŒçŸæç¹ã§ã¯ããã®é ã®ã¿ïŒã ãã®å€æ°ã䜿çšããŠã察å¿ããã¹ãã©ã€ããã¬ã€ã€ãŒã«é 眮ããŸãã
ã²ãŒã ã§ã®äœæ¥äžãSnakeLayerã¬ã€ã€ãŒã®ã³ã³ã¹ãã©ã¯ã¿ãŒã倧ãããªããŸã-ããŸããŸãªãªããžã§ã¯ããåæåããã³ãã³ããè¿œå ãããŸãã
ããã®é ãäœæããããšããå§ããŸãããã ããã¯ãå®éã«ã¯ãç¹å¥ãªããããã£ãšç¬èªã®ã°ã©ãã£ãã¯è¡šçŸãæã€ããã£ã®ã»ã°ã¡ã³ãã®1ã€ã§ãã
ããã®é ïŒsnakeHeadå€æ°ïŒãåæåããã«ã¯ã次ã®ã³ãŒããSnakeLayerã«è¿œå ããå¿ èŠããããŸãã
var SnakeLayer = cc.Layer.extend({ snakeHead: null, ctor: function () { /* */ this._super(); /* */ this.snakeHead = new SnakePart(asset.SnakeHead_png); /* - */ this.addChild(this.snakeHead); }, });
ããã§ã¯ãSnakePartã¯ã©ã¹ã®ãªããžã§ã¯ããSnakeLayerã¬ã€ã€ãŒã®åå«ãªããžã§ã¯ããšããŠè¿œå ããŸãã ãšãã¥ã¬ãŒã¿ã§ãããžã§ã¯ããéå§ããããã§äœãé¢çœããã®ãèŠãæºåãã»ãŒæŽããŸããããæåã«ã³ã³ãããŒã«ãªããžã§ã¯ãã®ã³ãŒããä¿®æ£ããå¿ èŠããããŸãã
- main.jsãã¡ã€ã«ã«ç§»åããŸã
- è¡ãrunSceneããèŠã€ããŸã
cc.director.runScene(new HelloWorldScene());
- HelloWorldSceneãGameSceneã«çœ®ãæããŸãã
cc.director.runScene(new GameScene());
å€æŽãä¿åãããšãã¥ã¬ãŒã¿ã§ãããžã§ã¯ããå®è¡ããŸãã
ãšãã¥ã¬ãŒã¿ãŒã§å®è¡äžã®ã²ãŒã
ç§ãã¡ã®ä»äºã®çµæã¯éåžžã«ç®ç«ã£ãŠããŸãããããã®é ã¯é ã«è»¢ããããã¬ãŒã ã¬ãŒãã«ãŠã³ã¿ãŒã«ãã£ãŠãããããã¯ãããŸããã ã«ãŠã³ã¿ãŒãåé€ããŸãã ãããè¡ãã«ã¯ã次ãå®è¡ããŸãã
- project.jsonãã¡ã€ã«ãéããŸã
- ãshowFPSïŒtrueãããshowFPSïŒfalseãã«å€æŽããŸã
çµè«
ä»æ¥ã®ã¬ãã¹ã³ãèŠçŽããŸãã
- éçºããã²ãŒã ã¯ãå€å žçãªSnakeã®ããŒãžã§ã³ã§ãã
- ã²ãŒã ã«ã¯ãã¡ãã¥ãŒïŒã¡ãã¥ãŒïŒãã²ãŒã ç»é¢ïŒã²ãŒã ïŒãã²ãŒã å®äºç»é¢ïŒGameOverïŒã®3ã€ã®ã·ãŒã³ãå«ãŸããŸãã
- ã²ãŒã ãã£ã©ã¯ã¿ãŒã®ããã¯ãããã€ãã®éšåã§æ§æãããŠããŸã
ãããä»ã§ããããšã§ãã
- ã²ãŒã ãªãœãŒã¹ããããžã§ã¯ãã«è¿œå ããŠåé€ããŸãã
- resourse.jsãæŽæ°ããŠãæ°ãããªãœãŒã¹ãŸãã¯äžè¶³ããŠãããªãœãŒã¹ãèæ ®ããŸãã
- ãããžã§ã¯ãããã³ãŒããã¡ã€ã«ãåé€ããŠè¿œå ããŸãã
- ã²ãŒã ã®ã¿ã€ãã«ãæŽæ°ããŸãã
- ã²ãŒã ã·ãŒã³ïŒGameSceneïŒãäœæããŸãã
- ã¬ã€ã€ãŒãäœæããŸãïŒäŸãšããŠSnakeLayerã䜿çšïŒã
- ããã®èº«äœéšåïŒSnakePartïŒãå®è£ ããŸãã
- ããã®é ãåæåããŸãã
- ãã¬ãŒã ã¬ãŒãæ å ±ã®åºåãç¡å¹ã«ããŸãã
ãããžã§ã¯ãã®äœæ¥ã®ãã®æ®µéã§ã¯ãã²ãŒã ç»é¢ãšã³ãŒããŒã«èã®é ããããŸãã 次åã¯åº§æšç³»ãåŠçããããããã£ãšé¢çœãå Žæã«ç§»åãããŸãã
ããŒã1 » ããŒã2 » ããŒã3 » ããŒã4 » ããŒã5 » ããŒã6 » ããŒã7 //çµäºïŒ