简体   繁体   中英

What is best way to create scrolling WORLD?

In this game im trying to create, players are going to be able to go in all directions

I added one single image(1024x768 2d texture) as background, or terrain.

Now, when player moves around I want to display some stuff.

For example, lets say a lamp, when player moves enough, he will see lamp. if he goes back, lamp will disappear because it wont be anymore in screen

If Im unclear, think about mario. when you go further, coin-boxes will appear, if you go back they will disappear. but background will always stay same

I thought if I spawn ALL my sprites at screen, but in positions like 1599, 1422 it will be invisible because screen is only 1024x768, and when player moves, I will set place of that sprite to 1599-1,1422-1 and so. Is it a good way to do this ?

Are there better ways?

There are two ways you can achieve this result.

  1. Keep player and camera stationary, move everything else.
  2. Keep everything stationary except the player and the camera.

It sounds like you are trying to implement the first option. This is a fine solution, but it can become complicated quickly as the number of items grows. If you use a tile system, this can become much easier to manage. I recommend you look into using a tile engine of some sort. There are a lot of great tile map editors as well.

Some resources for using Tiles:

  • Tiled -- Nice Map Editor
  • TiledLib -- XNA Library for using Tiled Maps

What you're describing there is a Viewport , which describes a portion of the 'world' that is currently visible.

You need to define the contents of your 'world' somehow. This can be done with a data structure such as a scene graph, but for the simple 2D environment you're describing, you could probably store objects in an array. You would need to bind your direction keys to change the coordinates of the viewport (and your character if you want them to stay centered).

It's a good idea to only draw objects that are currently visible. Without knowing which languages or packages you are using it's difficult to comment on that.

I would look into Parallax scrolling . Here is an example of it in action.

If this is what you require, then here is a tutorial with source code.

XNA Parallax Scrolling

After you are finished with basic scrolling, try to implement some frustum culling. That is only draw objects which are actually visible on the screen and avoid unnecessary drawing of stuff that cannot be seen anyway.

I would prefer solution number 2 (move player and camera) - It would be easier for me, but maybe its just personal preference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM