我是Java语言的新手,我正在尝试编写一个涉及移动块的简单游戏。块由继承MBlocks类的类createjs.Container表示,我添加了一个名为select的方法,如果传递了true,则该方法应将块中的圆圈的颜色变为红色。如果通过false,则为灰色。在init()函数中这可以正常工作,但是当我尝试通过击键事件调用该函数时,控制台会告诉我select方法不存在。不起作用的行在reactKey函数中,如下所示:mBlocks[activeID].select(true);。我已经在下面发布了代码。我无法确定这是变量范围还是具有继承性或完全其他类的类的问题。任何的建议都受欢迎。

<!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>

最佳答案

你在叫init吗?...

在发布的代码中,您永远不会调用init,因此,在这种情况下,您还没有定义mBlocks数组。

也许您可以执行以下操作(如果您仍然希望使用init函数,否则可以使用IIFE)

function init() {
    //init code
}
init();


使用IIFE

(function(){
    //init code
})();


最后的建议...停止思考“类” ... javascript中没有这样的事情...最多您拥有构造函数,该函数创建具有相似结构(创建模式)的对象...停止思考静态类然后开始思考动态定义。

编辑
在注释中解决了这个问题一段时间之后,我们终于在select方法中发现了一个错字。

原始代码,带有错别字:

MBlock.prototype.select = function(sel) {
    this.select = sel; //Here was the problem
    //more code
};


更正的代码:

MBlock.prototype.select = function(sel) {
    this.selected = sel;
    //more code
};

07-24 09:46
查看更多