[目次]
Enchant.js での衝突判定 | Webプログラミング!
衝突判定のサンプル
衝突判定のサンプルhittest1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<title>Test of Enchant.js</title>
<style type="text/css">
body { margin: 0; }
</style>
<script type="text/javascript" src="enchant.js"></script>
<script type="text/javascript" src="plugins/ui.enchant.js"></script>
<script type="text/javascript" src="hittest1.js"></script>
</head>
<body>
</body>
</html>
hittest1.js
enchant();
window.onload = function() {
var game = new Game(320,320);
game.fps = 16;
game.preload('chara1.png');
game.onload = function() {
var expl = new Label("");
expl.text="くまさんたちを重ねてみてね";
expl.font="24px monospace";
expl.color="rgb(128,128,128)";
expl.moveTo(10,10);
game.rootScene.addChild(expl);
var polarbear = new Sprite(32,32);
polarbear.image = game.assets['chara1.png'];
game.rootScene.addChild(polarbear);
polarbear.frame = 7;
polarbear.moveTo(200,200);
var bear = new Sprite(32,32);
bear.image = game.assets['chara1.png'];
game.rootScene.addChild(bear);
bear.frame = 2;
var d=2;
var s=2;
bear.moveTo(30,50);
bear.addEventListener(Event.ENTER_FRAME,
function() {
var input=game.input;
if( input.left ){
this.moveBy(-d,0);
} else if ( input.right ){
this.moveBy(+d,0);
} else if ( input.up ){
this.moveBy(0,-d);
} else if ( input.down ){
this.moveBy(0,+d);
} else if ( input.a ){
this.scaleX*=2;
} else if ( input.b ){
this.scaleX*=-1;
}
if(this.intersect(polarbear)){
game.rootScene.removeChild(polarbear);
}
if(this.within(polarbear,100)){
polarbear.frame=8;
}
}
);
};
game.start();
};