I have learned "Touch events", "Touch capture events", and "button events" before. In addition, quick has several other events, which have been listed above,
Predefined node events:
- Cc. node_event-enter, exit, and other events
- Cc. node_enter_frame_event-frame event
- Cc. node_touch_event-touch event
- Cc. node_touch_capture_event-capture touch events
Predefined layer events:
- Cc. accelerate_event-gravity event
- Cc. keypad_event-hardware button event
Predefined menu events:
- Cc. menu_item_clicked_event-ccmenu
Here, I will take two simple examples to make it easy to master. First read this cc. node_event.
Node events are actually several lifecycles of nodes, which must be understood in cocos2dx C ++. Let's look at the Code directly,
local layer = display.newLayer() self:addChild(layer) layer:addNodeEventListener(cc.NODE_EVENT, function (event) if event.name == "enter" then print("enter") elseif event.name == "exit" then print("exit") elseif event.name == "cleanup" then print("cleanup") elseif event.name == "enterTransitionFinish" then print("enterTransitionFinish") elseif event.name == "exitTransitionStart" then print("exitTransitionStart") end end)
Cc. node_event can respond to the onenter, onexit, cleanup, exittransitionstart, and entertransitionfinish events of a node. If you do not add a listener, you can rewrite the corresponding function,
function MyScene:onEnter()endfunction MyScene:onExit()end
Next let's take a look at the CC. node_enter_frame_event frame event.
A frame event is an update timer, which calls each frame. If you want to use it, you must enable the update timer in addition to adding a listener, such as code,
local layer = display.newLayer() self:addChild(layer) layer:scheduleUpdate() layer:addNodeEventListener(cc.NODE_ENTER_FRAME_EVENT, function(dt) print(dt) end)
I will not talk about the other several events. Let's talk about how to master them.
The main problem is that for the parameter variables in the callback function, such as the acceleration event, what can be clicked after the event? I am so sure,
Because in version 2.2.3, the event distribution mechanism is re-created, go to the \ quick-cocos2d-x-2.2.5 \ framework \ cocos2dx directory, then look at the classification of these events, acceleration events belong to the predefined layer, so in layerex. in the Lua file, touch screens, frame events, and so on are predefined nodes, so they are in nodeex. in the Lua file.
Open these files to find the available fields, such as acceleration events. Open the layerex. Lua file and you can see the code like this,
function Layer:addAccelerateEventListener(listener) PRINT_DEPRECATED("Layer.addAccelerateEventListener() is deprecated, please use Layer.addNodeEventListener()") self:addNodeEventListener(c.ACCELERATE_EVENT, function(event) listener(event.x, event.y, event.z, event.timestamp) end)end
In listener, these are provided by quick, so that you can add event listening and write corresponding events in the callback function. These are discarded, but they all provide the addnodeeventlistener method, so...
The above is an introduction to all events. I think quick should be familiar with this. You can write some games to play games ~ I wonder if you are interested in writing one by yourself, haha.
Quick-cocos2d-x game development [13] -- node lifecycle events, frame events and other events