How to create scrolling text in GameMaker

GameMaker doesn't have a built-in way to scroll text marquee-style, but here's a straightforward way to produce a scrolling text effect.

CREATE EVENT

First, the Create event defines a handful of instance variables to control the scroll speed and length of the message window.

I used a custom Atari 8-bit style sprite-based font, but this technique should work with any font. (Note: creating a custom font is relatively easy and there are many how-to tutorials available on the web.)

The message string is prepended and appended with a blank string the length of the message window so the scrolling text cleanly enters and exits the message window while looping.

The final score of the previous game is inserted into the message string by replacing the placeholder score with the value of the built-in score variable.

// current starting character pointer
pointer = 0;
// number of characters to display
window_len = 10;
// pause between before next scroll 
scroll_spd = 5;
// boolean flag to control next scroll
next_scroll = true;
// create custom font from sprite
atari_font = font_add_sprite(spr_atari_font, ord(" "), 0, -1);

// define message to be scrolled
// concatenated for editing ease
// prepend and append a blank string
message = string_repeat(" ", window_len);
message += "ROTO - WRENCH HTML5  ";
message += "FEATURING ROTO - WARP  ";
message += "BY HEAVY KETTLE 2014  ";
message += "FINAL SCORE 0000";
message += string_repeat(" ", window_len);

// replace placeholder score string with final score of last game
message = string_replace(message, "0000", string(score));
// message string length for use in draw event
msg_len = string_length(message);

STEP EVENT

The Step event controls which portion of the message string to currently display. When the pointer exceeds the length of the message string and window length, the pointer variable is reset to zero and the scrolling starts over.

The function string_copy parses the message string at the current pointer position to the length of the window_len variable.

The scroll speed boolean variable and Alarm event (below) are used to control the next scroll interval.

If your scrolling text appears on a title page (like mine does), you can check for a left mouse click to proceed to the next room.

if (next_scroll) {
  pointer++;
  // increment pointer until end of message
  if (pointer + window_len > msg_len) {
    // reset the pointer to the beginning
    pointer = 0;
  }

  // create the sub string for message window
  output = string_copy(message, pointer, window_len);

  next_scroll = false;
  alarm[0] = scroll_spd;
}

if (mouse_check_button_pressed(mb_left)) {
  // goto main screen and start game
  room_goto(rm_main);
}

DRAW EVENT

The Draw event outputs the parsed portion of the message string.

// set the custom font
draw_set_font(atari_font);
// set the text justification
draw_set_halign(fa_left);
// set color with custom constant
draw_set_color(COLOR_BLUE);
// draw the text at the coordinates
draw_text(240, 312, output);

ALARM 0 EVENT

Lastly, Alarm 0 contains a boolean variable is used to handle the scroll interval.

next_scroll = true;

Check out Roto-Wrench HTML5 to see the scrolling text effect in action.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.