Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm working at making a HTML5 game. Top down, closest thing I can equate it to is the gameboy zeldas, but open world and no rooms.

What I have so far is a procedurally generated map in a multi dimensional array. And a starting position on the map. Along with this I have an array of movable and non movable tile ID's.

I also have a class for my player and have him being rendered out in the center of the starting tile.

My problem however is getting the movement sorted out for the player. I want to be able to have the character free move around the map (pixel by pixel essentially) ontop of this 2D generated world. Ideally this would allow the user to move around the walk able area of the canvas. this is simple enough for me to do, but I am having problems now moving the world.

If the user is 20% from the edge of the screen i want the world to start panning in the direction the player is heading. But I'm rather lacking in ideas of how to do this.

I've looked around for some tutorials, but am coming up blank on ideas of how to generate the playable area (zoomed in) and to then move this generated area under the player when they reach near the end of the screen.

My current idea was to generate a certain amount of tiles full size to fill the screen and place the player i the middle. Then when the user approaches the edge of the screen start generating the tiles offset by the distance moved and the direction. I can kind of see this working but I really have no idea if this is the best or easiest to code of methods for generating the world.

sorry for the lack of code but I'm still just in the theory stages of working this all out.

share|improve this question
2  
This question seems to be a combination of multiple different questions on the site. I believe the following questions can answer your questions: gamedev.stackexchange.com/questions/7135/… gamedev.stackexchange.com/questions/25030/… gamedev.stackexchange.com/questions/55113/…. As for the 20% thing, draw yourself a picture and write out some pseudo code and I bet you can figure that one out on your own. –  Byte56 Jun 27 '13 at 13:14
    
Further, search around for 2D camera, tilemaps and free movement. You'll find lots of similar questions on the site. –  Byte56 Jun 27 '13 at 13:15
add comment

1 Answer

Generating the world:

This is often done by dividing the world into a big rectangular areas (blocks).

You decide how big the areas are - making them big enough that one fills the whole screen is a good starting point.

You generate the area that the player starts in, and the 8 areas that go around it, like so:

123
4X5  < -- X is the start
678

When the player walks out of the first area into one of the others, you generate all the areas around that area. This means that areas will always be generated before the player walks into them.

If the user is 20% from the edge of the screen i want the world to start panning in the direction the player is heading

Like Byte56 said this is usually done with a camera.

Have a camera object that has an x and y position.

Every time you draw something, subtract the camera's position from the thing's position.

drawHero(hero.x - camera.x, hero.y - camera.y)

Then you can make some rules to make the camera follow the hero.

if (hero.x > camera.x + screenWidth * 0.8) camera.x += 1; //hero is 20% from right edge of the screen, so chase after them with the camera.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.