[Cocos2d-js official documentation] 16, create, cocos2d-jscreate

Source: Internet
Author: User

[Cocos2d-js official documentation] 16, create, cocos2d-jscreate

All objects are created using the create method, and methods such as createXXX no longer exist.

1. Four ways to create Sprite

(1) create a path based on the image resource

// Parameter 1: image resource path var sprite1 = cc. sprite. create ("res/HelloHTML5World.png"); // parameter 1: image resource path, parameter 2: display area var sprite2 = cc. sprite. create ("res/HelloHTML5World.png", cc. rect (0, 0, 480,320 ));

(2) create a frame name based on the frame name in the plist file. Note: The # symbol must be added to the front to distinguish

// Parameter 1: frame namevar sprite = cc.sprite.create('{grossini_dance_01.png ');

(3) create a sprite frame

Var spriteFrame = cc. spriteFrameCache. getSpriteFrame ("grossini_dance_01.png"); // parameter 1: cc. SpriteFrame object var sprite = cc. Sprite. create (spriteFrame );

(4) texture-Based texture Creation

Var texture = cc. textureCache. addImage ("HelloHTML5World.png"); // parameter 1: texture var sprite1 = cc. sprite. create (texture); // parameter 1: texture, parameter 2: display area var sprite2 = cc. sprite. create (texture, cc. rect (0, 0, 480,320 ));
2. Text LabelTTF can be created in two ways

(1) create multiple parameters based on the font and size

// Parameter 1: Display string, parameter 2: font, parameter 3: font size, parameter 4: width and height, parameter 5: locate var myLabel = cc. labelTTF. create ('label text', 'times New Roman ', 32, cc. size (320,32), cc. TEXT_ALIGNMENT_LEFT );

(2) create a cc. FontDefinition object

Var fontDef = new cc. fontDefinition (); fontDef. fontName = "Arial"; fontDef. fontSize = "32"; // parameter 1: Display string, parameter 2: custom object cc. fontDefinitionvar myLabel = cc. labelTTF. create ('label text', fontDef );
3. Three Animation creation methods

(1) Empty Creation

// No parameter var animation1 = cc. Animation. create ();

(2) create Based on sprite frames

Var spriteFrameArr = []; var spriteFrame = cache. getSpriteFrame ("grossini_dance_01.png"); spriteFrameArr. push (spriteFrame); // parameter 1: Number Group var animation1 = cc. animation. create (spriteFrameArr); // parameter 1: Number of Sprite frames, parameter 2: duration, unit: Second var animation2 = cc. animation. create (spriteFrameArr, 0.2); // parameter 1: Number of Sprite frames, parameter 2: duration, unit: seconds, parameter 3: number of cycles var animation3 = cc. animation. create (spriteFrameArr, 0.2, 2 );

(3) create an animation Frame

Var animationFrameArr = []; var animationFrame = new cc. animationFrame (); aFrame1.initWithSpriteFrame (spriteFrame1, 0.5); animationFrameArr. push (animationFrame); // parameter 1: Number of animation frames var animation1 = cc. animation. create (animationFrameArr); // parameter 1: Number of animation frames, parameter 2: duration, unit: Second var animation2 = cc. animation. create (animationFrameArr, 0.2); // parameter 1: Number of animation frames, parameter 2: duration, unit: seconds, parameter 3: number of cycles var animation3 = cc. animation. create (animationFrameArr, 0.2, 2 );
4. Batch creation of SpriteBatchNode in two ways

(1) Based on the image resource path

// Parameter 1: Image path, parameter 2: Capacity var spriteBatchNode = cc. SpriteBatchNode. create ("res/animations/grossini.png", 50 );

(2) Based on the texture

Var texture = cc. textureCache. addImage ("res/animations/grossini.png"); // parameter 1: texture, parameter 2: Capacity var spriteBatchNode = cc. spriteBatchNode. create (texture, 50 );
5. Two Methods for creating SpriteFrame

(1) Based on the image resource path

// Parameter 1: Image path; parameter 2: Region var frame1 = cc. spriteFrame. create ("res/grossini_dance.png", cc. rect (90,128,); // parameter 1: Image path, parameter 2: region, parameter 3: whether to rotate, parameter 4: Offset, parameter 5: original region var frame2 = cc. spriteFrame. create ("res/grossini_dance.png", cc. rect (90,128,), false, 0, cc. size (90,128 ));

(2) Based on the texture

Var texture = cc. textureCache. addImage ("res/grossini_dance.png"); // parameter 1: Image path, parameter 2: Region var frame1 = cc. spriteFrame. create (texture, cc. rect (90,128,); // parameter 1: Image path, parameter 2: region, parameter 3: whether to rotate, parameter 4: Offset, parameter 5: original region var frame2 = cc. spriteFrame. create (texture, cc. rect (90,128,), false, 0, cc. size (90,128 ));
6. There are two ways to create the particle effect system.

(1) Based on the image resource path

// Parameter 1: particle quantity var particle = cc. particle System. create (50 );

(2) Based on the texture

// Parameter 1: The file var particle = cc. particle System. create ("res/particle. plist") exported by particle designer ");
8. Four physical PhysicsSprite creation methods

(1) create a path based on the image resource

// Parameter 1: image resource path var physicsSprite1 = cc. physicsSprite. create ("res/HelloHTML5World.png"); // parameter 1: image resource path, parameter 2: display area var physicsSprite2 = cc. physicsSprite. create ("res/HelloHTML5World.png", cc. rect (0, 0, 480,320 ));

(2) create a frame name based on the frame name in the plist file. Note: The # symbol must be added to the front to distinguish

// Parameter 1: frame namevar physicsSprite = cc.physicssprite.create('{grossini_dance_01.png ');

(3) create a sprite frame

Var spriteFrame = cc. spriteFrameCache. getSpriteFrame ("batch"); // parameter 1: cc. SpriteFrame object var physicsSprite = cc. PhysicsSprite. create (spriteFrame );

(4) texture-Based texture Creation

Var texture = cc. textureCache. addImage ("HelloHTML5World.png"); // parameter 1: texture var physicsSprite1 = cc. physicsSprite. create (texture); // parameter 1: texture, parameter 2: display area var physicsSprite2 = cc. physicsSprite. create (texture, cc. rect (0, 0, 480,320 ));
9. There are two ways to create a large texture TextureAtlas

(1) Based on the image resource path

// Parameter 1: Image path, parameter 2: Capacity var textureAtlas = cc. TextureAtlas. create ("res/animations/grossini.png", 50 );

(2) Based on the texture

Var texture = cc. textureCache. addImage ("res/animations/grossini.png"); // parameter 1: texture, parameter 2: Capacity

var textureAtlas = cc.TextureAtlas.create(texture,50);

Reprinted Please note: http://www.cocos2dx.net/post/227

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.