「HTML5開発」画像処理、衝突検出処理、音の追加処理サンプルコードまとめ

1.画像を利用する場合

player.sprite = Sprite(“player");
player.draw = function() {
this.sprite.draw(canvas, this.x, this.y);
};
function Enemy(I) {


I.sprite = Sprite(“enemy");
I.draw = function() {
this.sprite.draw(canvas, this.x, this.y);
};

}
2.衝突検出処理
function collides(a, b) {
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;

}

function handleCollisions() {
playerBullets.forEach(function(bullet) {
enemies.forEach(function(enemy) {
if (collides(bullet, enemy)) {
enemy.explode();
bullet.active = false;
}
});
});
enemies.forEach(function(enemy) {

if (collides(enemy, player)) {

enemy.explode();

player.explode();

}

});

}
function update() {

handleCollisions();

}

function Enemy(I) {


I.explode = function() {

this.active = false;

// Extra Credit: Add an explosion graphic

};
return I;

};

player.explode = function() {

this.active = false;

// Extra Credit: Add an explosion graphic and then end the game

};

3.音処理の追加
function Enemy(I) {

I.explode = function() {
this.active = false;
// Extra Credit: Add an explosion graphic
};
return I;
};

player.explode = function() {
this.active = false;
// Extra Credit: Add an explosion graphic and then end the game
};

jQuery

Posted by arkgame