这是我的代码。我怎样才能重构它以轻松地将它们放入JavaScript数组中? 这是我想放在数组中的一些pic容器。
Ace = new createjs.Bitmap(loader.getResult("Ace"));
aceContainer = new createjs.Container();
aceContainer.addChild(Ace);
aceContainer.x = 90;
aceContainer.y = 148;
aceContainer.width = 276 ;
aceContainer.height = 266;
stage.addChild(aceContainer);
K = new createjs.Bitmap(loader.getResult("K"));
kContainer = new createjs.Container();
kContainer.addChild(K);
kContainer.x = 376;
kContainer.y = 148;
kContainer.width = 276 ;
kContainer.height = 266;
stage.addChild(kContainer);
Q = new createjs.Bitmap(loader.getResult("Q"));
qContainer = new createjs.Container();
qContainer.addChild(Q);
qContainer.x = 662;
qContainer.y = 148;
qContainer.width = 276 ;
qContainer.height = 266;
stage.addChild(qContainer);
答案 0 :(得分:0)
类似
function doThingsPeter(s, x, y, width, height, root) {
var bm = new createjs.Bitmap(loader.getResult(s));
var container = new createjs.Container();
container.addChild(bm);
container.x = x;
container.y = y;
container.width = width;
container.height = height;
root.addChild(container);
return container;
}
var containers = [];
containers.push(doThingsPeter('Ace', 90, 148, 276, 266, stage));
containers.push(doThingsPeter('K', 376, 148, 276, 266, stage));
containers.push(doThingsPeter('Q', 662, 148, 276, 266, stage));
可能? 编辑:响应“容器命名怎么样” - 我只能猜测你可能需要像
这样的东西function doThingsPeter(s, x, y, width, height, root) {
var bm = new createjs.Bitmap(loader.getResult(s));
var container = new createjs.Container();
container.addChild(bm);
container.x = x;
container.y = y;
container.width = width;
container.height = height;
root.addChild(container);
return container;
}
var containers = {};
containers.Ace = doThingsPeter('Ace', 90, 148, 276, 266, stage);
containers.K = doThingsPeter('K', 376, 148, 276, 266, stage);
containers.Q = doThingsPeter('Q', 662, 148, 276, 266, stage);
但那不是现在的数组
或者您想要的是:
var containers = {};
var bitmaps = {};
var arrayOfThings = [
{ target: 'Ace', x: 90, y: 148, width: 276, height: 266},
{ target: 'K', x: 376, y: 148, width: 276, height: 266},
{ target: 'Q', x: 662, y: 148, width: 276, height: 266}
];
arrayOfThings.forEach(function(thing) {
var target = thing.target;
var bm = bitmaps[target] = new createjs.Bitmap(loader.getResult(target));
var container = containers[target] = new createjs.Container();
container.addChild(bm);
container.x = thing.x;
container.y = thing.y;
container.width = thing.width;
container.height = thing.height;
stage.addChild(container);
});
结果:
Ace
容器Ace
位图等
第四次和最终尝试(再次基于评论)
function doThingsPeter(s, x, y, width, height, root) {
var bm = new createjs.Bitmap(loader.getResult(s));
var container = new createjs.Container();
container.addChild(bm);
container.x = x;
container.y = y;
container.width = width;
container.height = height;
root.addChild(container);
return container;
}
aceContainer = doThingsPeter('Ace', 90, 148, 276, 266, stage);
kContainer = doThingsPeter('K', 376, 148, 276, 266, stage);
qContainer = doThingsPeter('Q', 662, 148, 276, 266, stage);
现在根据你的问题你有aceContainer,kContainer和qContainer