javascript 凄い
とは言え、良い試験なツールが欲しいですね。何が凄いかって
- closure 使える
とかなソレは良いとして
- オブジェクトの属性ダイナミックに追加が可能
というのは素晴しい。確か Ruby もそうした操作は可能だったはずなのですが、クラスの定義な記述は必要だったはず。
なんとなく
以下なナニをでっち上げてみました。preload する画像はてきとうにアレしてみて頂ければと。あと、@hanachin_ の中の人が作ったナニをパクらせて頂いております (原型は留めてませんが)。
コメント頂ければ幸いです。
enchant(); count = 0; window.onload = function() { var game = new Game(320, 320); game.fps = 10; game.preload('hoge.gif'); game.isEnd = false; game.score = 0; game.onload = function() { var scene = game.currentScene; var blocks = new Array(16); var title = new Label(); title.x = 120; title.y = 10; title.text = 'Beat the hoge'; title.font = "0.8em 'Arial'"; game.rootScene.addChild(title); var pointLabel = new Label(); pointLabel.x = 10; pointLabel.y = 10; pointLabel.text = 'Score: ' + game.score; pointLabel.font = "0.8em 'Arial'"; game.rootScene.addChild(pointLabel); var hoges = new Array(16); for(var i = 0; i < 16; i++) { hoges[i] = new Sprite(20, 20); hoges[i].image = game.assets['hoge.gif']; hoges[i].x = 25; hoges[i].y = 25; } for (var i = 0; i < 4; i++) { for (var j = 0; j < 4; j++) { var index = (i * 4) + j; blocks[index] = (function() { var block = new Group(); block.x = 0; block.y = 0; block.width = 62; block.height = 62; var bg = new Sprite(62, 62); bg.x = bg.y = 0; bg.backgroundColor = "#fff"; var background = (function(block, size, color){ var wrap = new Group(); var bg = new Sprite(block.width + size * 2, block.height + size * 2); bg.x = 0; bg.y = 0; bg.backgroundColor = color; block.x += size; block.y += size; wrap.addChild(bg); wrap.addChild(block); return wrap; })(bg, 4, "#2f4f4f"); block.addChild(background); return block; })(); blocks[index].x = 20 + j * 70; blocks[index].y = 20 + i * 70; blocks[index].ttl = 0; blocks[index].visible = false; scene.addChild(blocks[index]); } } count = 0; start = 0; game.rootScene.addEventListener('touchstart', function(e) { var x = parseInt((e.x - 20) / 70); var y = parseInt((e.y - 20) / 70); var i = (y * 4) + x; if(blocks[i].visible) { game.score++; pointLabel.text = 'Score: ' + game.score; blocks[i].removeChild(hoges[i]); blocks[i].visible = false; count--; } }); game.rootScene.addEventListener('enterframe', function() { for(var i = 0; i < 16; i++) { if(blocks[i].visible) { blocks[i].ttl--; if(blocks[i].ttl <= 0) { blocks[i].removeChild(hoges[i]); blocks[i].visible = false; count--; } } } start++; if(start > 450) { game.end(game.score, game.score); game.isEnd = true; } if(!game.isEnd) { var limit = 0; var ttl = 30; if(start < 150) { } else if (start < 300) { limit = 1; ttl = 10 + Math.random()*10; } else if (start < 450) { limit = 2; ttl = 10 + Math.random()*5; } for(var i = count; i <= limit; i++) { var idx = Math.floor(Math.random()*16); if(blocks[idx].visible) continue; blocks[idx].addChild(hoges[idx]); blocks[idx].ttl = ttl; blocks[idx].visible = true; count++; } } }); }; game.start(); };
明日、リファクタリングおよび .apk 化実施の方向。