enchant.jsの練習

この記事は約5分で読めます。

 

enchant();
/*
Coreオブジェクト
-rootScene
–Sprite(bear)
*/

 

window.onload = function() {
console.log(‘hello bear!’);
var core = new Core(320, 320);
core.preload(‘chara1.png’);
core.preload(‘miku.gif’);
core.fps=15;
core.onload = function() {

var Miku =Class.create(Sprite,{
initialize: function(x, y) {
Sprite.call(this, 64, 64);
this.x = x;
this.y = y;
this.scale(0.5,0.5);
this.image = core.assets[‘miku.gif’];
this.on(‘enterframe’, function() {
this.frame=this.age%40;
});
core.rootScene.addChild(this);
}
});

 

var miku = new Miku(50,50);

var Bear = Class.create(Sprite, {
initialize: function(x, y) {
Sprite.call(this, 32, 32);
this.x = x;
this.y = y;
this.frame=rand(5);
this.opacity = rand(100)/100;
this.image = core.assets[‘chara1.png’];

this.tl.moveBy(rand(100),0,40,enchant.Easing.BOUNCE_EASEOUT)
.moveBy(-rand(100), -rand(20), rand(20))
.fadeOut(20)
.fadeIn(10)
.loop();

this.on(‘enterframe’, function() {
//this.x += 5;
//this.rotate(rand(50));
});
core.rootScene.addChild(this);
}
});

var bears =[];
for(var i=0;i<100;i++){
//bears[i]=new Bear(rand(320),rand(320));
}

 

var bear = new Sprite(32, 32);
bear.image = core.assets[‘chara1.png’];
bear.x = 0;
bear.y = 0;
bear.frame =1;

 

bear.addEventListener(‘enterframe’,function(){
if(core.input.left) this.x-=5;
if(core.input.right) this.x+=5;
if(core.input.up) this.y-=5;
if(core.input.down) this.y+=5;

 

/*
this.x+=10;
this.frame=this.age%3+5;
if(this.x>320)this.x=0;
this.rotate(2);
this.scale(1.1,1.1);
*/

//wuthin 2つのスプライトの中心点の距離で当たり判定
if (this.within(enemy,10)){
//label.text=’当たったよ!!’;
core.pushScene(gameOveerScene);
core.stop();
}
//intersect ざっくりとした当たり判定
else if (this.intersect(enemy)){
label.text=’近づいてるよ!’;
}else{
label.text=”;
}
});

 

var enemy = new Sprite(32, 32);
enemy.image = core.assets[‘chara1.png’];
enemy.x = 80;
enemy.y = 0;
enemy.frame=5;

 

bear.on(‘touchstart’, function() {
//core.rootScene.removeChild(this);
});

core.rootScene.on(‘touchstart’, function(e) {
bear.x = e.x;
bear.y = e.y;
});

 

var label =new Label();
label.x=200;
label.y=5;
label.color=’red’;
label.font=’14px “Arial”‘;
//label.text=’0’;
label.on(‘enterframe’,function(){
// label.text=(core.frame/core.fps).toFixed(2);
});

 

var gameOveerScene =new Scene();
gameOveerScene.backgroundColor = ‘black’;

 

core.rootScene.addChild(enemy);
core.rootScene.addChild(label);
core.rootScene.addChild(bear);
}
core.start();
};

 

function rand(n){
return Math.floor(Math.random() * (n+1));
}

 

コメント

タイトルとURLをコピーしました