Jump to content

create an oop grid array ?


espace
 Share

Recommended Posts

hi, 

i know create an oop object with a simple graphic shape like this :

 // create an new instance of a pixi container
var container = new PIXI.Container();
// create a renderer instance
var renderer = PIXI.autoDetectRenderer(320, 480);

// add the renderer view element to the DOM
document.body.appendChild(renderer.view);

requestAnimationFrame( animate );


var T={}
T.draw = function() {
PIXI.Graphics.call(this)
this.beginFill(0xFFFFFF)
this.drawRect(100,100,80,200)
}

T.draw.prototype = Object.create(PIXI.Graphics.prototype);
T.draw.prototype.constructor = T.draw;

var button=new T.draw()

container.addChild(button);

function animate() {

	requestAnimationFrame( animate );
	renderer.render(container);
}

 

but I am lost on the fact of doing the same with  an 2d array. Can you point me ?

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]);
    };
};

 

Link to comment
Share on other sites

function Grid() {
PIXI.Container.call(this);
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;
        this.addChild(graphics[j][i]);
    };
};
this.graphics = graphics;
}

Grid.prototype = Object.create(PIXI.Container.prototype);
Grid.prototype.constructor = Grid;

Though i dont recomment use graphics, better to create texture and then sprites from them. "var tex = renderer.generateTexture(graphics)" will help you with that :)

Link to comment
Share on other sites

hi @ivan.popelyshev, thanks for your snippet but i have this error :

TypeError: child is undefined 

at line :

this.addChild(graphics[j][i]);

for the texture, i use always but for the forum is use graphics, it is easier to test for others.

 

 

 

Link to comment
Share on other sites

ok i have seen my error i forgot to call the prototype by

var array=new Grid() in place of var array=Grid() 

Is it possible to make the same ( a little like mattstyles say) like this :

(but as a good newbie this don't work :angry:)...

could you be say where is my error ?....

function Grid() {
var rectangle=PIXI.Graphics()
rectangle.beginFill(0xFF3300)
rectangle.drawRect(0,0,40,40)

PIXI.Graphics.call(this)
var grid=[];
for (var j = 0; j < 5; j++) {
    grid[j] = [];
    for (var i = 0; i < 5; i++) {
        grid[j][i] = rectangle;
this.addChild(rectangle)
};
};
}

Grid.draw.prototype = Object.create(PIXI.Graphics.prototype);
Grid.draw.prototype.constructor = Grid;

var array=new Grid()

 

Link to comment
Share on other sites

thanks for the tips.

i have found the solution :

have a nice day ;)

function drawre() {
var rectangle=new PIXI.Graphics()
rectangle.beginFill(0xFF3300)
rectangle.drawRect(0,0,40,40)
return rectangle
}

function Grid() {
	PIXI.Container.call(this)
var grid=[];
for (var j = 0; j < 5; j++) {
    grid[j] = [];
    for (var i = 0; i < 5; i++) {
        grid[j][i] = drawre(); 
        grid[j][i].x = i*50
        grid[j][i].y =j*50

this.addChild(grid[j][i])
};
};

this.grid = grid

}

Grid.prototype = Object.create(PIXI.Container.prototype);
Grid.prototype.constructor = Grid;

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...