Master Game Community

Would you like to react to this message? Create an account in a few clicks or log in to continue.
Master Game Community

Master Game Productions (MGP) presents Master Game Community (MGC). A forum for all your Game Maker needs.


    One Dimensional Array

    Anybloodyid
    Anybloodyid
    Member


    Posts : 10
    Reputation : 0
    Join date : 2011-03-21
    Location : England

    One Dimensional Array Empty One Dimensional Array

    Post by Anybloodyid Mon Mar 21, 2011 2:33 pm

    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


    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.

    One Dimensional Array Buttonimages

    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
    Add another Event, mouse left pressed and copy and paste this
    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]))
    Open the room and place the obj_control any where.
    That's it! run the program and see how it works

    Now for some explanation of what's going on.
    Code:
    image_speed=0
    This in the Create Event of obj_button and stops the sub_images from spinning around. Take it out and see what happens.
    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;
        }
    }
    var i just initialises the variable so that the program knows it's there.
    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

      Similar topics

      -

      Current date/time is Thu Mar 28, 2024 10:00 am