How to make a good tile engine?

I’m trying to make a simple tile engine because the tutorial site doesn’t go in to collision detection and such which I need. I made a class based off the tutorial to be easier to use but looping through all the tile objects freezes the game and it is irresponsive and i have to kill all haxe processes to stop it. Any tutorials or code? Here’s what I’m using at the moment:



EDIT: Oops, didn’t notice the tile colision part of the tutorial :stuck_out_tongue:

Here is an old engine using tilesheet. It works for openfl legacy, and I think it will work for next as well after little to no modification.

Thanks! I’m definitely going to give it a try. Before I do I want to know why this is slow even though I used a tile sheet. I created an array of tiles and render all of them every frame. Here is the simple tile class:

Is this a class for one single tile, or just your main class? If it is a single tile, there are some things I will have to explain to you because it looks like you aren’t clear on some of the ideas of tilesheet.

Also, I have noticed one thing: objects in haxe are passed by reference, meaning your ‘this.pos = pos’ will store pos as an reference, and when the old pos changes your pos will change as well. Use ‘this.pos.x = pos.x’ and ‘this.pos.y = pos.y’, or ‘this.pos = new Vec2().copy(pos)’ if your vector class has this functionality.

Alright, looks like this is your class for a single tile. I might not get everything right, so please correct me if I’m wrong. :stuck_out_tongue:
First, I want to explain a bit on how GPU render things using OpenGL(assuming you are doing stuff for native platform). To draw things using the GPU, you upload the vertex and texture information to the GPU and then you make a draw call. Doing multiple draw calls may involve switching and reuploading informations, therefore is slow.
Now the draw tile function can take all your information in the data array, submit it, and make one draw call. The reason it is very fast, is because 1: you only need one draw call to render many objects. 2: you only need to upload one Single texture to render different tiles(by packing many images into a big one so that you only need to upload once, I recommend using the software TexturePacker for packing). Therefore, the less draw tile calls you make, the more efficient it is.
Now the most efficient method of utilizing the tilesheet class is used by the engine I gave you: use one single sprite as canvas, use tile classes to store some information about position and such, and then call render function to arrange all informations and call draw tile once per frame. DO NOT use sprite as tile class, because OpenFL use one draw call for each sprite: much much slower than tiles.
Now, the tile layer engine can’t have parent transformation. In order to have the same transformation function as the normal sprite, you need to modify the engine to add the matrix multiplication which I have already done for my own engine but might not be efficient enought.
Anyways. I hope that was helpful :smiley:

Thanks for the pass by reference tip. I’m not sure it made much of a difference though. And yes, this is my Tile class. Enlighten me on any mistakes. I’ve been using haxe and OpenFL for 3 days :slight_smile:

It makes a BIG difference. If you pass by reference, when you modify the old position your new position will change - all your tiles are gonna have the same position!

In terms of programming, every variable you make will take up some space in the memory. In haxe, for objects, if you don’t use new Vec2() to make a new object, there will always be just one object sitting in memory, and your reference is just an address of where that object is sitting. Any operation will just be modifying that object: see the problem here? You may have 5 tiles, but there is 1 pos object, all the other ones are just referring to it.

Well I think you already know this. So nvm lol

Do you have any other prior knowledge of game developing tho? Before starting on OpenFL I was using flash and AS3. Because flash is still more well known and is older, there are much more tutorials, larger community and more through documentations on AS3 than OpenFL. I recommend looking into them before starting on OpenFL as it is slightly more advanced.

I have most experience in OpenGL and LibGdx. OpenFL/flash is kind of weird for me because I’m used to having a render method where I handle drawing everything. I’ll definatly look into more AS3 as I’ve seen tutorials that look similar to OpenFL.

As for the tile engine tool it throws an error: Uncaught exception - Invalid field access : rect

at var r:Rectangle = cast bmp.rect.clone();

Are you sure you are using legacy? If you are using flash develop, click on the deploy target text box and change it to “windows -Dlegacy” and then deploy.

Oh, I’m using sublime text and probably the newest OpenFL and Haxe.

Umm, the thing is to change the deployment target to “windows -Dlegacy” if you are currently using “Windows”.

The tilesheet API should be very easy for you: it allows you to handle the drawing yourself. Consider each drawtile as a draw call. And you can just look into the engine and find out how it does the drawings.

I’m on linux so I guess do something with neko?

Not sure about linux. Maybe someone more familiar with linux can help.

Recommend any flash tutorials to help me with OpenFL?

Some general tutorials here:

Simple games like:

1 Like