|
在国外,越来越多的DIRECTOR程序员倾向用LINGO中的对象概念来编写他们的程序。这主要是基于以下几点:1:提高代码重用率。2:减少代码的复杂度。
因为在DIRECTOR编程中经常会碰到这种情况:很多STAGE(舞台)上的SPRIRITE(精灵) 具有相同或相似的性质。比如编写一个射击不断下落的彩球的游戏,不同的彩球除了颜色和大小等属性不同外,具有相同的下落速度和消失方式。如果我们为每一个彩球编写 一套代码,则一百个小球就会有一百套相似的代码。这无疑会增加程序的复杂度。用LINGO中对象的概念可以只实现一个彩球的父脚本,由此派生出一百个具有不同属性的彩球。
LINGO并不是一种面向对象的语言(如C++),它原为一种脚本语言,在DIRECTOR4.0时才引 入了面向对象的概念。所以它有着先天的不足。这一点在对象的继承上表现最明显。但这 并不影响在LINGO中广泛使用对象。现在我就把传统C++的对象概念与LINGO中的对象概念作一个对比。
C++ LINGO 对象 父脚本 成员变量 属性 成员函数 句柄 实例或对象指针 对象ID号
其实LINGO中的对象要比想象的简单。首先要在脚本的Script cast Member Properties 中将要设为父脚本的脚本类型(type)设为Parent。
父脚本是为单独对象编写的通用脚本。它应具有一些属性和最少一个NEW句柄(注: DITECTOR4.0为BIRTH句柄)用以派生对象。以人为例。它的脚本应类似如下的句柄,
---Parent Script of Human----cast name 为parent:Human property Age property Gender property Height property Weight property Heartbeat .......。
on new me,pAge,pGender,pHeight,pWeight,pHeartbeat... --NEW句柄必需有 set Age = pAge set Gender = pGender set Height = pHeight set Heartbeat = pHeartbeat ......
return me ---此语句用以返回对象ID号 end
on heart me if (Heartbeat = 0 ) then alert "He or she is Dead") return end
对象中的句柄的第一个参数必为me.当调用父脚本生成对象时,每一次调用产生不同的对象ID号用以区分同一脚本派生的不同对象。具体调用如下: set HumanObject = new(script"parent:Human") 将对象IDH号存入HumanObject变量。如何调用对象句柄呢? 只要将包含ID号的对象句柄参数传给对象句柄就可以了。例如:heart(HumanObject)。
以上简单解释了LINGO中的对象用法。现在用它来实现一个滚动条对象。 很多DIRECTOR用户都知到DIRECTOR中的滚动条很难看,有点类似早期MACINTOSH的界面。 用它作出的图形很不和谐。用LINGO对象,你可以扩展成任何形式的滚动条,定做自己的滚动条和滚动块以及滚动面板。具体LINGO 原码如下:
我在这里做一些解释:滚动条对象包含以下属性: -- Scroller scriptproperty pnVisibleLines --滚动框中的可见行数 property pOnScreenField -- 视屏文字域的名字或脚色号property pchThumb -- 滑动块的通道号 property pchScrollBar -- 滑动杆的通道号 property pTheTextString -- 滚动文字 property pCurrentTopLine -- 当前最高行号 property pStartLineOfLastFullScreen -- 上一满屏的起始行号 property pThumbTop -- 滑动杆所能到的最高偏移值(pixel计算) property pThumbBottom -- 滑动杆所能到的最低偏移值(pixel计算) property pThumbRange -- 滑动杆所能移动范围(pixel计算) property pHeightPerLine -- 每一文字行的高度(pixel计算)
--滚动条对象包含以下句柄:
--用于初始化的NEW句柄 on new me, textString, targetField, visibleLines, chScrollBar, chThumb set pTheTextString = textString set pOnScreenField = targetField set pnVisibleLines = visibleLines set pchScrollBar = chScrollBar set pchThumb = chThumb set pThumbTop = the top of sprite pchScrollBar + (the height of sprite pchThumb / 2) set pThumbBottom = the bottom of sprite pchScrollBar - (the height of sprite pchThumb / 2) set pThumbRange = pThumbBottom - pThumbTop set pStartLineOfLastFullScreen = the number of lines of pTheTextString - (pnVisibleLines - 1) set pHeightPerLine = float(pThumbRange) / float(pStartLineOfLastFullScreen - 1) set pCurrentTopLine = 1 mScroll(me, 0) puppetSprite pchThumb, TRUE return me end birth
on mHitScrollArrow me, upOrDown --单击滚动杆的上下箭头的句柄 set chArrow = the clickOn if upOrDown = #Up then set scrollAmount = -1 else set scrollAmount = 1 end if mScroll(me, scrollAmount) repeat while the mouseDown mScroll(me, scrollAmount) end repeatend mHitScrollArrow
on mHitScrollBar me --单击滚动杆的句柄 if the mouseV > the locV of sprite pchThumb then mScroll(me, (pnVisibleLines - 1)) repeat while the stillDown AND (the bottom of sprite pchThumb < the mouseV) mScroll(me, (pnVisibleLines - 1)) end repeatelse mScroll(me, -1 * (pnVisibleLines - 1)) repeat while the stillDown AND (the top of sprite pchThumb > the mouseV) mScroll(me, -1 * (pnVisibleLines - 1)) end repeat end if end mHitScrollBar
on mDragThumb me --拖动滚动块的句柄 repeat while the mouseDown set newMouseV = the mouseV if newMouseV >= pThumbBottom then set newMouseV = pThumbBottom else if newMouseV <= pThumbTop then set newMouseV = pThumbTop end if set the locV of sprite pchThumb = newMouseV updateStage end repeat set pct = float(newMouseV - pThumbTop) / float(pThumbRange) set tempTopLine = pStartLineOfLastFullScreen * pct set pCurrentTopLine = integer(tempTopLine + 0.5) if pCurrentTopLine = 0 then set pCurrentTopLine = 1 end if mScroll(me, 0) end mDragThumb
on mScroll me, nLines --滚动nLines行 set pCurrentTopLine = (pCurrentTopLine + nLines) if (pCurrentTopLine < 1) then set pCurrentTopLine = 1 end if if pCurrentTopLine > pStartLineOfLastFullScreen then set pCurrentTopLine = pStartLineOfLastFullScreen end if set the text of field pOnScreenField = line pCurrentTopLine to (pCurrentTopLine + (pnVisibleLines - 1)) of pTheTextString set pixelOffset = integer(pHeightPerLine * float(pCurrentTopLine - 1)) set the locV of sprite pchThumb = pThumbTop + pixelOffset updateStage end mScroll
尽管对象的概念对一些用户来说并不好理解,但LINGO中对象的使用将必然在DIRCTOR 高级编程中占主要地位。我使用LINGO对象编程时间不常。希望与DIRECTOR爱好者们探讨这方面的问题。
|