ActionScript 3 now supports event propagation - the transference of a single event applying to multiple objects to each of those objects instead of one - in Display objects. In ActionScript 1 and 2, "Button" events (such as onPress, onRelease, etc.) handled by movie clips were not propagated to that movie clip's children. This means that though visually you were clicking on a child of a movie clip handling an onPress event, that onPress would never make it to the child because the parent movie clip handling the event would intercept it and prevent the propagation of the onPress event to that child.
Example; movie clip parent_mc contains child_mc:
ActionScript Code:
// AS1 and AS2
parent_mc.onPress = function(){
trace("parent pressed");
}
parent_mc.child_mc.onPress = function(){
trace("child pressed");
}
/* click child output:
parent pressed
*/
ActionScript Code:
// AS3
parent_mc.addEventListener(MouseEvent.CLICK, parentClick);
parent_mc.child_mc.addEventListener(MouseEvent.CLICK, childClick);
function parentClick(event:MouseEvent):void {
trace("parent pressed");
}
function childClick(event:MouseEvent):void {
trace("child pressed");
}
/* click child output:
child pressed
parent pressed
*/
In ActionScript 1 and 2, the child is never able to receive the event. In ActionScript 3, both movie clips are able to receieve it as the event is propagated to each movie clip to which it applies.
0 评论:
发表评论