【发布时间】:2016-03-11 20:51:56
【问题描述】:
假设我有 2 个相似的类:
//one class
function classA() {
var texture = PIXI.Texture.fromImage("someImage");
PIXI.extras.TilingSprite.call(this, texture, 224, 219);
this.position.x = 0;
this.position.y = 0;
this.tilePosition.x = 0;
this.tilePosition.y = 0;
}
classA.constructor = classA;
classA.prototype = Object.create(PIXI.extras.TilingSprite.prototype);
//another class
function classB() {
var texture = PIXI.Texture.fromImage("anotherImage");
PIXI.extras.TilingSprite.call(this, texture, 36, 42);
this.position.x = 0;
this.position.y = 0;
this.tilePosition.x = 0;
this.tilePosition.y = 0;
}
classB.constructor = classB;
classB.prototype = Object.create(PIXI.extras.TilingSprite.prototype);
在另一个文件中,我从 classB 创建了 1 个 classA 和 3 个实例,并将它们全部添加到容器中:
container = new PIXI.Container();
renderer = PIXI.autoDetectRenderer(224, 219, {view:document.getElementById("some-canvas")});
a = new classA();
container.addChild(a);
b1 = new classB();
container.addChild(b1);
b2 = new classB();
container.addChild(b2);
b3 = new classB();
container.addChild(b3);
我想以某种方式将 classB 关联到 classA,这样我就不需要在每次创建 1 个 classA 实例时创建 3 个 classB 实例(换句话说,我想执行一个 addChild() 函数每个类实例化)。这可能吗?
【问题讨论】:
-
虽然我不确定我是否真的在回答中回答了正确的问题。由于我不理解以下语句:“这样我就不需要在每次创建 1 个 classA 实例时创建 3 个 classB 实例”。因为如果您多次执行相同的操作,您可以将其分离到它自己的功能中。但是将 addChild 放在构造函数中是 imo。仍然是一个糟糕的选择......或者你可以制作 addChildren 函数,它遍历一个数组并将所有子元素添加到给定的容器中。
标签: javascript animation pixi.js