【发布时间】:2017-03-16 23:20:58
【问题描述】:
我正在尝试扩展 Container 类,但我遇到了一些奇怪的行为(至少对我来说很奇怪)。我以与 createjs 中相同的方式创建了 Item “类”。我在调用 setValue 时添加了一个形状对象。这是 Item 类:
this.test = this.test||{};
(function() {
"use strict";
var Item = function() {
this.initialize();
};
var p = Item.prototype = new createjs.Container();
p.initialize = function() {
};
p.setValue = function(color){
if (this.hasOwnProperty("bg"))
this.removeChild(this.bg);
this.bg = new createjs.Shape();
this.bg.graphics.beginFill(color).drawRoundRect(-50/2,-50/2,50,50,4);
this.addChildAt(this.bg);
};
p.getValue = function(){
return this.value;
};
test.Item = Item;
}());
现在我在 html 中执行以下操作。我创建了 2 个对象,我希望每个对象都创建一个不同颜色的子项。结果是每个项目实例包含 2 个形状,并且两个项目对象看起来相同。当我改变第二个对象的“bg”形状的位置时,它也改变了第一个项目对象。
function init()
{
canvas = document.getElementById("canvas");
stage = new createjs.Stage(canvas);
createjs.Ticker.addEventListener("tick", stage);
var item1 = new test.Item();
stage.addChild(item1);
item1.setValue("#555555");
item1.x = 50;
item1.y = 50;
var item2 = new test.Item();
stage.addChild(item2);
item2.setValue("#999999");
item2.x = 300;
item2.y = 50;
item2.bg.x += 10;
item2.bg.y += 10;
stage.update();
}
我附上了截图。我正在使用最新的 createjs 版本。我要扩展的 Container 类是 here。
【问题讨论】:
标签: javascript easeljs createjs