Last Modified: 8/22/2022
This tutorial covers how to create sprites and move them around the screen. This tutorial will not go over the details of every command used. For information on how those commands and functions work, please refer to the Commands and Functions areas of the documentation.
Sprites are objects that you can move independantly from the background planes, and are typically used for player characters, enemies, items, and bosses. On the Sega Genesis/Mega Drive, sprites can be anywhere from 1 tile wide to 4 tiles wide, and the same applies to the height. This means the minimum size of a single sprite can be 8 pixels by 8 pixels, and the maximum size is 32 pixels by 32 pixels. If your object/character exceeds those size limits, you'll need to use additional sprites to account for the extra pixels required.
Before we get into any code, we'll talk briefly on how to create the artwork and get it into the proper format so you can use your own sprites.
You can use any modern graphic editor to make your sprites (and other art assets for your game), though using tools designed specifically for these purposes do help in keeping everything within the hardwares limitations. We recommend using PXL*, but you can use whatever tool you feel comfortable with. When you go to export, you'll need to export in the Y direction, otherwise when you load your tile data into your game, the tiles will out of order. If your character/object is larger than 32x32, you'll want to break the image up into blocks of 32x32 pixels or smaller so that you can load each section with the proper tile ordering. You'll also need to export the palette as well.
* The current version of PXL that's available during the writing of this tutorial does have some bugs and quirks. A new version is scheduled for release soon.
Okay, so now that you've exported your data, we need to add it to your project and load the tiles into video memory (VRAM), and load the palette into color memory (CRAM). We'll need to use line labels to mark the graphic and palette data so we know how to reference it in the code.
Alternatively, you can use Data statements for tile data and DataLong for palette data. It doesn't matter how you choose to do it - either external files, or by code, the method is still the same (just replace the DataFile line with whatever your editor of choice output the data as).
Now we need to load both of the data into memory (we'll also assume that your sprite is 32x32 pixels, or 16 tiles in total):
The code above is loading the 16 tiles located at "MySprite" into VRAM, starting at tile number 300. The next line is loading the 16 color palette into CRAM, at palette index 0 and color index 0.
Okay, so what do we do now? We create a sprite handler, which is just an Integer variable. We use 2 commands; one to create the sprite, and the other to set the tile and palette data:
sprite = AddSprite(4,4)
The first command is pretty self explanatory - it just sets the height and width of the sprite. The second line sets the VRAM location and palette index.
Now where's the sprite? You might have compiled already, excited to see your sprite on the screen, but feel slightly disappointed that it isn't there. Sprites are hidden off screen until their X & Y coordinates are 128. This means that your sprite will be offscreen until it, or some part of it, meets or exceeds pixel location 128,128.
Great! Now how do I control my sprite?
Before we get into that, let's talk about some best practices. There's a few commands to retrieve a sprites X and Y coordinates, but those commands are slow. They work, but there's a faster, and easier way to keep track of the coordinates, and that's with variables. When using the SpritePosX() and SpritePosY() functions, you have to store the result in a variable anyway. Why retrieve coordinates when you can have them already stored in a variable? With that, let's take a look at the full code:
Dim sprite As Integer
Dim sprite_x As Integer
Dim sprite_y As Integer
Dim jpad As Integer
sprite = AddSprite(4,4)
sprite_x = 128
sprite_y = 128
jpad = JoyPad(0)
And that concludes this tutorial. If you have any questions or comments, please join our Discord server.