One Dimensional Array
Using one object to create a number of buttons with different images.
Compatibility: GM8
Tutorial Type: Walkthrough
Downloadable Content: N/A
Skill Level: Beginner
Objective: How to store items in an array
Category: How to store items in an array
Time to Complete: 15 minutes
Using one object to create a number of buttons with different images.
Compatibility: GM8
Tutorial Type: Walkthrough
Downloadable Content: N/A
Skill Level: Beginner
Objective: How to store items in an array
Category: How to store items in an array
Time to Complete: 15 minutes
This is my first walk through so I hope that it gives the beginner an insight in to arrays.
When I started to write code arrays were one of the things that I found hard to understand, so I thought I would write this walk through to maybe help those of you who are new to writing code.
First thing to do is start Game Maker !
Then create a room no need to change anything the room as it is will do fine.
Create a sprite with 10 sub-images, click center fo the origin, name it spr_button, you can call it anything but with spr as a prefix it helps if you need to ask others for help.
Here's the button I used and it's sub images.
Now create an object, call it obj_button and use the spr_button for the sprite.
Create another object and call it obj_control, no sprite.
Add a Create Event to the object obj_control and then click on the control tab on the right and drag the create code to the actions window.
Whn the editor opens copy and paste the following.
- Code:
var i;
for (i=0; i<=9; i+=1)
{
with(instance_create(100 + 48*(i), 200, obj_button))
{
image_index = i;
global.buttonID[i] = image_index;
global.imagevalue[i]=(i+1)*10;
}
}
Click on the tick to save the changes.
Click on obj_button then add a Create Event and drag the create code to the actions window again and copy and paste the following.
- Code:
image_speed=0
- Code:
show_message("You pressed the button with an image_index of " + string(global.buttonID[image_index])+"#Which shows the sub_image " + string(global.imagevalue[image_index]))
That's it! run the program and see how it works
Now for some explanation of what's going on.
- Code:
image_speed=0
In the Mouse Pressed Event we just have a Show message so that we can see which button we clicked on.
That's the easy out of the way now for an explanation of the array.
- Code:
var i;
for (i=0; i<=9; i+=1)
{
with(instance_create(100 + 48*(i), 200, obj_button))
{
image_index = i;
global.buttonID[i] = image_index;
global.imagevalue[i]=(i+1)*10;
}
}
for (i=0; i<=9; i+=1)
This is a for loop that goes around 10 times
with(instance_create(100 + 48*(i), 200, obj_button))
This is just saying with each instance of the button created do the following.
image_index = i;
Give each buttons image_index it's own number
global.buttonID[i] = image_index;
This is the first array, global.button[i] inside the square brackets is where each item of the array is stored, we're storing the number of each image_index
global.imagevalue[i]=(i+1)*10;
This next array stores the sub_image number, I used numbers 10, 20, 30 etc
So the first time the for loop goes around it stores
( i+1)*10 0+1 = 1 1*10 = 10
Next time it stores
( i+1)*10 1+1 = 2 2*10 = 20
Next time it stores
( i+1)*10 2+1 = 3 3*10 = 30