Jump to content

Using sprite from variable doesn't display tile


NickOver
 Share

Recommended Posts

Hi!

In custom preinit function i add sprite from image:

this.images['#'] = PIXI.Sprite.fromImage('assets/floor.png');

Next on render function i want to use it:

Game.prototype.render = function(data) {
	i = 0;
	for(y in data['map']) {
		for(x in data['map'][y]) {
			if( data['map'][y][x] != null ) {
				console.log(this.images);
				tile = this.images['#'];
				tile.x = 32 * x;
				tile.y = 32 * y;
				this.stage.addChild(tile);
				i++;
			}
		}
	}
	console.log(i);
	console.log(this.stage);
	this.renderer.render(this.stage);

	// this.socket.close();
}

But i have blank canvas. Few times on start i have error: //In line tile.x = 32 * x;

Quote

Cannot set property 'x' of undefined

If i use :

Game.prototype.render = function(data) {
	i = 0;
	for(y in data['map']) {
		for(x in data['map'][y]) {
			if( data['map'][y][x] != null ){
				console.log(this.images);
				tile = PIXI.Sprite.fromImage('assets/floor.png');
				tile.x = 32 * x;
				tile.y = 32 * y;
				this.stage.addChild(tile);
				i++;
			}
		}
	}
	console.log(i);
	console.log(this.stage);
	this.renderer.render(this.stage);

	// this.socket.close();
}

Everything render fine. Additionally when i use this method this.stage have 5566 elements in children array but loop walk only 121 times.

Is very problematic for me because i wanna use tiles from tileset. There is my code:

Game.prototype.prepareTileset = function() {

	tileset = PIXI.TextureCache["assets/tileset.png"];

	tileset.frame = new PIXI.Rectangle(128, 128, 32, 32);

	game.images['#'] = new PIXI.Sprite(tileset);


	tileset.frame = new PIXI.Rectangle(128, 128, 32, 32);

	game.images['W'] = new PIXI.Sprite(tileset);


	tileset.frame = new PIXI.Rectangle(128, 128, 32, 32);

	game.images['S'] = new PIXI.Sprite(tileset);


	tileset.frame = new PIXI.Rectangle(128, 128, 32, 32);

	game.images['A'] = new PIXI.Sprite(tileset);

	console.log(game.images);
}

But i still have blank canvas.

Initially i thought PIXI drop from memory added children but console.log(tile); display object, and i doesn't give PIXI reference to them.

Can someone explain me where i make a mistake, and explain why my code doesn't work? 

 

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...