javascript acces an item of an array? -
i'm beginner javascript used pixi.
i don't know how format array row , line. try snippet when want access specific value of array :
alert(graphics[2][1].position.x)
i have error :
uncaught typeerror: cannot read property '1' of undefined(anonymous function) @ main.js:40
here complete snippet :
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); (var j = 0; j < 5; j++) { (var = 0; < 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 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); }
if want have 2d array need use code:
var graphics = []; (var j = 0; j < 5; j++) { graphics[j] = []; (var = 0; < 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]); }; };
then able call this:
alert(graphics[2][1].position.x);
Comments
Post a Comment