Jump to content

Wrong respawn position with game.state.restart();


DazZ
 Share

Recommended Posts

When my player dies, he gets respawned to an x,y location that seems to be what the camera is currently showing rather than the maps x,y.

 

If the camera hasn't moved the player spawns in the correct spot, otherwise he'll be a a few hundred pixels from where it died.

 

However I have fixed this by loading a tilemap which is the size of the screen and then restarting the frame like so.

Player.prototype.death = function(){    //Set map and world size to camera size to fix respawning in wrong place    map = game.add.tilemap('menuMap');    platforms = map.createLayer('platforms');    platforms.resizeWorld();    game.state.restart();};

menuMap is 800,600 the same as the camera whilst the level maps are a bit larger, but setting the world to 800,600 and then restarting fixes my spawn issue.

 

Obviously this is rather hacky and I feel I'm missing something that should make this cleaner. Any insight would be lovely!

 

Edit: I've narrowed it down to setting the world boundaries to nothing instead of loading another map, but I still feel like I'm doing something wrong.

Player.prototype.death = function(){    game.world.setBounds(0, 0, 0, 0); //fixes spawning in wrong place    game.state.restart();};
Link to comment
Share on other sites

Not sure if this is the solution to the problem or not but changing the state does not reset the world bounds, so I think if you explicitly set the world bounds to whatever size they need to be at the start of each state (and for states which do not have specific requirements, set the bounds to the size of the game, which is the default)  things should hopefully work as expected.

Link to comment
Share on other sites

I think if you explicitly set the world bounds to whatever size they need to be at the start of each state (and for states which do not have specific requirements, set the bounds to the size of the game, which is the default)  things should hopefully work as expected.

 

In the create function for every state I have platforms.resizeWorld(); which I think should be doing just that.

It is called before putting the player in game as well.

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