• 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!

GML How to make it so that only one of each sprite is visible at a time.

  • Thread starter MasterOfTheYeetz
  • Start date
M

MasterOfTheYeetz

Guest
How do I make it so that only one of each sprite is visible at a time.
my code

Code:
global.speedmodifier = 0.9;

alarm[0] = room_speed * 1;
GML:
randomize();
var count = irandom_range(1, 2);

var i = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
i.sprite_index = choose(Sprite5, Sprite6, Sprite7, Sprite8, Sprite9);
switch (i.sprite_index)
{
    case Sprite5:
    case Sprite6:
    case Sprite7:
    case Sprite8:
    case Sprite9:
        i.image_speed = 0;
        i.image_index = irandom_range(0, sprite_get_number(i.sprite_index) - 4)
        
        if (global.speedmodifier > 1)
        {
            if (count == 1)
            {
                var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
                j.sprite_index = choose(Sprite5, Sprite6, Sprite7, Sprite8, Sprite9);   
                j.image_speed = 0;
                j.image_index = 1
            }
        }
    break;
    
    default:
    i.image_speed = 1;
    i.y = choose(room_height - 85, room_height -145, room_height - 180);   
    
}

alarm[0] = room_speed * (irandom_range(2,4))
 

Nidoking

Member
Do you mean that you don't want i.sprite_index and j.sprite_index to be the same? Check to see if they're the same, and if they are, choose a different sprite. A better way would be to write a choose statement without the sprite that was already used, but that would be more complicated. A good thing to learn, but if you're not up to it, a loop will suffice.
 

Tony Brice

Member
If I'm understanding you right then you only want one of that object on screen at any time so don't want to create a new one unless the old one has gone?

You just need to modify the bit where you create the sprite to check if any instance of oObstacle already exists, as shown below.

GML:
if (!instance_exists(oObstacle) {
  var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
  j.sprite_index = choose(Sprite5, Sprite6, Sprite7, Sprite8, Sprite9);   
  j.image_speed = 0;
  j.image_index = 1;
}
 
M

MasterOfTheYeetz

Guest
oops i meant to have to have only one of each type of sprite on the screen (so there aren't two sprite 9's one after another.) I'm fine with multiple sprites on the screen at at time.
If I'm understanding you right then you only want one of that object on screen at any time so don't want to create a new one unless the old one has gone?

You just need to modify the bit where you create the sprite to check if any instance of oObstacle already exists, as shown below.

GML:
if (!instance_exists(oObstacle) {
  var j = instance_create_layer(room_width +100, room_height - 75, "Instances", oObstacle);
  j.sprite_index = choose(Sprite5, Sprite6, Sprite7, Sprite8, Sprite9); 
  j.image_speed = 0;
  j.image_index = 1;
}
 
A

Arconious

Guest
Remember, you can still access 'i' while you are setting up 'j'. A rudimentary way to achieve your goal is to compare both instances sprite_indexes to each other (after setting instance j's sprite_index) to see if they are the same, choosing again if they are. A 'while' loop is necessary here, in order to keep choosing in case they end up the same a second time.
 

TheouAegis

Member
when you have just 2 instances like this, just compare the sprites and rechoose. There is nothing wrong with that at all.

If you are going to have more than 2 (arguably 3 is fine as well), you may want to either maintain a shuffled list of options or utilize some other form of indexing.

The typical list method is to put all the sprites in a ds_list and shuffle it. Then you either assign the first sprite in the list and delete it (you can move it to the bottom of the list alternatively), or you can use a global index that keeps track of where in the list the next instance will grab its sprite.

Another alternative, although harder to implement, is to just assign the sprites incrementally, but then shuffle the locations of the instances.
 
Top