Dealing with infinite/huge worlds in actionscript

2020-05-06 11:06发布

How are infinite/huge procedural generated worlds achieved with minimal lag in actionscript 2? In a game like Terraria or Minecraft for example. What would the best way to go about handling huge world like this be?

Obviously looping through every block and moving them that way won't work. I've tried placing blocks into 50x50 'chunks' and then moving each of the chunks, but the result isn't anywhere near as smooth as it should be.

Is there any way to completely disable sections of the map if the player isn't near them? Would it be possible to simply store them in memory and load them when needed?

Any help is appreciated, thanks!

1条回答
倾城 Initia
2楼-- · 2020-05-06 11:09

If your game is predominantly tile-based, that is, the world has same-size blocks as its base unit, it is most practical to store your world in an array. Instead of placing your blocks by hand, an array like the following can store keys that correspond to specific block types:

"grass" "grass" "grass" "water" "water"
"grass" "water" "water" "water" "water"
"grass" "grass" "woods" "woods" "woods"
"grass" "grass" "woods" "woods" "woods"
"grass" "grass" "woods" "woods" "woods"
"grass" "grass" "grass" "woods" "woods"

Imagine that in the above example, the player can only see nine blocks at a time, i.e.

"grass" "woods" "woods"
"grass" "woods" "woods"
"grass" "woods" "woods"

This is the player at position 2,2 on the world array.

Whenever the player moves, its position with respect to the array is incremented or decremented respectively. So moving upward to would decrease the position value to 2,1 and load the blocks that are located farther north.

From the array, you would retrieve that the blocks immediately above are "water" "water" "water", and would load three water movieclips. Just in case, this answer shows how to load movieclips dynamically.

Also, a quick way to move the player with respect to the world, instead of moving the entire world with respect to the player, is to change the values _root._x and _root._y.

In the end, if the game is too graphics intensive, have you considered traversing the world on a 'room-based' system, where the player's position is not fixed, and every fifty blocks or so goes to a new screen of blocks? That would easily cut down on the strain.

AS2 is absolutely great for working with simple games that you want to get up and running quickly, although the nature of AS3 may be more suited for loading many objects dynamically.

查看更多
登录 后发表回答