Welcome back, young spinny fish.
It is time I finish this tutorial series, so this one will be a lot longer than the other two. So let's get to it.
Sprite Collisions!
Learning how to detect when sprites collide is important for creating software. So here's how you do that!
SPCOL
So, to begin we'll need the
SPCOL command, and all it does is tell SB to give you collision data for that specific sprite. You need to use this to use the other collision commands, so it's essential! To use it go into EDIT mode and type this:
ACLS
SPSET 0,17
SPCOL 0,TRUE
Now I'll break down the code.
SPCOL: The zero is the management number, TRUE means you want the collision area to grow or shrink depending on SPSCALE.
Here's a visual of how the collision works. 1 is one sprite and 2 is another.
|-----|
1 | 2 |
|_____|
The square is the collision area of 2 and it would be bigger or smaller if you used SPSCALE and said TRUE in SPCOL. I hope I explained that well, because we're moving on!
SPHITSP
Pros reading this are probably wondering, "Why didn't you include SPCOLVEC?" and the reason is that it's optional and this is a tutorial for people who don't know much about the commands. Also because I don't see the use of it. But I digress, SPHITSP is used to see if a certain sprite that has been "activated" using SPCOL is touching another activated sprite.
Here's how you use it:
ACLS
SPSET 0,17
SPCOL 0,TRUE
WHILE 1
SPRITEBEINGHITVARIABLE=SPHITSP(0)
WEND
SPRITEBEINGHITVARIABLE can be replaced by any variable, but the zero is the sprite that is touching something, or will be. SPRITEBEINGHITVARIABLE will be equal to the management number of the sprite it's touching. Pretty simple right? So you can have a sprite that you control, and if the hit variable is equal to a spriite that's an enemy, then your health would go down. And any other uses you can imagine! (that are also possible.)
Linking and Unlinking Sprites
Linking sprites is useful for some situations where you need two sprites to stick to one another, so here's how!
SPLINK
SPLINK is used to attach two sprites together, maybe two lovebird fish? yes. Let's do that.
ACLS
SPSET 0,17
SPSET 1,17
SPCOLOR 1,#MAGENTA
SPOFS 1,32,0
SPLINK 0,1
SPOFS 0,100,100
Notice how they both move even though only the blue one was told to. They're inseparable! And also linked together... Let's tear them apart in the name of coding! But not before explaining SPLINK and SPCOLOR. SPLINK is pretty simple, it links one sprite (0), to another (1). That's it! SPCOLOR turns a sprite(1) to a specified color (#MAGENTA), although you can use RGB to get a VERY specific color.
SPUNLINK
SPUNLINK separates two linked sprites so they are independent of each other again. Just add these two commands to the end of the last group of commands.
SPUNLINK 0
SPOFS 0,0,0
Now they're separated! You can reattach them if you want.
SPRITE ANIMATION
to be continued... (BTW you need a basic understanding of arrays to get this, so study up while I find a decent way to explain this.)