irector 8从入门到精通

提高篇(三)

作 者 : 胡飞

ChinaByte网络学院

   现在需要判断激活的画图按钮。设置一个全局变量CurrectTool保存当前激活的按钮,可能取值为#none(没有按钮被激活),#chalk(粉笔按钮被激活),#rectangle(矩形按钮被激活),#circle(画圆按钮被激活),#pencil(铅笔按钮被激活)。在电影脚本中写自定义事件如下:

   on checkInactive

   global currenttool, mouseclickcounter

   if currenttool <> #none then

   case currenttool of

   #chalk: sprite(3).member = member("chalkUp")

   #rectangle: sprite(4).member = member("rectUp")

   #circle: sprite(5).member = member("circleup")

   #pencil: sprite(6).member = member("lineup")

   end case

   end if

   mouseclickcounter = 0

end

   然后在每个按钮的MouseDown事件中检测以上自定义事件。以“ChalkUp”按钮为例,写脚本如下:

on mouseDown me

   global currenttool, mouseclickcounter

   mouseclickcounter = 0

   checkInactive

   sprite(me.spriteNum).member = member("chalkdown")

   currenttool = #chalk

end

   选择了画图工具,现在开始画图。以矩形画图为例,写自定义动作脚本如下:

on drawwithrectangle

   global mouseclickcounter, drawcolor

   global beginpointX, beginpointY

   --注意到全局变量MouseClickCounter,由于矩形由左上角和右下角坐标决定,

   --因而必须鼠标连击两次才能画出一个矩形。

   --首先得到左上角坐标,再得到右下角坐标

   case mouseclickcounter of

   0: beginpointX = the mouseH - 49

   beginpointY = the mouseV - 66

   1: endpointX = the mouseH - 49

   endpointY = the mouseV - 66

   member(2).image.draw(beginpointX, beginpointY, endpointX, endpointY ,[#shapetype: #rect,#linesize: 2,#color: drawcolor])

   end case

end

   因为在黑板上作图,所以在黑板的MouseDown事件中写如下脚本:

on mouseDown me

   global currenttool, mouseclickcounter

   --如果鼠标已经连击了两次,就将全局变量MouseClickCounter清零

   if mouseclickcounter = 2 then

   mouseclickcounter = 0

   end if

--根据按钮的激活状态来决定画什么图形

   case currenttool of

   #rectangle: drawwithrectangle

   #circle: drawwithcircle

   #pencil : drawwithpencil

   end case

   --鼠标点击次数加一

   mouseclickcounter = mouseclickcounter + 1

end

  

   本例的画图思路基本就是这样,至于其他细节问题比如画图时光标的变化、画图的连续性,以及其他扩充的画图功能,请有兴趣的朋友自己实现。如果你根据系统提供的实例能独立实现画图功能,那么你对事件触发机制和Lingo脚本的掌握已经有很好的基础了。

   图六:画图的实现

   通常在编写脚本时会遇到很多意想不到的问题。比如在用PuppetSprite将精灵木偶化以后,如果不及时取消木偶化,在其他地方就会出现出乎预料的结果;在设置时间等待时,如果用delay()或自定义Wait函数,那么在等待时可能就不能同步播放声音了,解决的办法就是自定义Wait函数,在等待时判断声音通道是否空闲,如果空闲则继续播放声音。总之,熟能生巧,多思考多实践,你会成为高手的

关于栏目合作: 由于宇风多媒体的影响力不断扩大,有较大的访问量,并与众多的媒体建立合作关系,为了更充分利用各类资源,欢迎成为本站的专栏编辑,或建立合作关系。如需要开设其它栏目,也可提出Webmaster@yufeng21.com