Quick event reference

Source: Internet
Author: User
Tags addchild


The Cocos2d-x after quick modifications provides some underlying event support. These events are divided into functions and purposes:

  • Node event (CC. node_event)
  • Frame event (CC. node_enter_frame_event)
  • Touch event
  • Keyboard Events
  • Gravity sensing event
  • Application events
Node event (CC. node_event)

A node event is triggered when a Node object enters or exits.

local node = display.newNode()node:addNodeEventListener(cc.NODE_EVENT, function(event)    print(event.name)end) scene:addChild(node)

Event property:

  • Event. Name: indicates the node event type and has the following values:

    • Enter: node is added to a running scenario.
    • Exit: node exits the running scenario.
    • Entertransitionfinish: the special effect ends when you enter a new scene.
    • Exittransitionstart: the special effect that begins when you exit an existing scenario.
    • Cleanup: When the node is completely cleared and deleted from the memory

The following sample code demonstrates the occurrence time of five event types:

Require ("framework. init ") -- first create a blank scene local sceneinit = display. newscene ("sceneinit") -- enter the scene display. replacescene (sceneinit) Local function createtestscene (name) local scene = display. newscene (name) local node = display. newnode () node: addnodeeventlistener (CC. node_event, function (event) printf ("node in scene [% s] node_event: % s", name, event. name) End) Scene: addchild (node) return sceneend -- Wait 1.0 seconds to create the first test scenario sceneinit: descrimwithdelay (function () Local scene1 = createtestscene ("scene1") display. replacescene (scene1) -- Wait 1.0 seconds to create the second test scenario scene1: descrimwithdelay (function () print ("--------") Local scene2 = createtestscene ("scene2") display. replacescene (scene2) End, 1.0) End, 1.0)

Output result:

Cocos2d: [1.1140] node in scene [scene1] NODE_EVENT: enterCocos2d: [1.1140] node in scene [scene1] NODE_EVENT: enterTransitionFinishCocos2d: [2.1467] --------Cocos2d: [2.1469] node in scene [scene1] NODE_EVENT: exitTransitionStartCocos2d: [2.1469] node in scene [scene1] NODE_EVENT: exitCocos2d: [2.1470] node in scene [scene1] NODE_EVENT: cleanupCocos2d: [2.1471] node in scene [scene2] NODE_EVENT: enterCocos2d: [2.1471] node in scene [scene2] NODE_EVENT: enterTransitionFinish

If no special effect is used during the switchover scenario, the event appears in the above sequence.

However, if you change the test codedisplay.replaceScene(scene2)Changedisplay.replaceScene(scene2, "random", 1.0), The event appears in the order:

Cocos2d: [1.1094] node in scene [scene1] NODE_EVENT: enterCocos2d: [1.1094] node in scene [scene1] NODE_EVENT: enterTransitionFinishCocos2d: [2.1258] --------Cocos2d: [2.1337] node in scene [scene1] NODE_EVENT: exitTransitionStartCocos2d: [2.1338] node in scene [scene2] NODE_EVENT: enterCocos2d: [3.1752] node in scene [scene1] NODE_EVENT: exitCocos2d: [3.1753] node in scene [scene2] NODE_EVENT: enterTransitionFinishCocos2d: [3.1754] node in scene [scene1] NODE_EVENT: cleanup

The reason for this difference is that two scenes will be rendered at the same time during the scenario switching special effect playback. Therefore, from the event perspective, we can seeenterIn the first scenarioexitEvent.

Therefore, when using node events, we should not assume the order in which events appear, but take specific measures based on specific events.

The following are common suggestions:

  • Enter: Some scenario initialization work can be done here.
  • Exit: if special effects are used for scenario switching, you can stop some animations in the scenario here to avoid downgrading the frame rate caused by the special effects of the scenario switching.
  • Cleanup: Suitable for cleaning

To simplify usage, quick encapsulates several ready-made methods for CC. node. If a developer creates his own Lua class from node (or inherited class), he can directly overwrite these methods:

Require ("framework. init ") -- define your own CC. node inheritance class local mynode = Class ("mynode", function (scenename) local node = display. newnode () node. scenename = scenename return nodeend) function mynode: onenter () printf ("node in scene [% s] method % s", self. scenename, "onenter") end function mynode: onexit () printf ("node in scene [% s] method % s", self. scenename, "onexit") end function mynode: onentertransitionfinish () printf ("node in scene [% s] method % s", self. scenename, "onentertransitionfinish") end function mynode: onexittransitionstart () printf ("node in scene [% s] method % s", self. scenename, "onexittransitionstart") end function mynode: oncleanup () printf ("node in scene [% s] method % s", self. scenename, "oncleanup") end -- first create a blank scene local sceneinit = display. newscene ("sceneinit") -- enter the scene display. replacescene (sceneinit) Local function createtestscene (name) local scene = display. newscene (name) local node = mynode. new (name) node: setnodeeventenabled (true) -- to enable node events, Scene: addchild (node) is called) return sceneend -- Wait 1.0 seconds to create the first test scenario sceneinit: descrimwithdelay (function () Local scene1 = createtestscene ("scene1") display. replacescene (scene1) -- Wait 1.0 seconds to create the second test scenario scene1: descrimwithdelay (function () print ("--------") Local scene2 = createtestscene ("scene2") display. replacescene (scene2, "random", 1.0) End, 1.0) End, 1.0)
The execution result is as follows:
Cocos2d: [1.0988] node in scene [scene1] method onEnterCocos2d: [1.0988] node in scene [scene1] method onEnterTransitionFinishCocos2d: [2.1156] --------Cocos2d: [2.1159] node in scene [scene1] method onExitTransitionStartCocos2d: [2.1160] node in scene [scene2] method onEnterCocos2d: [3.1488] node in scene [scene1] method onExitCocos2d: [3.1489] node in scene [scene2] method onEnterTransitionFinishCocos2d: [3.1489] node in scene [scene1] method onCleanup

The effect is the same as registering an event directly. The two methods allow developers to choose based on their actual needs.

Node frame event (CC. node_enter_frame_event)

After the event is registered, the event is triggered every time before the screen is refreshed (that is, between the previous frame and the next frame.

Require ("framework. init ") local scene = display. newscene () -- register the event scene: addnodeeventlistener (CC. node_enter_frame_event, function (DT) print (DT) End) -- enable the frame event scene: scheduleupdate () -- after 0.5 seconds, stop the frame event scene: performwithdelay (function () -- disable frame event scene: unscheduleupdate () print ("stop") -- wait another 0.5 seconds to re-enable frame event scene: performwithdelay (function () -- enable frame event scene again: scheduleupdate () End, 0.5) End, 0.5) display. replacescene (scene)

During running, the screen will continuously output the interval between the previous frame and the next frame (usually 1/60 seconds), and temporarily pause at 0.5 seconds.

Note: Make sure to callscheduleUpdate()The frame event is triggered.

Source: Click to open the link

















Quick event reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.