现在的位置: 首页 >> 多媒体 >> Director >> Director 动态事件传递(2)
添加时间:2005-10-29 来源:网教中国 作者: unknown
Director 动态事件传递(2)

Director 动态事件传递
   那么更下层的其他角色该怎么办呢?忽略它们吗?也就是说,为什么不将此事件传递给所有下层的角色呢?

   我们必须考虑的很重要的一点就是运行时间问题。Director允许在单帧中显示多达1000个角色,所以为了从一个行为中传递此事件,我们可能需要测试999次。

   但其他一些事件也有可能发生,像mouseDown、mouseEnter等等,忽略它们并不是一个好的想法,因此可以在getPropertyDescriptionList中添加一些参数,以允许我们确定哪些事件将获得消息,哪些事件不获得消息。

   最后,我们还应考虑到可见性和运动状态。因为Director是一个动态的工作环境,角色有时会重叠,有时不会重叠,所以最好能够动态侦测交叠状态,此外还可以添加一个参数,以使我们能够选择是否给一个已设置为不可见的角色发送事件。

   下面的影片包含了一个基本完成的行为,允许Director动态判定一个给定角色是否正和当前角色及鼠标位置交叠,一旦如此,行为将向底层角色发送指定的任何事件。


播放,以上示例,需下载此插件。点击下载插件

   稍后的Director影片使用了此行为的一个修改版本。原来的行为认为舞台上没有动画发生,虽然这样的行为已经非常适合于静态使用了,但稍后的版本更适合于动态使用。

   当然,你可以根据自己的需要添加其他参数,以最大限度的提高代码库的利用率。


播放,以上示例,需下载此插件。点击下载插件

   就象你看到的那样,背景动画并不会导致行为失效,因为它能够很清楚的判定两个矩形何时交叠,何时不交叠,并做出恰当的响应。

   下面是最终影片中完整的“动态事件传递”行为的全部代码。

PROPERTY pnIntersectSprite
PROPERTY pbPropagateMouseDown
PROPERTY pbPropagateMouseUp
PROPERTY pbPropagateMouseEnter
PROPERTY pbPropagateMouseLeave
PROPERTY pbPropagateMouseWithin
PROPERTY pbPropagateRightMouseDown
PROPERTY pbPropagateRightMouseUp
PROPERTY pbPropagateToInvisibleSprite
PROPERTY pbAssumeStaticStage

on beginSprite me
me .FindLowerSprite()
END beginSprite

on mouseDown me
if pbPropagateMouseDown = TRUE then
me .TransmitEvent( #mouseDown )
end if
END mouseDown

on mouseUp me
if pbPropagateMouseUp = TRUE then
me .TransmitEvent( #mouseUp )
end if
END mouseUp

on mouseEnter me
if pbPropagateMouseEnter = TRUE then
me .TransmitEvent( #mouseEnter )
end if
END mouseEnter

on mouseLeave me
if pbPropagateMouseLeave = TRUE then
me .TransmitEvent( #mouseLeave )
end if
END mouseLeave

on mouseWithin me
if pbPropagateMouseWithin = TRUE then
me .TransmitEvent( #mouseWithin )
end if
END mouseWithin

on rightMouseDown me
if pbPropagateRightMouseDown = TRUE then
me .TransmitEvent( #rightMouseDown )
end if
END rightMouseDown

on rightMouseUp me
if pbPropagateRightMouseUp = TRUE then
me .TransmitEvent( #rightMouseUp )
end if
END rightMouseUp

on FindLowerSprite me
   pnIntersectSprite = 0
if pbAssumeStaticStage = TRUE then
     nSprite = me . spriteNum - 1
     rMyRect = sprite ( me . spriteNum ). rect
repeat with nTest = nSprite down to 1
       rTestRect = sprite (nTest). rect
       rIntersect = intersect ( rMyRect, rTestRect )
if rIntersect <> rect ( 0 , 0 , 0 , 0 ) then
         pnIntersectSprite = nTest
exit repeat
end if
end repeat
end if
END FindLowerSprite

on TransmitEvent me , yEvent
   pMousePoint = the mouseLoc
if pnIntersectSprite <> 0 then
     rTestRect = sprite (pnIntersectSprite). rect
if pMousePoint. inside ( rTestRect ) then
if sprite (pnIntersectSprite). visible = TRUE or ( sprite (pnIntersectSprite). visible = FALSE and pbPropagateToInvisibleSprite = TRUE ) then
sendSprite ( pnIntersectSprite, yEvent )
end if
end if
else if pnIntersectSprite = 0 and pbAssumeStaticStage = FALSE then
     bFoundIntersect = FALSE
     nSprite = me . spriteNum - 1
     rMyRect = sprite ( me . spriteNum ). rect
repeat with nTest = nSprite down to 1
       rTestRect = sprite (nTest). rect
       rIntersect = intersect ( rMyRect, rTestRect )
if rIntersect <> rect ( 0 , 0 , 0 , 0 ) then
         bFoundIntersect = TRUE
exit repeat
end if
end repeat
if bFoundIntersect = TRUE then
       rTestRect = sprite (nTest). rect
if pMousePoint. inside ( rTestRect ) then
if sprite (nTest). visible = TRUE or ( sprite (nTest). visible = FALSE and pbPropagateToInvisibleSprite = TRUE ) then
sendSprite ( nTest, yEvent )
end if
end if
end if
end if
END TransmitEvent

on getPropertyDescriptionList me
if the currentSpriteNum > 0 then
     lMyPropList = [:]
     lMyPropList. addProp ( # pbPropagateMouseDown, [ # comment: "Propagate mouseDown?" , # format: # boolean, # default: FALSE ] )
     lMyPropList. addProp ( # pbPropagateMouseUp, [ # comment: "Propagate mouseUp?" , # format: # boolean, # default: TRUE ] )
     lMyPropList. addProp ( # pbPropagateMouseEnter, [ # comment: "Propagate mouseEnter?" , # format: # boolean, # default: FALSE ] )
     lMyPropList. addProp ( # pbPropagateMouseLeave, [ # comment: "Propagate mouseLeave?" , # format: # boolean

[1] [2]  下一页


上一篇:在Shockwave 3D中添加混合效果 下一篇:Director 动态事件传递(1)
大部分文章摘自网上,如有侵犯您的权益请与我们联系,我们会第一时间进行处理,谢谢! [ 打印文章 ] [ 关闭窗口 ]
推荐文章
 
相关文章
·Director 8制作模拟刮胡子演示程
·Director 中如何控制读取数据库
·director 应用技巧
·Director FAQ
·常见的Director MX 2004全屏问题
·Director MX 2004中MIAW使用心得
·Director 如何检测演员的载入进
·Director 对象列表的调用与删除
·Director 动态事件传递(1)
·Director Lingo应用之repeat wit
最新文章
·发布Director MX 2004
·DirectMedia Xtra 在 Director
·Speech Xtra 应用(Director MX
·谈谈Director作品的发布模式
·关于多媒体程序运行速度的研究
·Director MX 2004教程--哪些人适
·Director MX 2004教程--常用多媒
·Director MX 2004教程--为什么选
·Director MX 2004教程--Director
·Director MX 2004教程--Director
Google