Flash 平台技術的最佳化(九) 事件捕獲和冒泡

來源:互聯網
上載者:User

使用事件捕獲和冒泡可以最大程度地減少事件處理函數。
ActionScript 3.0 中的事件模型引入了事件捕獲和事件冒泡的概念。利用事件冒泡可協助您最佳化 ActionScript 代碼執行時間。您可以在一個對象(而不是多個對象)上註冊事件處理函數以提高效能。例如,想象建立這樣一種遊戲,在該遊戲中使用者必須以最快的速度單擊蘋果以將其銷毀。該遊戲將刪除螢幕上各個被擊中的蘋果並為使用者增加分數。要偵聽由各個蘋果調度的MouseEvent.CLICK 事件,您可能會編寫以下代碼:
const MAX_NUM:int = 10;
var sceneWidth:int = stage.stageWidth;
var sceneHeight:int = stage.stageHeight;
var currentApple:InteractiveObject;
var currentAppleClicked:InteractiveObject;
for ( var i:int = 0; i< MAX_NUM; i++ )
{
currentApple = new Apple();
currentApple.x = Math.random()*sceneWidth;
currentApple.y = Math.random()*sceneHeight;
addChild ( currentApple );
// Listen to the MouseEvent.CLICK event
currentApple.addEventListener ( MouseEvent.CLICK, onAppleClick );
}
function onAppleClick ( e:MouseEvent ):void
{
currentAppleClicked = e.currentTarget as InteractiveObject;
currentAppleClicked.removeEventListener(MouseEvent.CLICK, onAppleClick );
removeChild ( currentAppleClicked );
}
此代碼對各個 Apple 執行個體調用 addEventListener() 方法。此外,它還會在使用者單擊每個蘋果時使用 removeEventListener() 方法刪除對應的接聽程式。然而, ActionScript 3.0 中的事件模型為某些事件提供了一個捕獲和冒泡階段,允許您偵聽來自父InteractiveObject 的這些事件。因此,可以最佳化以上代碼並在最大程度上減少對 addEventListener() 和removeEventListener()方法的調用次數。以下代碼使用捕獲階段偵聽來自父物件的事件:

const MAX_NUM:int = 10;
var sceneWidth:int = stage.stageWidth;
var sceneHeight:int = stage.stageHeight;
var currentApple:InteractiveObject;
var currentAppleClicked:InteractiveObject;
var container:Sprite = new Sprite();
addChild ( container );
// Listen to the MouseEvent.CLICK on the apple's parent
// Passing true as third parameter catches the event during its capture phase
container.addEventListener ( MouseEvent.CLICK, onAppleClick, true );
for ( var i:int = 0; i< MAX_NUM; i++ )
{
currentApple = new Apple();
currentApple.x = Math.random()*sceneWidth;
currentApple.y = Math.random()*sceneHeight;
container.addChild ( currentApple );
}
function onAppleClick ( e:MouseEvent ):void
{
currentAppleClicked = e.target as InteractiveObject;
container.removeChild ( currentAppleClicked );
}
上述代碼不僅經過了簡化,而且在很大程度進行了最佳化,只對父容器調用了一次 addEventListener() 方法。接聽程式不再註冊到Apple 執行個體,因此不需要在單擊蘋果時將其刪除。可通過停止事件傳播(此操作將阻止繼續傳播事件)來進一步最佳化onAppleClick() 處理函數:
function onAppleClick ( e:MouseEvent ):void
{
e.stopPropagation();
currentAppleClicked = e.target as InteractiveObject;
container.removeChild ( currentAppleClicked );
}
冒泡階段也可用於捕捉事件,方法是將 false 作為第三個參數傳遞到 addEventListener() 方法:
// Listen to the MouseEvent.CLICK on apple's parent
// Passing false as third parameter catches the event during its bubbling phase
container.addEventListener ( MouseEvent.CLICK, onAppleClick, false );
捕獲階段參數的預設值是 false,因此您可以將其忽略:
container.addEventListener ( MouseEvent.CLICK, onAppleClick );

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.