Sprite classes in Cocos2d-JS

Source: Internet
Author: User

Sprite classes in Cocos2d-JS
The Sprite class is cc. Sprite, and its class diagram is shown in. The cc. Sprite class directly inherits the cc. Node class and has the basic features of cc. Node.
You can use constructor to create Sprite objects and use the same parameters. These parameters are flexible. In summary, there are four main ways to create a genie object:
1. Create an image based on the image resource path

// Image resource path var sp1 = new cc. sprite (res/background.png); // image resource path and the cropped rectangular area var sp2 = new cc. sprite (res/tree.png, cc. rect (604, 38,302,295 ))
2. Create Based on the sprite frame name in the sprite table (texture Gallery)
// Image resource path var sp1 = new cc. sprite (res/background.png); // image resource path and the cropped rectangular area var sp2 = new cc. sprite (res/tree.png, cc. rect (604, 38,302,295 ))
This method is a string used to create parameters for image resource paths. to distinguish between the sprite frame name and image resource path, a bid (#) is added before the sprite frame name.


3. Create Based on the sprite Frame
We can get the sprite frame object through the sprite frame cache, and then get the sprite object from the sprite frame object.
// Genie frame cache var spriteFrame = cc.spriteFrameCache.getSpriteFrame(background.png); var sprite = new cc. Sprite (spriteFrame );
4. Create genie Based on Texture
// Create a texture object
Var texture = cc.textureCache.addImage(background.png); // specify the texture creation sprite var sp1 = new cc. sprite (texture); // specify the texture and the cropped rectangular area to create the Sprite var sp2 = new cc. sprite (texture, cc. rect (0, 0, 480,320 ));

 

 

Related Article

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.