How do I make a grid based game using arrays?

Get help using Construct 2

Post » Mon Oct 30, 2017 11:08 am

I'm trying to make a grid based capture game, where the tiles will be changed regularly, so wanted to try to use an array to store numbers related to the tiles and then make the layout look accordingly. I'm not sure how to go about this as i'm still a rookie on arrays.

So a grid like
1 | 2 | 2
------------
1 | 1 | 2
------------
3 | 1 | 1

Would look something like

Neutral | Enemy | Enemy
--------------------------------------
Neutral | Neutral | Enemy
---------------------------------------
Friendly | Neutral | Neutral
B
3
Posts: 1
Reputation: 177

Post » Mon Oct 30, 2017 3:24 pm

The more I learn C2, the less often I use arrays :)
Surprisingly, in many situations where you would normally use an array, C2 allows you to do the same easier without them.

For example, let's assume that your tiles are sprites. You can add a couple of instance variables to the tile sprite, like "occupied" or "objectType" and do something like this:

Repeat 5 times:
Pick an empty tile, spawn Neutral sprite on it, mark the tile as occupied, set objectType=neutral.

Repeat 3 times:
Pick an empty tile, spawn Enemy sprite on it, mark the tile as occupied, set objectType=enemy.

Repeat 1 times:
Pick an empty tile, spawn Friendly sprite on it, mark the tile as occupied, set objectType=friendly.

And now you have your "array" in a form of 9 Tile instances. You can add more instance variables to Tile sprite and store lots of data for each tile. You can pick/filter tiles using various events, loop through tiles in any order etc.
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :) If you'd like to hire me, please see this post
B
19
S
9
G
158
Posts: 2,393
Reputation: 84,413


Return to How do I....?

Who is online

Users browsing this forum: Bitsix and 1 guest