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 ));