// 键盘按下事件
keydown : function(e) {
var keyCode = e.keyCode;
if (!this.isMove) {
this.move(keyCode);
}
},
// 键盘释放事件
keyup : function(e) {
// 判断是否为键盘释放
if (this.keyCodeAndDirection[e.keyCode]) {
// 停止移动
this.stopMove();
} else if (e.keyCode == 32) {
// 设置为非发弹中
this.isSend = false;
}
},
// 移动
move : function(keyCode) {
// 设置为移动中
this.isMove = true;
var _this = this;
// 判断移动方向
switch(this.keyCodeAndDirection[keyCode]) {
case "left" : {
this.moveId = setInterval(function() {_this.moveLeft();}, _this.movesp);
break;
}
case "right" : {
this.moveId = setInterval(function() {_this.moveRight();}, _this.movesp);
break;
}
default : break;
}
},
// 向左移动
moveLeft : function() {
var left = this.dom["offsetLeft"];
left = left - this.movepx >= 0 ? left - this.movepx : 0;
this.dom.style["left"] = left + "px";
if (left == 0) {
this.stopMove();
}
},
// 向右移动
moveRight : function() {
var left = this.dom["offsetLeft"];
var maxDistance = this.gameWidth - this.dom.clientWidth;
left = left + this.movepx <= maxDistance ? left + this.movepx: maxDistance;
this.dom.style["left"] = left + "px";
if (left == maxDistance) {
this.stopMove();
}
},
您可能感兴趣的文章: