Page 1
Page 2
Page 3
Flying DevBuzzards are cool, but it
would be even cooler if our ship could move. To do this,
we can handle the InputKeyPressed and InputKeyReleased
events:
Private Sub Game1_InputKeyPressed(ByVal
lKeyCode As Long)
Dim iDir As Integer
iDir = Game1.InputKeyDirection
If iDir = 0 Then
Game1.SpriteSetDirection SPRITE_SHIP, 0, 2
Else
If iDir = 4 Then
Game1.SpriteSetDirection SPRITE_SHIP, 180, 2
End If
End If
End Sub
Private Sub Game1_InputKeyReleased(ByVal
lKeyCode As Long)
Game1.SpriteSetDirection SPRITE_SHIP, 0, 0
End Sub
There's a lot going on in these two
Input events. We used the InputKeyDirection event as a quick
way to determine what keys the user is pressing. InputKeyDirection
will return a value from 0 to 7 - 0 if the right key is
pressed; 1 if the right and down keys are pressed; 2 if
the down key is pressed, and so on. The numbers increase
clockwise starting with 0 meaning right.
We used SpriteSetDirection to
set the ship moving at an angle - 0 degrees is directly
right and 180 degrees is directly left, at a velocity of
2 pixels per game frame. Below is a diagram depicting the
iAngle parameter sent to SpriteSetDirection; you'll notice
any ASpriteCE method that deals with directions or angles
has zero pointing directly right, then increasing clockwise
from there.

This diagram shows how the iAngle parameter of SpriteSetDirection
is used
We're getting close now - we just need
to let the ship fire and detect the collisions with the
bullet sprite. We'll add this code to the end of the InputKeyPressed
event to send the bullet on it's way. The one thing to note
here is the use of SpriteGetPositionEx to determine where
to place the bullet in relation to the ship. SpriteGetPositionEx
will give you any of 8 offset positions in relation to a
sprite - this just makes computing relative positions easier.
' Bullet Fire
If Game1.InputKeyA And _
Not Game1.SpriteActive(SPRITE_BULLET) Then
Dim iX As Integer, iY As Integer
Game1.SpriteGetPositionEx SPRITE_SHIP, 6, iX, iY
Game1.SpriteSetPosition SPRITE_BULLET, iX, iY
Game1.SpriteSetDirection SPRITE_BULLET, 270, 4
Game1.SpriteActive(SPRITE_BULLET) = True
End If
And now for the collisions between the
bullet and the buzzards, as well as the collisions between
the bullet and the top of the screen. We use the SpriteCollision
and SpriteCollisionEdge events to trap these. In
either case, we want to inactivate the sprites that collided.
Private Sub Game1_SpriteCollision( _
ByVal lSpriteNum1 As Long, _
ByVal lSpriteNum2 As Long, _
ByVal iCollisionX As Long, _
ByVal iCollisionY As Long)
If (lSpriteNum1 >= SPRITE_BUZZARD_1 And _
lSpriteNum1 < SPRITE_BUZZARD_1 + NUM_BUZZARDS And _
lSpriteNum2 = SPRITE_BULLET) Then
' bullet collided with buzzard
Game1.SpriteActive(lSpriteNum1) = False
Game1.SpriteActive(lSpriteNum2) = False
End If
End Sub
Private Sub Game1_SpriteCollisionEdge(
_
ByVal lSpriteNum As Long, _
ByVal iEdges As Long)
If lSpriteNum = SPRITE_BULLET Then
Game1.SpriteActive(SPRITE_BULLET) = False
End If
End Sub
Once again, let's see our work - if
you run Project1.vb now, you should be able to shoot
down those nasty buzzards. Now if we only had a couple of
sounds...
We'll add two sounds - one for the bullet
firing (bullet.wav) and another for a dead buzzard
(deadbuzzard.wav). Loading wave files is done with
the SoundLoad method, and they're played with the SoundPlay
Method. We'll need to add a couple more constants to the
top of Form1's code:
Const SOUND_DEADBUZZARD = 0
Const SOUND_BULLET = 1
Then, add the loading of the sounds
in the Form1_Load event:
' Load Sounds
Game1.SoundLoad SOUND_DEADBUZZARD, _
App.Path & "\deadbuzzard.wav"
Game1.SoundLoad SOUND_BULLET, _
App.Path & "\bullet.wav"
And, finally we play the sounds in the
InputKeyPressed and SpriteCollision events:
' Add this to the Game1_InputKeyPressed
event
' after If Game1.InputKeyA And ...
Game1.SoundPlay SOUND_BULLET
' Add this to the Game1_SpriteCollision
event:
' after If (lSpriteNum1 >= ...
Game1.SoundPlay SOUND_DEADBUZZARD
Well, our DevBuzzards game isn't exactly
polished, but you get the idea where it's going. We obviously
would want our buzzards to move in some way; right now they're
sitting ducks. And right now our ship can be moved right
off the screen! Plus we need scoring, levels, shields...
Hold on! This is just an introductory article for the ASpriteCE
control. We'll call it quits here, but for more ideas be
sure to check out the samples that ship with ASpriteCE -
they demonstrate oodles of functionality including scrolling
and tiling. Happy Coding!
Previous Page