Jump to content

Drawing an array of tiles problem


VeeShort
 Share

Recommended Posts

Curently, I am making a roguelike game and have a little problem with drawing(updating) the level on the screen.

 

N00Mn9OH4Mc.jpg

 

The map is two-dimentional array which generates at the begining of the script ( :wacko:). If the player hits an arrow button then movePlayer() function is called. Inside that function we draw the map (1 frame per player's move). That's where the trouble begins. It is starting to drop frames very badly when all the tiles are drawn together (less tiles - more drawing speed, but not enough to play the game).


 

Here is some code:

var mapSize = { // in tiles (one is 16x16 px)    width: 44,    height: 62};//...generating random arrayvar stage, renderer;start();var wallTexture = PIXI.Texture.fromImage("images/level/wall.png");var container = new PIXI.DisplayObjectContainer();function start(){     renderer = PIXI.autoDetectRenderer(992, 704, document.getElementById('canvas'));     stage = new PIXI.Stage();    this.document.onkeydown = movePlayer; } function movePlayer(){    //... <-- basic movement here    requestAnimFrame(drawMap);    renderer.render(stage);} function drawMap(){     for (var i = 0; i < mapSize.width; i++){         for (var j = 0; j < mapSize.height; j++){             if(map[i][j] == '20'){ //'20' is a wall tile                 var wall = new PIXI.Sprite(wallTexture);                 wall.position.x = i * tile;                 wall.position.y = j * tile;                 container.addChild(wall);             }            //... drawing other tiles here         }    }     stage.addChild(container); }

Thank you, for your help and time.

 

Link to comment
Share on other sites

You're adding new items to the map on each move - you should batch the whole level and then only update textures on the sprites that you know has changed (and to reuse the sprite object - not create new ones on top of it).

You can use the spirte.loadTexture method for that, an example:
http://examples.phaser.io/_site/view_full.html?d=animation&f=change+texture+on+click.js&t=change%20texture%20on%20click

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