Private Sub TimerAnimation_Timer()
Static X As Long
Static Y As Long
''''Clear the form, since we do not have a background
Me.Cls
''''Draw the mask
BitBlt Me.hDC, X, Y, SpriteWidth, SpriteHeight, picMask.hDC, _
(FrameNumber - 1) * SpriteWidth, 0, vbSrcAnd
''''Draw the sprite
BitBlt Me.hDC, X, Y, SpriteWidth, SpriteHeight, picSprite.hDC, _
(FrameNumber - 1) * SpriteWidth, 0, vbSrcPaint
''''Check to see if we need to update th frame
If CurrentTick - LastTick > FrameTime Then
FrameNumber = (FrameNumber Mod MaxFrames) 1
LastTick = GetTickCount()End If
''''Update drawing positions
X = (X Mod Me.ScaleWidth) 1
Y = (Y Mod Me.ScaleHeight) 1
''''Force an update of the form
Me.Refresh
End Sub
If you run the sample project now, you can observe that the sprite is moved a small distance before the frame of the sprite is updated.
This is just one way to control the frame rate of a given sprite animation. You could also have used the traveled distance of the sprite to change the frame, or more commonly a user action could trigger a frame change.
We may want to use a blinking lamp as part of the demo project since we use the example and the graphical representation may be very helpful. We can also encourage them to change the value of FrameRate or add a slider control so that they can see how they can control the blinking-Burt.
StretchBlt
There is also another way of animating a sprite, which does not require extra bitmaps, but simply changes the drawn sprite directly. The function for this is the StretchBlt function, which, as the name implies, can stretch or shrink a sprite.
The StretchBlt function is very similar to the BltBit function. Both require a source and destination DC, and they both do raster operations. The difference is that StretchBlt will stretch or shrink the size of the source rectangle to fit the size of the destination rectangle. The declaration is as follows:
Declare Function StretchBlt Lib "gdi32" (ByVal hdc As Long, _
ByVal x As Long, ByVal y As Long, _
ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As Long, ByVal xSrc As Long, _
ByVal ySrc As Long, ByVal nSrcWidth As Long, _
ByVal nSrcHeight As Long, ByVal dwRop As Long _
) As Long
With StretchBlt you can perform some tricks, that might otherwise require a new bitmap.
The Sample project STRETCHBLT found in STRETCHBLT.ZIP moves a sprite around the screen, stretching and shrinking it as it goes.
We have two picture boxes in the sample project, serving as storage for the sprites. The idea of the program is quite simple, stretch the sprite to a size of 96 pixels and then shrink it to 32 pixels while the sprite moves across the form.
Private Sub TimerStretch_Timer()
Static X As Long
Static Y As Long''''Clear the form, since we have no background
Me.ClsIf Shrinking Then
Stretch = Stretch - 2
Else
Stretch = Stretch 2文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




