GameMaker: Studio

GameMaker: Studio

View Stats:
Pujii Apr 27, 2013 @ 9:37pm
quick array help
When a certain button is clicked, i want the player object and enemy object to change into different random sprites based on the same 2D array.

array[0,0] = spr_1
array[0,1] = spr_2
array[0,2] = spr_3
array[1,0] = spr_4
array[1,1] = spr_5
array[1,2] = spr_6

when button is clicked:

rand_1 = irandom(3)
rand_2 = irandom(3)
rand_3 = irandom(3)
rand_4 = irandom(3)
obj_player.sprite_index = array[rand_1, rand_2]
obj_enemy.sprite_index = array[rand_3,rand_4]

So where should each code part go? Anything missing?

I've tried controller objects, scripts, and fiddling with the event targets but can't seem to get it to work (usually an error pops up, if not then the sprite doesn't change). I'm just learning as I go by using the search function in the manual to find ways to accomplish what I want but it's not the best of help in some areas. Thanks!
< >
Showing 1-4 of 4 comments
wer2chosen Apr 28, 2013 @ 4:56am 
I was tring something similiar and had problems with an unrelated issue. It then hit me that each object already has a collection which is what an array is. You can get the number of sprites in each object, if they are not equal use that number as the one to generate the random number from. You can just change the sprit index.

I had created this complex hit point system for each of my many, many objects. I wanted when a ship took damage to change to a different color image. I just added a duplicate image to handle 0, since I want the ship to blow up at zero armor or hitpoints. So the image index became my array.

This checks to see if in a colision the defender should be destroyed or destroy the attackers. Attackers just have one armor point.

if(round(self.image_index) < 1)
with (other)
// int_attackDstry = int_attackDstry + 1 ;


my defenders have different starting points of armor based on where that are created. If created in a red zon;

ID = instance_create(mouse_x,mouse_y,obj_Defender) with ID image_index = 4 ;
in a green zone the starting index would be 2

Iy seems that if we are creating arrays to just use as holders for other array info we may as well just use the objects array.My thought, may not work for you.
Pujii Apr 29, 2013 @ 9:10pm 
I sorta understand what you're saying but it sounds like you're just working with sprites that have unique images for each sub_image. I'm working with various animated sprites so I don't think you're method works. Thanks for the thought though.
H.A.X. Apr 30, 2013 @ 9:12am 
Put the declaration of the array in the create event of an unique controller object or the button itself and the rest of the code in the left mouse released event of the button. You may get an error because irandom(3) can result in 0, 1, 2 and 3 and your array doesn't have a third section or how it's called: array[0,3] is missing, array[1,3] etc.
Change it to irandom(2) and the sprites should change. If not then you should look at the rest of your code.
Pujii Apr 30, 2013 @ 11:30am 
I tried something similar before but it didn't work (program couldn't find the array or something) but now it works perfectly. Thanks for the help!
< >
Showing 1-4 of 4 comments
Per page: 15 30 50

Date Posted: Apr 27, 2013 @ 9:37pm
Posts: 4