Skip to content

Instantly share code, notes, and snippets.

@yuskesuzki
Created January 28, 2012 02:25
Show Gist options
  • Save yuskesuzki/1692153 to your computer and use it in GitHub Desktop.
Save yuskesuzki/1692153 to your computer and use it in GitHub Desktop.
enchant.js sample 1
<script type="text/javascript" src="./enchant.js"></script>
<script type="text/javascript" src="./plugins/ui.enchant.js"></script>
<script type="text/javascript">
/*
* ---------------------------------------------------------------------
* enchant.js サンプル
* クマをカーソルキーで動かしつつも、グルグルとオブジェも回してみる
*
* 同じディレクトリ内に
* enchant.js, /plugins, /images ディレクトリも置いてください
* 同じディレクトリに apad.png, pad.png を置くとコントロールパッドも表示
* ---------------------------------------------------------------------
*/
enchant();
window.onload = function() {
var game = new Game(320, 320);
game.fps = 24;
game.preload('./images/chara1.gif', './images/chara2.gif');
// The images used in the game should be preloaded
game.onload = function() {
var bear = new Sprite(32, 32);
bear.x = 8;
bear.y = 8;
bear.image = game.assets['./images/chara1.gif'];
// クマをカーソルキーで動かすためのイベントを定義
bear.addEventListener('enterframe', function(e) {
// check input (from key or pad) on every frame
if (game.input.right) {
bear.x += 20; // 速すぎ
}
if (game.input.left) {
bear.x -= 20;
}
if (game.input.up) {
bear.y -= 20;
}
if (game.input.down) {
bear.y += 20;
}
});
// add bear to rootScene (default scene)
game.rootScene.addChild(bear);
var label = new Label();
var nRadian = 0;
var sprite = new Sprite(64, 64);
sprite.image = game.assets['./images/chara2.gif'];
// 三角関数を用いて、やや楕円っぽい軌道でオブジェを回す
sprite.addEventListener('enterframe', function(e) {
nRadian += 0.1;
sprite.x = Math.cos(nRadian)*20;
sprite.y = Math.sin(nRadian)*10;
label.text = "x:" + sprite.x;
label.text += "y:" + sprite.y;
});
scene = new Scene();
scene.addChild(sprite);
scene.addChild(label);
game.rootScene.addChild(scene);
// display d-pad
// コントロールパッド出さないようコメントアウト
/*
var pad = new Pad();
pad.x = 0;
pad.y = 224;
game.rootScene.addChild(pad);
game.rootScene.backgroundColor = '#ffffff';
*/
};
game.start();
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment