Visual Basic动画编程技术[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 minkerui 的 blog

image1.picture = loadpicture(app.path & "ird" & imageno% & ".bmp")

‘移动image对象
image1.move image1.left + detax%, image1.top + detay%

‘碰到边界,则校正位置,并反向
if image1.left + image1.width >= form1.width then ‘碰到右边界
image1.move form1.width - image1.width, image1.top
detax% = -detax%
elseif image1.top + image1.height >= form1.height then ‘碰到上边界
image1.move image1.left, form1.height - image1.height
detay% = -detay%
elseif image1.left <= 0 then ‘碰到左边界
image1.move 0, image1.top
detax% = -detax%
elseif image1.top <= 0 then ‘碰到下边界
image1.move image1.left, 0
detay% = -detay%
end if
end sub

五、 缩放动画
气球的膨胀或缩小是缩放动画的典型例子。在timer事件过程中修改image对象的width和/或height属性,便可实现缩放动画。但如果要表现物体的同心缩放,则还应同时还需移动image对象。下面是模拟气球在空中缩放(同心)的例子。程序启动时,在窗体中显示一只气球,用鼠标左键点击它,则气球将开始膨胀,如碰到窗体的边界,气球将缩小,缩小到原来大小时,又将膨胀;再用鼠标左键点击气球,则气球将停止缩
放。位图文件balloon.bmp存放在当前工程所在的目录中。在需显示动画的窗体(form1)中设置image对象image1和timer对象timer1,并按下表设置它们的属性,未列出的属性使用缺省值。
对象
属性
设置值
image1
picture
...(工程文件所处路径)balloon.bmp

stretch
true
timer1
enabled
false

interval
150
程序代码如下:
option explicit `变量必须先定义,才能使用
dim isplaying as boolean `动画是否启动
dim width0%, height0% `气球原大小
dim detax%, detay% `沿x、y轴的缩放增量

private sub form_load()
isplaying = false
image1.tooltiptext = "开始"
width0% = image1.width ‘保存气球的初始大小
height0% = image1.height
detax% = 100
detay% = 100
end sub

private sub image1_click()
if isplaying then
isplaying = false
timer1.enabled = isplaying
image1.tooltiptext = "开始"
else
isplaying = true
timer1.enabled = isplaying
image1.tooltiptext = "停止"
end if
end sub

private sub timer1_timer() `timer事件
`移动image对象,保持同心缩放
image1.move image1.left - detax% / 2, image1.top - detay% / 2
‘按增量缩放
image1.width = image1.width + detax%
image1.height = image1.height + detay%

`碰到边界,则缩小
if image1.left + image1.width >= form1.width or _
image1.top + image1.height >= form1.height or _
image1.left <= 0 or image1.top <= 0 then
detax% = -detax%
detay% = -detay%
end if

`缩到原大,则膨胀
if image1.width <= width0% or image1.height <= height0% then
detax% = -detax%
detay% = -detay%
end if

本文关键:动画编程
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top