More Related Content Similar to Html canvas shooting_and_performanceup
Similar to Html canvas shooting_and_performanceup (20) Html canvas shooting_and_performanceup3. 今日お話するネタはこちらです
http://www.yoheim.net/labo/html5/canvasShooting.html
24. 1. 初期化処理
// Canvasを取得
var canvas
= document.getElementById("myCanvas");
// Canvasの大きさを画面に合わせる
canvas.width = window.innerWidth || 800;
canvas.height = window.innerHeight * 0.95 ||
400;
// contextを生成する
var ctx = canvas.getContext("2d");
30. 3. 自機を動かす ①
// まずは自機を表すオブジェクトを作ります
var plane = {
img : img_plane,
posX : 10,
posY : (h - img_plane.height) / 2,
life : 3
}
36. 4. 自機がショットを撃つ ③
// mainLoop内で、speed分だけ移動させて描画する
var gameMainLoop = function () {
for (var i = 0; i < plane_balls.length; i++) {
var ball = plane_balls[i];
ball.posX += ball.speed;
ctx.fillStyle = "rgba(200,0,0,1)";
ctx.fillRect(ball.posX, ball.posY, ball.radius,
ball.radius);
}
}
41. 5. 敵機を出現させる ②
// 敵機はランダムに出現させます
function createEnemy () {
var num =(Math.floor((Math.random()*100)%100);
if (num < 5) {
var s = num;
var i = (num%2==0 ? imgE01 : imgE02);
var y = canvasHeght * Math.random();
var x = canvasWidth;
var enemy = {img:i, posX:x, posY:y, speed:s};
enemies.push(enemy);
}
}
47. 6.当たり判定を行う ③
// ソースコードではこんな感じです
var b = plane_balls[i];
var e = enemies[j];
if ((b.posX + b.radius) >= e.posX /* x 座標 */
&& b.posY <= e.posY+e.img.width /* y座標下 */
&& b.posY >= e.posY) { /* y座標上 */
delete plane_balls[i];
delete enemies[j];
}
48. 6. 当たり判定を行う ④
// 最後にdeleteした要素をお掃除する
// 例えば自機ショットの場合
var new_plane_balls = [];
for (var i = 0; i < plane_balls.length; i++) {
if (plane_balls[i])
new_plane_balls.push(plane_balls[i]);
}
plane_balls = new_plane_balls;
49. 7.GameOver or GameClear ①
// ボスを倒すか、自機が倒されたら終わり
if (deadBoss() || deadSelf()) {
// mainLoopを終了させる
clearTimeout(timer);
// メッセージを表示する
if (deadBoss())
alert("CONGURATURATION!!");
else
alert("GAME OVER");
}
57. 2、プレレンダリング
// プレレンダリング
var m_canvas = document.createElement('canvas');
m_canvas.width = 32;
m_canvas.height = 32;
var m_context = m_canvas.getContext(‘2d’);
drawEnemy(xxx);
// メインループでの描画
function gameMainLoop() {
context.drawImage(m_canvas, 0, 0);
requestAnimationFrame(render);
}
63. 5、ステートの変更は最小に
// Good
context.fillStyle = COLOR1;
for (var i = 0; i < STRIPES/2; i++) {
context.fillRect((i*2) * GAP, 0, GAP, 480);
}
context.fillStyle = COLOR2;
for (var i = 0; i < STRIPES/2; i++) {
context.fillRect((i*2+1) * GAP, 0, GAP, 480);
}
74. ゲームの前処理
window.onload = function() {
enchant();
// サイズを決めるのはこれだけ。HTML要素も要らない。
var game = new Game(320,320);
// 画像読み込み完了か否かはライブラリが判断してくれる。
game.preload('fig1.png','fig2.png');
game.onload = function(){/*ゲーム開始時の処理*/}
game.start();
}
// ゲームループの実装は必要ない
// ループ毎のCanvasのお掃除もない(Canvasではない為)
75. ゲーム中 part1
要素の追加、フレーム毎の処理
// 登場キャラクターの追加(オブジェクト指向で便利)
// アーキテクチャ設計は既にされていて、考える必要なし
var enemy = new Sprite(32,32);
enemy.image = game.assets['enemy01.png'];
enemy.x = enemy.y = 50;
game.rootScene.addChild(enemy);
// フレーム毎の処理は、追加した要素毎に記載
enemy.addEventListener('enterframe',function(){
this.x -= 2;
});
76. ゲーム中 part2
// Sprite毎のタップイベントも簡単に扱える。
// 位置特定を自動でしてくれるなんていい感じ。
plane.addEventListener("touchend", function(param){
plane.x = param.x;
plane.y = param.y;
});
// 衝突判定はたったこれだけ。
// 面倒な条件判定はライブラリがやってくれる。
if(ball1.intersect(enemy1)){
game.rootScene.removeChild(enemy1);
}