zoukankan      html  css  js  c++  java
  • Cocos2d-x场景生命周期函数介绍

    层(Layer)的生命周期函数有如下:

    init()。初始化层调用。

    onEnter()。进入层时候调用。

    onEnterTransitionDidFinish()。进入层而且过渡动画结束时候调用。

    onExit()。退出层时候调用。

    onExitTransitionDidStart()。退出层而且开始过渡动画时候调用。

    cleanup()。层对象被清除时候调用。

    提示  层(Layer)继承于节点(Node),这些生命周期函数根本上是从Node继承而来。事实上所有Node对象(包括:场景、层、精灵等)都有这些函数,只要是子类化这些类都可以重写这些函数,来处理这些对象的不同生命周期阶段事件。

    我们重写HelloWorld层的中几个生命周期函数,代码如下:

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. bool HelloWorld::init()  
    2. {             
    3.     if( !Layer::init() )  
    4.     {  
    5.          returnfalse;  
    6.     }  
    7.    
    8.     log("HelloWorldinit");  
    9.     ......  
    10.     returntrue;  
    11. }  
    12.    
    13. void HelloWorld::onEnter()  
    14. {  
    15.     Layer::onEnter();  
    16.     log("HelloWorldonEnter");  
    17. }  
    18.    
    19. voidHelloWorld::onEnterTransitionDidFinish()  
    20. {  
    21.     Layer::onEnterTransitionDidFinish();  
    22.     log("HelloWorldonEnterTransitionDidFinish");  
    23. }  
    24.    
    25. void HelloWorld::onExit()  
    26. {  
    27.     Layer::onExit();  
    28.     log("HelloWorldonExit");  
    29. }  
    30.    
    31. voidHelloWorld::onExitTransitionDidStart()  
    32. {  
    33.     Layer::onExitTransitionDidStart();  
    34.     log("HelloWorldonExitTransitionDidStart");  
    35. }  
    36.    
    37. void HelloWorld::cleanup()  
    38. {  
    39.     Layer::cleanup();  
    40.     log("HelloWorldcleanup");  
    41. }  
    42.    

     

    注意 在重写层生命周期函数中,第一行代码应该是调用父类的函数,例如HelloWorld::onEnter()中第一行应该是Layer::onEnter()函数,如果不调用父类的函数可能会导致层中动画、动作或计划无法执行。

    如果HelloWorld是第一个场景,当启动HelloWorld场景时候,它的调用顺序如下图所示:

    更多内容请关注Cocos2d-x系列图书《Cocos2d-x实战(卷Ⅰ):C++开发》
    本书交流讨论网站:http://www.cocoagame.net
    欢迎加入cocos2d-x技术讨论群:257760386、327403678
     
  • 相关阅读:
    How to function call using 'this' inside forEach loop
    jquery.validate.unobtrusive not working with dynamic injected elements
    Difference between jQuery.extend and jQuery.fn.extend?
    Methods, Computed, and Watchers in Vue.js
    Caution using watchers for objects in Vue
    How to Watch Deep Data Structures in Vue (Arrays and Objects)
    Page: DOMContentLoaded, load, beforeunload, unload
    linux bridge
    linux bridge
    EVE-NG网卡桥接
  • 原文地址:https://www.cnblogs.com/iOS-Blog/p/3763756.html
Copyright © 2011-2022 走看看