Multidimensional Array Tutorial
Posted: Mon Jun 06, 2011 9:42 pm
Ever want to understand Arrays better? well now you can! I've decided to make a multidimensional array tutorial out of boredom.
Q1: What are arrays and multidimensional arrays?
Alright so lets get started shall we? First, a normal array would look something like this: int array[25];. To put things simply, I guess you could say that the one variable named "array" that we just seen can have 25 different values in it. For example: array[0]=1; array[1]=345; notice how Array 0 and Array 1 both contain different = values. It's kind of hard for me to explain exactly how this works so I'll leave the rest of the basic array up to you to read up on using wikipedia
Now lets get started on the multidimensional arrays. A multidimensional array can be used just like a normal array, only you can have more dimensions. For example: int array[255][255]; is a 2D array containing a max ammount of different values of 255 for each dimension. In other words you can use it like this
You get the point? XD Using multidimensional arrays can have it's advantages. You can add as many dimensions as you like. Example: int array[255][255][255][255][255] This is a 5D array. After you get the hang of it, it's pretty simple to use. Let's try writting something that the player can use. lets make a simple 2D Array (2 dimensions)
now in the draw actor of your player use something like this
This will have the player follow the mouse, now I know most of you would probably just use follow mouse But since this is an example on how to get started using multidimensional arrays, we'll just stick with this.
Hope this helped Try using arrays for many different things, to store HP, Enemy Count, anything you want to
Q1: What are arrays and multidimensional arrays?
- An array is data structure (type of memory layout) that stores a collection of individual values that are of the same... Read More
Alright so lets get started shall we? First, a normal array would look something like this: int array[25];. To put things simply, I guess you could say that the one variable named "array" that we just seen can have 25 different values in it. For example: array[0]=1; array[1]=345; notice how Array 0 and Array 1 both contain different = values. It's kind of hard for me to explain exactly how this works so I'll leave the rest of the basic array up to you to read up on using wikipedia
- Code: Select all
http://en.wikipedia.org/wiki/Array
Now lets get started on the multidimensional arrays. A multidimensional array can be used just like a normal array, only you can have more dimensions. For example: int array[255][255]; is a 2D array containing a max ammount of different values of 255 for each dimension. In other words you can use it like this
- Code: Select all
array[0][0]=255;
array[0][1]=188;
array[1][0]=248;
You get the point? XD Using multidimensional arrays can have it's advantages. You can add as many dimensions as you like. Example: int array[255][255][255][255][255] This is a 5D array. After you get the hang of it, it's pretty simple to use. Let's try writting something that the player can use. lets make a simple 2D Array (2 dimensions)
- Code: Select all
//the first digit represents the actor, the second one represents x,y, etc.. anything else :3
int array[255][255];
now in the draw actor of your player use something like this
- Code: Select all
array[0]0]=xmouse;
array[0][1]=ymouse;
xscreen=array[0][0];
yscreen=array[0][1];
This will have the player follow the mouse, now I know most of you would probably just use follow mouse But since this is an example on how to get started using multidimensional arrays, we'll just stick with this.
Hope this helped Try using arrays for many different things, to store HP, Enemy Count, anything you want to