English 中文(简体)
How to create a side scroller game?
原标题:

I want to create a game where the user can go to the right-most part of the screen, and if they go any further, they will slide into a new view.

So basically, if they have 3 monsters chasing them on screen # 1, and they go all the way to the right and keep going, they will enter screen #2 where there will be 5 new monsters and a new terrain.

When the game starts, should I initialize all the NPC monsters and terrain features for every screen right away, or should i initialize each screen as they enter it? And what is the best format/way for storing what goes into each screen?

最佳回答

should i initalize all the NPC monsters and terrain features for every screen right away

You can, there certainly isn t anything stopping you from doing that:

However it depends on how big your levels are and the tradeoff between load time and lags during the game (for example when the next screen is loaded).

I would suggest the following:

First: Threads

Create two threads that run concurrently, one that is repsonsible for the actual gameplay, and another whose only responsibility is to load new terrain. The gameplay thread is the main one, and will trigger the loader thread to load new terrain data when the player goes to the next screen.

Second: Reducing lags between one terrain and another

Avoid loading the next terrain upon entering it. What you should do instead is (for a sidescroller), load the terrain that s one to the left, and load the terrain that s one to the right of the current position. That way you don t need to keep the entire level in memory at once, and at the same time will never run into a lag upon entering the adjacent terrain. This works rather well with the first point.

This also fits in nicely with your requirement of having the NPC monsters in one terrain being able to "follow" you to the next terrain (if you haven t killed them); with the caveat being that if the player gets through two screens in a row without the NPC monsters getting through one, they are not able to folow you further; which is how many of the sidescrollers I have played appear to work anyway.

Also note that this means that that when you start a level, you will have to load either 2 or 3 terrains at once (depending on whether you start at the edge or the middle of the level), but subsequently, you will only ever have to load one terrain at a time, because all you have to do is swap out the current one with the adjacent one each time the player advances to the next terrain

what s the best format / way for storing what goes into each screen?

Serialize

Given that your chosen platform is Java, I would say the most load-time efficient way of persisting the level data is serialization. Make the classes which store information about the levels and the object in in implement the Serializable interface.

If you do choose this option, when you create the levels for the first time, you would either have to create special methods to hardcode the intialisation of each level and then Serialize them before loading up the game; or you would have to build a level editor.

XML

This option increases your load times considerably as compared to deserialising objects, but it is more robust, and and is easy as hell to make changes. This is probably the best option if your levels aren t very complicated.

In fact, there is nothing stopping you from doing a combination of both.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签