?? 79(1).txt
字號:
用Visual Basic的Move方法實現動畫效果
哈爾濱 姜軍
Visual Basic和結構化程序相比,增加了對象的“方法”功能。充分掌握這種不同于對象屬性的“方法”,對可視化應用的開發極為重要。下面,以VB4.0的move方法為例,說明該方法在動畫中的應用。
本例欲通過move方法完成一只蝴蝶的飛翔過程。通過定時器的中斷,每隔一定時間(本例為0.2秒),在屏幕上移動蝴蝶的位置,并且改變蝴蝶的形態(展翅和收翅),于是利用視覺的暫存效應,看到蝴蝶栩栩如生的飛行場面。蝴蝶的位置的移動就由move方法來實現,move方法使用的格式如下:
對象. move left, top
其中left為對象左邊界的水平坐標(x軸),top為對象上邊界的垂直坐標(Y軸),本例中的對象框(image box),名字為main。
蝴蝶的展翅和收翅圖象由位圖文件bfly1.bmp bfly2.bmp來提供,并可根據需要制作其它飛行的位圖文件,以使效果更加逼真。
本例中窗體結構和各個對象的屬性設置如下:
對象 屬性設置
窗體caption 蝴蝶飛行動畫設計
圖象框name main
picture bfly1
圖象框name openwings
picture bfly1
圖象框name closewings
picture bfly2
命令框name command1
caption E&xit
定時器name timer1
interval 200
有關程序比較簡單,代碼如下:
定時器中斷程序:
Private SubTimer-Timer()
Static PickBmp As Integer
Main.Move Main.Left +20,Main.Top -5
If PickBmp Then
Main.Picture =Open Wings.Picture’Displays the open butterfly picture.
Else
Main.Picture =Close Wings.Picture’Displays the closed butterfly picture.
End If
PickBmp=Not PickBmp’Toggle the value.
End Sub
退出按鈕(exit)程序:
Private Sub Command1-Click()
Unload Me
End
End Sub
這樣,利用VB的move方法和其它對象屬性,就可以實現更加復雜的動畫設計。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -