Drawn Squares Constantly Flicker Between Assigned Colors

92 views Asked by At

I am very new to GameMaker 1.4, but I have some experience with Python and C++. I have created a matrix of random ints between 1 and 5 inclusive to represent the number of stories each building on a map has. I want to use this to draw a 10x10 set of squares with lighter grey squares representing taller buildings. Unfortunately, I can't get the squares to stay the same color. They constantly flicker between colors.

How can I get them to stop flickering and represent their relevant values appropriately?

Here is my script:

// Random 2D Map Height Generator

/*
Generates a matrix which is used for determining 
 the amount of stories per building in the game.

 Then draws squares of different colors to represent
 the number of stories of each building.
*/

ct = irandom(1);
// create array
for (i = 0; i <= global.height; i += 1) {
   for (j = 0; j <= global.width; j += 1) {
      mapArray[i, j] = irandom_range(1, 5);
      if (ct % 2 != 0 && mapArray[i, j] < 5) {
      mapArray[i, j] += 1;
   }
   ct += 1;
   }
}

// make colors
one = make_color_rgb(0, 0, 0);
two = make_color_rgb(51, 51, 51);
three = make_color_rgb(102, 102, 102);
four = make_color_rgb(153, 153, 153);
five = make_color_rgb(204, 204, 204);

// initialize coordinates
ex = 300;
wy = 100;

// count columns

// draw map
for (i = 0; i <= global.height; i += 1) {
   for (j = 0; j <= global.width; j += 1) {
      ex2 = ex + 30;
      wy2 = wy + 30;
      switch (mapArray[i, j])
      {
         case 1:
            draw_set_color(one);
            break;
         case 2:
            draw_set_color(two);
            break;
         case 3:
            draw_set_color(three);
            break;
         case 4:
            draw_set_color(four);
            break;
         case 5:
            draw_set_color(five);
            break;
         default:
            draw_text(200, 200, 
            "ERROR: scr_mapGeneration, case not met");
            break;
      }
      draw_rectangle(ex, wy, ex2, wy2, false);
      ex += 33;
      if (j == global.width) {
         wy += 33;
         ex = 300;
      }
   }
}

I call it in an object with the draw event and execute this code with it:

script_execute(scr_mapGeneration);

I call randomize(); in my initial room's creation code. The problem occurs in another room.

Here is a picture of what is produced, obviously this picture doesn't have flickering squares. enter image description here

Please let me know if you need any more information.

2

There are 2 answers

0
Aaron H On BEST ANSWER

The colors are constantly new assigned with new random values, since the draw funtion is called every frame.

randomize() does have nothing to do with your problem, random always creates a new random, it just changes the random seed. ;)

You should call the script in the create event once, and instead of drawing the rectangles you store them in e.g. a ds_grid (more info here) In the draw funtion you just draw the saved values from the grid.

0
LuminousNutria On

The problem was that I had included all of the text as part of a draw event. So the array was constantly remade. The solution is to separate the array-making code and execute it under a different event. I used the creation event for this.

I would provide a detailed example, but it's difficult given the nature of the GameMaker program, and the solution should be self-explanatory anyway.