我是一名使用PIXI的javascript初学者。
我不知道如何使用行和行格式化数组。我尝试这个片段但是当我想访问这个数组的特定值时:
alert(graphics[2][1].position.x)
我有这个错误:
Uncaught TypeError: Cannot read property '1' of undefined(anonymous function) @ main.js:40
这是我的完整代码段:
var renderer = PIXI.autoDetectRenderer(800, 600,{backgroundColor : 0x1099bb});
document.body.appendChild(renderer.view);
var stage = new PIXI.Container();
var container = new PIXI.Container()
stage.addChild(container);
for (var j = 0; j < 5; j++) {
for (var i = 0; i < 5; i++) {
var graphics = new PIXI.Graphics();
graphics.beginFill(0xFF3300);
graphics.lineStyle(4, 0xffd900, 1);
graphics.drawRect(0,0,10,10);
graphics.position.x= 40 * i;
graphics.position.y=40 * j;
container.addChild(graphics);
};
};
alert(graphics[2][1].position.x)//here my error
container.x=100
container.y=100
container.scale.x=container.scale.y=.1;
animate();
function animate() {
requestAnimationFrame( animate );
container.rotation += .1;
renderer.render(stage);
}
答案 0 :(得分:0)
如果你想拥有2d数组,那么你需要使用这段代码:
var graphics = [];
for (var j = 0; j < 5; j++) {
graphics[j] = [];
for (var i = 0; i < 5; i++) {
graphics[j][i] = new PIXI.Graphics();
graphics[j][i].beginFill(0xFF3300);
graphics[j][i].lineStyle(4, 0xffd900, 1);
graphics[j][i].drawRect(0,0,10,10);
graphics[j][i].position.x = 40 * i;
graphics[j][i].position.y = 40 * j;
container.addChild(graphics[j][i]);
};
};
然后你就可以这样称呼:
alert(graphics[2][1].position.x);