• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Help in coding the code given below!

P

prithvidiamond

Guest
Hello dear readers and forum viewers,

I have a problem with the below code and need help,

globalvar fruit;

randomize();

fruit = irandom_range(0,3);

if fruit = 0
{
draw_self();
draw_sprite(spr_fr_wm,0,320,150);
}


if fruit = 1
{
draw_self();
draw_sprite(spr_fr_ap,0,320,150);
}

if fruit = 2
{
draw_self();
draw_sprite(spr_fr_sb,0,320,150);
}

if fruit = 3
{
draw_self();
draw_sprite(spr_fr_rs,0,320,150);
}

basically the problem is that the sprite keeps changing rapidly, I need ONLY ONE RANDOM sprite to show up until further instructions which I am unable to code.

Please Help any assistance will be of great help!

Thank you,

prithvidiamond
 
J

joakimFF

Guest
I assume all this code is in the draw event? first of you only need to call randomize(); once at the start of the game. secondly call fruit = irandom_range(0,3); in another event that is only executed when you want to.
 
@

@Alex@

Guest
It appears that you initialize the random variable over and over. This is because the draw event is executed multiple times per second. You need to initialize when you want the sprite to be set, either the create event , a button press or whenever it needs declared.

You should look into the switch statement as that'll shorten the code and make it easier to expand.
 
P

prithvidiamond

Guest
Thank you all so much I will try I all your suggestions. You people are a wonderful community! :)
 
P

prithvidiamond

Guest
I assume all this code is in the draw event? first of you only need to call randomize(); once at the start of the game. secondly call fruit = irandom_range(0,3); in another event that is only executed when you want to.
Nice game you have made!
 
P

prithvidiamond

Guest
Thank you all, you are all a wonderful community! :)
it worked, thanks to all !
 
Last edited by a moderator:
D

Drift

Guest
Instead of this, if you are using sprites with just 1 image, you could use this:

draw_sprite(spr_fr_wm,fruit,320,150);

Otherwise, use an array to store each sprite

*There's no need to use those draw_self()
 
Top