Arrays for Beginners

26
  • 249 favourites

Index

Attached Files

The following files have been attached to this tutorial:

.capx

animals-starter.capx

Download now 173.28 KB
.capx

animals-complete.capx

Download now 174.16 KB
.capx

highscores-example.capx

Download now 182.31 KB

Contributors

Stats

47,032 visits, 154,996 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

2-dimensional arrays

A 2-dimensional array has a width dimension (x) and a height dimension (y). For example, a 5x5 2-dimensional array would look like this:

The index of the top-left cell is (0,0) ie. x = 0, y = 0. The bottom-right cell is (4,4).

Looping, comparing and setting values is done in a similar way except now we need to specify both the x and y indices.

Example use for a 2-dimensional array

2-d arrays are great for representing grids and so they have a lot of uses in 2d games.

Let’s say we have a block sprite with 3 different colour animations. We want to place 12 blocks in a 4x3 grid and give them random colours. Well, we can use a 2-d array to represent the grid and we can set each cell of the array to a random colour.

Then to display our grid, we loop through the array and create a sprite with it’s position based on the current array indices and it’s animation based on the current array value.

  • 7 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hi, what's going on with second and other pages of this tutorial - it shows just an error...

  • Thank you very much.

    With new version we can change WebStorage plugin to LocalStorage plugin, And use LocalStorage On item "array_save_example" event for set animals when LocalStorage Get item "array_save_example" loaded

    1. btn loadArray on clicked > LocalStorage Get item "array_save_example"

    2. LocalStorage On item "array_save_example" get > animals Load from Json string LocalStorage.ItemValue

    • Thanks for trying to provide an update to the Webstorage reference. i'm totally lost from your update however. i'll have to find a more up to date tutorial.

  • Thanks for very good and important topic with detailed explanation

  • Best writing on arrays

  • Thank u, it helps a lot. 😁😁😁😬😬😬😬