Developers Manual > Cocos2d-x > Deprecated Docs for Cocos2d-x > Upgrade Guides Cocos2d-x > About Static Constructor API changes in cocos2d-x v20

About Static Constructor API Changes in cocos2d-x v2.0

Previously, we had OBJ-C style API to create objects.
For example, creating a sprite:

CCSprite* pSprite = CCSprite::spriteWithFile ("filename.png");

But this style is unfriendly for both C++ and Javascript programmers. Therefore, we added a ‘create’ static constructor for most classes.
Now, to create an object, the only constructor name you need to remember is ‘create’.
There are ‘CCSprite::create’,‘CCNode::create’,‘CCMoveTo::create’,‘CCCallfunc::create’and so on.
For example
<pre>
CCSprite spr = CCSprite::create;
spr~~>setPosition);
addChild;
CCMoveBy* act1 = CCMoveBy::create);
CCMoveBy* act2 = act1~~>reverse;
CCFiniteTimeAction
act3 = CCSequence::create(act1, act2, NULL);
CCRepeat* act4 = CCRepeat::create(act3, 2);
spr->runAction(act4);
</pre>
All objects created from ’XXX::create ()’ are autorelease object and they does not need to be released unless you manually retained it.

Although we have added the ‘create’ API for most classes, we kept old obj-c style API too.
In addition, we marked all old obj-c API with ‘@deprecated’, and added a marco CC_DEPRECATED_ATTRIBUTE at the beginning of old API which will warn you during compilation if
you are using an old api to create object. We strongly advise you to use the new API, since old API may be discarded sooner or later.

Furthermore, the javascript binding API will also be related to the ‘create’ API. This will make sure that the Javascript binding uses the same set of API as Cocos2d-html5
Creating a sprite in Cocos2d-html5 and Javascript binding is simply:

var sp = cc.Sprite.create("filename.png");

Create methods also have been used in Lua-Bindings:

local sp = CCSprite:create("fileName.png")

BTW, create methods are also friendlier for javascript programmers.

Copyright © 2010 - 2014 Cocos2d-x.org Support Email: support@cocos.org