"COCOS2D-JS Official Documents" 18, Cocos2d-js v3.0 object construction and class inheritance

Source: Internet
Author: User
Tags deprecated

In Cocos2d-js, objects are constructed in the same way as cocos2d-x, using functions of each class create . In Cocos2d-js v3.0 We will also bring you a traditional way to use new operators. Also, in the v3.0 alpha1 version, we cannot inherit the engine class in JSB because we have not found a better solution. But in v3.0 alpha2, the problem has been solved. Because new of the use of operators, it is easier to write inheritance code.

In this document, we'll show you how to use them, as well as a brief implementation principle.

1. Deprecated createFunction

Because the Cocos2d-js v3.0 supports a more simple and convenientnewConstruction method, allcreateAndcreateWithXXXFunctions are deprecated and will receive a warning message when they are called.newConstructed to support all types of oldcreateThe parameters of the function.

Developers have two ways to construct a Sprite object: a uniform create function and new operator call constructors, which accept the same parameters. These 2 methods are supported in both HTML5 and JSB, but their implementation principles are very different.

2. Constructors

We can now directly use new operator invokes the constructor of the class. For example, developers should use new cc. Sprite (...) To create a CC. Sprite objects are supported in both HTML5 and JSB, but their implementation is very different:

var sprite = new CC. Sprite (filename, rect); var sprite = new CC. Sprite (texture, rect); var sprite = new CC. Sprite (spriteframename);  

In the HTML5 engine, we reconstructed all engine classes

in JSB if you invoke the constructor of Cc.sprite using the new operator, we will actually invoke the Js_cocos2dx_sprite_constructor function in the C + + layer. In this C + + function, it allocates memory for this sprite object, adds it to the auto recycle pool, and then calls the JS layer's _ctor function calls different init functions based on the parameter type and number, which are also bindings for C + + functions:

Javascript JSB Cocos2d-x
Cc. Sprite.initwithspriteframename Js_cocos2dx_sprite_initwithspriteframename Cocos2d::sprite::initwithspriteframename
Cc. Sprite.initwithspriteframe Js_cocos2dx_sprite_initwithspriteframe Cocos2d::sprite::initwithspriteframe
Cc. Sprite.initwithfile Js_cocos2dx_sprite_initwithfile Cocos2d::sprite::initwithfile
Cc. Sprite.initwithtexture Js_cocos2dx_sprite_initwithtexture Cocos2d::sprite::initwithtexture

The sequence diagram for this process is as follows:

3. Inheritance

in COCOS2D-HTML5 2.x, when we inherit a class, we need to

var MySprite = cc.        Sprite.extend ({ctor:function () {this._super (); Custom initialization}//Add own properties and methods});    Mysprite.create = function (filename,rect) {var sprite = new MySprite ();    Initialize Sprite.initwithtexture (fileName, rect) using materials and rectangular regions; Return sprite;};/ /Create your elf var sprite = mysprite.create (Texture,cc.rect (0,0,480,320));  

In Cocos2d-js, we just need to write the ctor function with the correct arguments and call the _super function.

var MySprite = cc.Sprite.extend({    ctor:function(filename,rect){        this._super(filename,rect);        // 自定义初始化    }    // 添加自己的属性和方法});// 创建你的精灵var sprite = new MySprite(texture,cc.rect(0,0,480,320));

This is very well understood in the HTML5 engine, as we support the use of new operator.

But in JSB it's a bit complicated, in _super function We will call the sprite C + + layer ctor function: js_cocos2dx_sprite_ctor , this function not only instantiates the Sprite object, also calls CC. Sprite.prototype._ctor the and passes the arguments. _ctor function encapsulates the true initialization function of the Sprite class, invoking different initialization functions based on the passed arguments, so that we finally complete the execution of the custom ctor function.

The sequence diagram for this process is as follows:

Reprint: http://www.cocos2dx.net/post/238

"COCOS2D-JS Official Documents" 18, Cocos2d-js v3.0 object construction and class inheritance

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.