0

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!

  • loading chunks = storing them in memory. But of course you could have a flag to enable/disable the handling of those chunks. Also, why are you "moving each of the chunks"? Once a chunk is generated, its position does not change. – Simon Forsberg Sep 27 '13 at 21:56
  • Right, but I need to move the world somehow as this is a 2D game. – Mike Hunter Sep 27 '13 at 21:59
  • 1
    Try to move the objects inside the world instead of the entire world. Not even Minecraft or Terraria moves the world. – Simon Forsberg Sep 27 '13 at 22:01
  • Sorry, can you elaborate? Let's say one of my chunk contains 2500 blocks. Do you mean that I should simply move the one current chunk that the player is on, then somehow have a 'flag' as you stated for when I need to move the next chunk? – Mike Hunter Sep 27 '13 at 22:07
  • 1
    I don't understand why you are moving the chunks at all. When you create a chunk, you should give the chunk a "chunk index". For example, chunk (0, 1) has the blocks in x-range 0-49 and y-range 50-99. Once this chunk is created, **the chunk itself should not move**. If the player moves, you move the player. You don't move the chunk. – Simon Forsberg Sep 27 '13 at 22:09
  • Ah -- I understand. That makes much more sense, but is that capable in flash? To my understanding you can't move the stage(basically the 'camera') so you must move the background instead. – Mike Hunter Sep 27 '13 at 22:12
  • 1
    I suggest you separate the "model" from the "view". Don't show all things in the view at once. Only show the stuff that are close to the player and update that view when the player moves. Otherwise your view object would be waaaay too big. (What you're trying to do here is not very simple, but if you are a somewhat experienced programmer you will manage to do it. But I don't think a project like this is good for beginners). – Simon Forsberg Sep 27 '13 at 22:18
  • This is one of three related questions by the same user, the questions are: [AS2 Best way to decrease lag when dealing with several movieclips with onEnterFrames](http://stackoverflow.com/questions/16848043), [Dealing with infinite/huge worlds in actionscript 2](http://stackoverflow.com/questions/19060661/dealing-with-infinite-huge-worlds-in-actionscript-2), [Actionscript 2 large tile-based maps creating lag](http://stackoverflow.com/questions/15583420/actionscript-2-large-tile-based-maps-creating-lag) – Simon Forsberg Sep 28 '13 at 10:51
  • 1
    May I ask why you use AS2 instead of AS3? – Lars Blåsjö Sep 28 '13 at 17:06
  • @SimonAndréForsberg, I've managed to get something like that working(Horizontal movement works infinitely, I just need to modify the code a bit to include vertical chunks, but I'm sure I'll manage). Thanks for the suggestions. – Mike Hunter Sep 28 '13 at 19:49
  • @LarsBlåsjö, I've used AS2 for years, I've honestly just been putting off converting over to AS3, I know it'll benefit me greatly though. – Mike Hunter Sep 28 '13 at 19:50

1 Answers1

0

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.

Community
  • 1
  • 1
interpolack
  • 876
  • 10
  • 26