Cocos2d x之事件分發機制

2021-10-03 07:31:35 字數 857 閱讀 4604

eventdispatch事件分發是響應使用者事件的機制,基本組成:

事件***封裝了我們事件的處理**

事件分發器通知使用者事件的***

事件物件包含事件的資訊

有五種事件***:

eventlistenertouch:響應touch觸控事件

eventlistenerkeyboard:響應鍵盤事件

eventlisteneracceleration:響應加速度計事件

eventlistenmouse:響應滑鼠事件

eventlistenercustom:響應自定義事件

當你有乙個***並且你想要乙個物件接受它被給予的事件時,你必須吞下它。這樣它就不會被傳遞給優先順序從高到低的其他物件。這很容易做到:

// when "swallow touches" is true, then returning 'true' from the

// ontouchbegan method will "swallow" the touch event, preventing

// other listeners from using it.

listener1-

>

setswallowtouches

(true);

// you should also return true in ontouchbegan()

listener1-

>ontouchbegan =

(touch* touch, event* event)

;

eventdispatcher事件分發器使用優先順序決定哪個***首先得到乙個事件。

cocos2dx事件分發機制

事件型別 enum class type 事件 型別 enum class type 關於事件 的優先權通過 addeventlistenerwithscenegraphpriority 新增的 優先權為0。通過 addeventlistenerwithfixedpriority 新增的 可以自定義...

cocos2dx自學之 事件監聽

auto label labelttf create click me courier 30 label setposition visiblesize.width 2,visiblesize.height 2 addchild label auto listener eventlistenerto...

cocos2d x註冊touch事件

cocos2d x註冊touch事件 之前在寫cocos2d專案時,一般註冊touch事件都是在init 方法中或onenter 方法中,例如 bool gamelayer init ccdirector shareddirector gettouchdispatcher addtargetedde...