Jump to content

Dynamic load of tilemaps is possible?


Trova
 Share

Recommended Posts

Hello, first of all sorry if my english is kinda bad, I'm still learning.

As you can see, I'm new to Phaser and I'm doing a kind of TMMORPG as a one of my school projects (I have never made one but I think that I have the fundamental concepts), and I was wondering if it is possible to load tilemaps on the fly, I mean, the server is who will send the "tilemap.json" every time a player enter to another area.

I dont know if the loading of a tilemap must be done on the preload() function (I am actually doing in that way), or can be done on the fly while the player is warping to another area.

Hope you can help me with my doubts.

Link to comment
Share on other sites

Yeah, definitely possible. You can use game.add.sprite / game.load anywhere.

 

For example, for monster / player attacks (I wait until a Player uses the skill to load the asset) -- Once the user loads the assets, then I don't need to load it anymore ~ I store that in the  SpriteSkills object (dynamically updated) -- If a player joins and uses a skill for example, if the player has it loaded they won't need to re-load it or call that function, etc.

		// If the user doesn't have this skill effect sprite already loaded, load it.				if(!SpriteSkills[SKILL_ID]){ // Only load in the sprite if the player doesn't have the skill sprite loaded.  WIDTH, HEIGHT, STEPS (FRAMES)				SpriteSkills[SKILL_ID] = game.load.spritesheet('spriteskill'+SKILL_ID, '../img/rpg/Skills/skill0.png', C_SKILL_ANIMATION.width, C_SKILL_ANIMATION.height, C_SKILL_ANIMATION.steps);				game.load.onFileComplete.add(fileComplete, this);				game.load.start();		}else{				doSkillAnimation(); // Async is such a  bitch...		}

C_SKILL_ANIMATION is just referencing back to a JSON loaded file with skill properties.

 

 

Edit: awww, I saw you said tilemaps, I'm pretty sure you can use the same methodology. Just call the same stuff you would in create/preload

Link to comment
Share on other sites

Thanks anyway for the answer, I talked to my consultant in the school and he said that It could be a better idea to have the player to downlaod all the game map info, in that way, inside the game the player should not wait until all the request, the map download and the render of the map, and It should not be as hard to the net taking into consideration that todays ISP has decent bandwidth for a simple 2d browser game.

Anyway I am planning on trying to make the load of the maps dynamically just as practice and because I think that must be done in that way. If i am wrong then on the course of development I will decide if I should switch.

Again thanks for the reply

Link to comment
Share on other sites

Thanks anyway for the answer, I talked to my consultant in the school and he said that It could be a better idea to have the player to downlaod all the game map info, in that way, inside the game the player should not wait until all the request, the map download and the render of the map, and It should not be as hard to the net taking into consideration that todays ISP has decent bandwidth for a simple 2d browser game.

Anyway I am planning on trying to make the load of the maps dynamically just as practice and because I think that must be done in that way. If i am wrong then on the course of development I will decide if I should switch.

Again thanks for the reply

 

Hey, that's cool :)  Just to add, no disrespect, but I would sadly disagree with him. For a game that has thousands of assets and plenty of maps it's not ideal to load files that the client will not use.

 

 

Especially since you're making a TMMORPG.  Take a look at a game called CrossCode, http://www.cross-code.com/en/home they dynamically load all their assets / maps. It would be silly to just load in everything at once.

 

Edit: I know your game might not be that big, but since you mentioned 'mmorpg' I took it as big and can only assume, haha  :)

Link to comment
Share on other sites

Now that I'm finally in home im trying to implement a simple function that receives an url to the tilemap.png and try to draw it on screen but im getting 

Phaser.Cache.getImage: Key "meadows_tileset" not found in Cache.

everytime i try to do a simple game.add.image(0,0,"meadows_tileset"). This also happens if I do a game.load.tilemap and then a game.add.tilemap.

I made then a more simple function, something like

function loadImage(){    tree = game.load.image('Tree','assets/tree.png');    game.load.start();      game.add.image(0, 0, 'Trre');}

I assume that this simple code, when executed, should load a tree image and draw it on my canvas, but I get the "not found in Cache" error. 

Did I get lost on somewhere? 

 

Edit: I am running the function in console, can this be the error?

Link to comment
Share on other sites

You can't simply just assume that the image will be loaded when you call game.add.image(...). Because it won't be. That's why assets should be loaded in the preload() function : because Phaser will not move to the create() function until all assets are loaded. But if you load assets anywhere else, then you don't have the same guarantee that your assets will be loaded. Loading are asynchronous.

 

If you still want to load things from outside the preload() function and want to know when the loading is complete, use the onLoadComplete callback function : http://phaser.io/docs/2.4.4/Phaser.Loader.html#onLoadComplete

 

Honestly I don't really think dynamic asset loading is a good idea. Can't you split your games into levels or worlds? And have players abilities somewhere (like a JSON), so that you could load everything in the preload() function?

Link to comment
Share on other sites

Honestly I don't really think dynamic asset loading is a good idea. Can't you split your games into levels or worlds? And have players abilities somewhere (like a JSON), so that you could load everything in the preload() function?

The thing is that I don't want players to load all the maps just entering the game. The main concept is that the player will move around the world in "tiles" of the main world (something like the old Dofus 1.29), so players will be on a small area of the map until he warp to another small area of the map.

I don't know if I am explaining the idea.
 
Edit: An idea that I am considering is every time the player warp to another map area, make a kind of reload of the game, so the server will send the new assets to load on the preload, but I think it is kinda complicated for a simple thing.
Link to comment
Share on other sites

Yeah, that's kind of what I'm suggesting. Using Game States to do that properly. Split your map into zones, and when a player changes zone, load all assets for the new zone.  Because dynamic loading is very dangerous. What happens if the loading of a tree tile or fireball takes 10 seconds? Then your game is going to look really weird. If you use States instead, then you could even display a quick loading bar/screen, so that the player knows what's going on.

Link to comment
Share on other sites

 

Yeah, that's kind of what I'm suggesting. Using Game States to do that properly. Split your map into zones, and when a player changes zone, load all assets for the new zone.

Sounds nice, just to corroborate, I will need an object that will be my State and this object must have it's preload, create, update and render functions, Am I right?
So as it will call the preload i can save on a state object the new assets urls sent by the server, that way, the player will load the new map while he sees a tiny "loading" screen.
 
Edit: Let's say that I use 2 state objects, so I can use one as a buffer (the new zone to be loaded), I need to call game.state.add every time I update the active state?
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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