当前位置:软件学习 > Director >>

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 )

[1] [2] 下一页  

[page_break]

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, # default: FALSE ] )
     lMyPropList. addProp ( # pbPropagateMouseWithin, [ # comment: "Propagate mouseWithin?" , # format: # boolean, # default: FALSE ] )
     lMyPropList. addProp ( # pbPropagateRightMouseDown, [ # comment: "Propagate rightMouseDown?" , # format: # boolean, # default: FALSE ] )
     lMyPropList. addProp ( # pbPropagateRightMouseUp, [ # comment: "Propagate rightMouseUp?" , # format: # boolean, # default: FALSE ] )
     lMyPropList. addProp ( # pbPropagateToInvisibleSprite, [ # comment: "Propagate events to nonvisible sprite?" , # format: # boolean, # default: FALSE ] )
     lMyPropList. addProp ( # pbAssumeStaticStage, [ # comment: "Sprites will not animate behind this sprite" , # format: # boolean, # default: TRUE ] )
end if
return lMyPropList
END getPropertyDescriptionList

相关附件(完)

上一页  [1] [2] 

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,