Director 动态事件传递(2)
Director 动态事件传递那么更下层的其他角色该怎么办呢?忽略它们吗?也就是说,为什么不将此事件传递给所有下层的角色呢?
我们必须考虑的很重要的一点就是运行时间问题。Director允许在单帧中显示多达1000个角色,所以为了从一个行为中传递此事件,我们可能需要测试999次。
但其他一些事件也有可能发生,像mouseDown、mouseEnter等等,忽略它们并不是一个好的想法,因此可以在getPropertyDescriptionList中添加一些参数,以允许我们确定哪些事件将获得消息,哪些事件不获得消息。
最后,我们还应考虑到可见性和运动状态。因为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
相关附件(完)
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]
- 更多Director疑问解答:
- Director MX 2004教程--Director与Flash,竞争还是合作?
- Director MX 2004教程--常用多媒体编著软件
- Director MX 2004教程--哪些人适合使用Director
- 关于多媒体程序运行速度的研究
- 谈谈Director作品的发布模式
- Director MX 2004教程--用Lingo语法和javascript实现同一功能
- Director中的属性
- Director MX 2004教程--创建新的演员表
- Director 疑难解答(5)
- Director的“洋葱皮”技术介绍(2)
- Director的“洋葱皮”技术介绍(1)
- Director MX 2004教程--演员的管理
- Director MX 2004教程--演员窗口
- Director MX 2004教程--提高开发效率
- Director MX 2004教程--Director MX 2004都支持些什么?
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,
部分文章来自网络,