VeeShort Posted October 20, 2014 Share Posted October 20, 2014 Curently, I am making a roguelike game and have a little problem with drawing(updating) the level on the screen. The map is two-dimentional array which generates at the begining of the script ( ). 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. Quote Link to comment Share on other sites More sharing options...
lukaszwojciak Posted October 21, 2014 Share Posted October 21, 2014 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 Quote Link to comment Share on other sites More sharing options...
VeeShort Posted October 21, 2014 Author Share Posted October 21, 2014 and to reuse the sprite object - not create new ones on top of itI don't get how to do that Can you explain it with PIXI code? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.