Javascript 使用它,当您使用MBlock.prototype={}时,该方法运行良好在第6行。当我将该行更改为MBlock.prototype=createjs.Container()时该方法已停止工作。我确保在左上角的下拉菜单中选择CreateJS扩展,并尝试将默认
Javascript 使用它,当您使用MBlock.prototype={}时,该方法运行良好在第6行。当我将该行更改为MBlock.prototype=createjs.Container()时该方法已停止工作。我确保在左上角的下拉菜单中选择CreateJS扩展,并尝试将默认,javascript,class,inheritance,scope,Javascript,Class,Inheritance,Scope,使用它,当您使用MBlock.prototype={}时,该方法运行良好在第6行。当我将该行更改为MBlock.prototype=createjs.Container()时该方法已停止工作。我确保在左上角的下拉菜单中选择CreateJS扩展,并尝试将默认的容器初始化添加到MBlock初始化的开头。你知道我们如何编辑JSFIDLE使它继承容器,并且仍然有一个有效的选择方法吗?我终于让它工作了。最大的问题是一个非常愚蠢的打字错误……我把this.select=sel而不是this.selected
使用它,当您使用
MBlock.prototype={}时,该方法运行良好代码>在第6行。当我将该行更改为MBlock.prototype=createjs.Container()时代码>该方法已停止工作。我确保在左上角的下拉菜单中选择CreateJS扩展,并尝试将默认的容器
初始化添加到MBlock
初始化的开头。你知道我们如何编辑JSFIDLE使它继承容器,并且仍然有一个有效的选择方法吗?我终于让它工作了。最大的问题是一个非常愚蠢的打字错误……我把this.select=sel代码>而不是this.selected=sel代码>我基本上将代码< >选择< /代码>为代码中的布尔变量>选择方法。你的小提琴和建议帮了大忙。
<!DOCTYPE html>
<html>
<head>
<title>M-Blocks Game</title>
<link href="../_shared/demo.css" rel="stylesheet" type="text/css">
<script src="http://code.createjs.com/easeljs-0.7.0.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
var stage, mBlocks, bkgd;
var sw, sh;
var activeID;
//MBlock class inherits from Container. MBlocks are squares that in addition to Container
//properties have velocity and rotational velocity and can be either active or inactive
//and selected or not selected
var MBlock = function (x,y,color) {
this.initialize(x,y,color);
};
MBlock.prototype = new createjs.Container();
MBlock.prototype.Container_initialize = MBlock.prototype.initialize;
MBlock.prototype.initialize = function(x,y,color) {
this.Container_initialize();
this.x = x;
this.y = y;
this.selected = false;
this.color = color;
this.lightCol = "#DDD";
//Create and draw block
this.block = new createjs.Shape();
this.block.graphics.beginFill(this.color).drawRoundRect(x,y,30,30,7);
this.addChild(this.block);
//Create and draw light bulb
this.light = new createjs.Shape();
this.light.graphics.beginFill("#DDD").drawCircle(this.x+15,this.y+15,5);
this.addChild(this.light);
};
MBlock.prototype.select = function(sel) {
this.select = sel;
this.light.graphics.clear();
this.lightCol = "red";
if (sel) this.lightCol = "red";
else this.lightCol = "#DDD";
this.light.graphics.beginFill(this.lightCol).drawCircle(this.x+15,this.y+15,5);
};
function init() {
stage = new createjs.Stage("demoCanvas");
sw = stage.canvas.width;
sh = stage.canvas.height;
activeID = 0;
bkgd = new createjs.Shape();
bkgd.graphics.beginFill("#DDF").drawRoundRect(0,0,sw,sh,7);
stage.addChild(bkgd);
mBlocks = [];
mBlocks.push(new MBlock(0,0,"#0F0"));
mBlocks.push(new MBlock(35,0,"#0F0"));
activeID = 1;
mBlocks[1].select(true);
stage.addChild(mBlocks[0],mBlocks[1]);
stage.update();
}
function reactKey(event) {
if(event.keyCode==32) { //space key--switch between selection of active mBlocks
mBlocks[activeID].select(false);
activeID += 1;
if (activeID >= mBlocks.length) activeID = 0;
mBlocks[activeID].select(true);
stage.update();
}
}
function reactKeyUp(event) {
//Code here
}
function onTick(){
//Code here
}
</script>
function init() {
//init code
}
init();
(function(){
//init code
})();
MBlock.prototype.select = function(sel) {
this.select = sel; //Here was the problem
//more code
};
MBlock.prototype.select = function(sel) {
this.selected = sel;
//more code
};