Making intros

From Spheriki

Jump to: navigation, search

This is how to make an intro for your game. Some text, and maybe a bit of music to spruce things up.


Contents

Showing text

We're going to draw an image and put some text on it. A simple example is:

function Intro()
{
  var font = GetSystemFont();
  font.drawTextBox(
      0, 0,
      GetScreenWidth(), GetScreenHeight(),
      0,
      "lots of text here..." );
  FlipScreen();  // show the text
  
  var start = GetTime();
  while(start + 5000 > GetTime());  // like Delay(5000)
}

But if you have lots of text to show, that example is rubbish.


Scrolling text up

So here is a much better Intro function that you might actually learn something from:

function Intro()
{
  var font = GetSystemFont();
  var txt = "Once upon a time in a land far away..." +
      "There was a guy called heh, heh, blah, good luck!";
  var background = LoadImage("intro.jpg");
  
  var x = 0;
  var y = GetScreenHeight() + 10;  // start at the bottom of the screen
  var h = 50;  // the height of the text box
  var w = GetScreenWidth();
  
  while (y > -h)  // force the txt to go up the screen
  {
    background.blit(0, 0);
    font.drawTextBox(x, y, w, h, 0, txt);
    
    FlipScreen();
    
    y -= 1;
    
    var start = GetTime();  // an inline delay function
    while(start + 75 > GetTime());  // like Delay(75)
  }
}

Yeah, now you have an Intro function, that you can call upon by using:

Intro();


How the scrolling works

The intro function essentially works by decreasing the y value of the call to font.drawTextBox(x, y, w, h, offset, txt). By decreasing this y value in a loop, the text vertically rises.

Now, the while (y > -h) means "while y is greater than minus h". So while y is greater than -50 in this case.

Also, it blits a background image at (0, 0) each time. This image could be a picture of some stars, to fully achieve a Star Wars-type intro.

Once the txt is drawn, it calls FlipScreen(), which copies the drawing that we've done from the backbuffer to the screen.


Adding music

If we wanted to add some music to this intro we could do so like so:

var song = LoadSound("blah.mp3");
song.play(true);  // repeat the song forever
Intro();  // call upon the intro
song.stop();

This isn't actually changing the Intro function at all. It's just wrapping music around it.


By Flikky (from the Sphere CHM, adapted and updated for the wiki.)

Personal tools