So you’re starting to learn more and more about coding for the Wii and you’ve come to the point where you your variables to be more organised. This may be the cause if you have multiple objects on the screen that need to move around, etc.

You are actually able to put variables in structures, which mean you can have multiple variables in the one element of the structure.


For example, if you wish to have an object’s x / y co-ordinates as well as if the objects are on the screen, you can use the example below.

view sourceprint?1.struct object_struct {
2.
int x;
3.
int y;
4.
bool on_screen;
5.};
Then you just assign this structure to a variable (10 elements of x, y and on_screen for this array)

view sourceprint?1.struct object _struct objects[10];
And now you can perform the following calls.

view sourceprint?1.object[0].x = 42;
2.object[0].y = 78;
3.object[0].on_screen = true;
4.object[1].x = 342;
5.object[1].y = 123;
6.object[1].on_screen = true;
You can then just use a “for” loop which will loop to 10 and assign random values to the x and y co-ordinates and make them move around easily as below.

view sourceprint?01.// Assign random x and y co-ordinates
02.int x;
03.for (x = 0; x < 50; x++) {
04.
object.x[x] = rand() % 640 + 1;
05.
object.y[x] = rand() % 480 + 1;
06.
object.on_screen[x] = true;
07.}
08.

09.// Make them move around randomly and disappear once they are off the screen
10.int on_screen_counter = 10;
11.

12.while (on_screen_counter >= 1) {
13.
for (i = 0; i < 10; i++) {
14.
if (object[i].on_screen == true) {
15.
// Random true or false
16.
if ((rand() % 1) == 1) {
17.
object[i].x += rand() % 3 + 1;
18.
object[i].y += rand() % 3 + 1;
19.
}
20.
else {
21.
object[i].x -= rand() % 5 + 1;
22.
object[i].y -= rand() % 5 + 1;
23.
}
24.

25.
// Outside the screen
26.
if (object[i].y > 640 || object[i].x < 0 || object[i].y > 480 || object[i].y< 0) {
27.
object[i].on_screen = false;
28.
on_screen_counter--;
29.
}
30.

31.
// Print your image on the screen here E.g:
32.
// DrawImg (object[i].x, object[i].y, test_img);
33.
}
34.
}
35.}
That wraps up this quick tutorial on using structures in your code. You are also able to have structures inside structures too. See you next time.

http://www.codemii.com/2009/03/15/tu...11-structures/