Time for action – updating the Sprite
Add the
Update()
method to theSprite
class:Public Overridable Sub Update(gameTime as GameTime) Dim elapsed as Single elapsed = CSng(gameTime.ElapsedGameTime.TotalSeconds) timeForCurrentFrame += elapsed If (timeForCurrentFrame >= FrameTime) Then currentFrame = (currentFrame + 1) Mod (frames.Count) timeForCurrentFrame = 0.0 End If _location += (_velocity * elapsed) End Sub
What just happened?
When Update()
is called for the sprite, the standard timing mechanism, we have been using is implemented to determine when the frame should be updated. When it is, the frame is set to (currentFrame + 1) Mod (frames.Count)
. This is a short-hand method of saying, "Add 1 to the current frame, divide the total by the number of frames in the animation and return the remainder.".
As an example, consider an animation with five frames (numbered zero through four). When currentFrame
needs to go from zero to one, the assignment looks...