绑定函数同时绑定作用域

This commit is contained in:
郑浩乐 2016-01-01 19:43:22 +08:00
parent fce69b38fd
commit 2e51922fc1
2 changed files with 34 additions and 6 deletions

View File

@ -58,7 +58,7 @@ function Game(id,options){
}); });
}); });
} }
_events[eventType]['s'+this.stage.index+'i'+this.index] = callback; _events[eventType]['s'+this.stage.index+'i'+this.index] = callback.bind(this); //绑定作用域
}; };
//布景对象构造器 //布景对象构造器
var Stage = function(options){ var Stage = function(options){
@ -128,7 +128,7 @@ function Game(id,options){
} }
}); });
} }
_events[eventType]['s'+this.index] = callback; _events[eventType]['s'+this.index] = callback.bind(this); //绑定事件作用域
}; };
//事件坐标 //事件坐标
this.getPosition = function(e){ this.getPosition = function(e){

View File

@ -9,8 +9,6 @@
case 13: case 13:
case 32: case 32:
game.nextStage(); game.nextStage();
// stage.status = 2;
// console.log(this);
break; break;
} }
}); });
@ -67,8 +65,38 @@
//游戏主程序 //游戏主程序
(function(){ (function(){
var stage = game.createStage(); var stage = game.createStage();
stage.bind('keydown',function(){ stage.bind('keydown',function(e){
console.log('没东西啦……还按!'); switch(e.keyCode){
case 13:
case 32:
this.status = this.status==2?1:2;
break;
}
});
//logo
stage.createItem({
x:game.width/2,
y:game.height*.45,
width:100,
height:100,
speed:10,
draw:function(context){
context.fillStyle = '#FC3';
context.beginPath();
if(this.frames%2){
context.arc(this.x,this.y,this.width/2,.20*Math.PI,1.80*Math.PI,false);
}else{
context.arc(this.x,this.y,this.width/2,.01*Math.PI,1.99*Math.PI,false);
}
context.lineTo(this.x,this.y);
context.closePath();
context.fill();
context.fillStyle = '#000';
context.beginPath();
context.arc(this.x+5,this.y-27,7,0,2*Math.PI,false);
context.closePath();
context.fill();
}
}); });
})(); })();
game.init(); game.init();