[Cocos2d-x for WP8] Sprite Elf __cocos2d-x

Source: Internet
Author: User
Tags addchild map class

Http://www.cnblogs.com/linzheng/p/3292684.html

Garfield Just have a little faith. [Cocos2d-x for Wp8]sprite wizard

Elves (Sprite) is the role of the game, such as the enemy, the game in the movement of objects and so on, so the wizard is a very common concept of the game, almost everywhere. In Cocos2d-x, the genie is represented by the Ccsprite class, which can be defined with a picture or a rectangular part of a picture. We can use the following code to create a sprite with a picture resource and add it to the game:

Ccsprite *b = ccsprite::create ("Cat.png");

B->setposition (CCP (s.width * 0.5, S.height * 0.5));

If you need to generate multiple identical sprites then you can use the wizard batch class Ccspritebatchnode class for processing, so this processing can improve the rendering efficiency of the wizard, the number of wizard needs to be drawn more, the effect is more obvious. Ccspritebatchnode is to put a number of sprites on a texture, draw the direct unified drawing of the texture, do not need to draw a separate child nodes, so the time to draw can save many times on the picture resources repeatedly open closed time. Ccspritebatchnode internally encapsulates a cctextureatlas (a texture atlas that encapsulates a cctexture2d) and a ccarray (a child node for storing Ccspritebatchnode: a single sprite). Note: Because the drawing is only open-close once, all the child nodes of the Ccspritebatchnode object must be the same picture as the one.

However, there is a limit to using Ccspritebatchnode, that is, the picture must come from the same file, if you use a picture to create the wizard, you will not be able to specify the depth of the wizard, so that all the sprites must be in the same rendering layer, but you can use the map set to avoid this problem If all of your textures are in the same file, you can just create a ccspritebatchnode. A texture map saves memory by putting some of the pictures we need in a fixed size picture. Because the OpenGL mechanism will be a single map processing to the corresponding size of the long width is 2 of the N-second side of the picture, so put the picture together can save space. You can use the Zwoptex tool to make such textures.

The Map class cctexture2d is the concept of OpenGL. In OpenGL is called the picture is the map, in Cocos2d-x cctexture2d is the picture object meaning, may create the spirit and so on the object by it. The Cctexture2d class is the basis for the wizard class and its associated classes.

In addition, there are Sprite frame class Ccspriteframe, Sprite Frame cache class Ccspriteframecache. The concept of sprite frames is generated relative to animation. A sprite is a fixed node that can have many Sprite frames (ccspriteframe), and the animation is created by switching between them. The sprite Frame cache class Ccspriteframecache is used to store sprite frames, and caching in advance helps improve the efficiency of the program. Ccspriteframecache is a single case pattern that is not part of a wizard and is shared by all sprites.

Here we use an example to generate a sprite using a map:

Testlayer::testlayer () {ccsize s = ccdirector::shareddirector ()->getwinsize ();
    Create a 2D texture from a picture cctexture2d *texture = Cctexturecache::sharedtexturecache ()->addimage ("Dragon_animation.png"); Creating a Sprite frame with a 2D texture allows you to intercept a region of the 2D texture via rectangular coordinates ccspriteframe *FRAME0 = ccspriteframe::createwithtexture (texture, ccrectmake (
    132*0, 132*0, 132, 132));
    Ccspriteframe *frame1 = ccspriteframe::createwithtexture (texture, Ccrectmake (132*1, 132*0, 132, 132));
    Ccspriteframe *frame2 = ccspriteframe::createwithtexture (texture, Ccrectmake (132*2, 132*0, 132, 132));
    Ccspriteframe *frame3 = ccspriteframe::createwithtexture (texture, Ccrectmake (132*3, 132*0, 132, 132));
    Ccspriteframe *frame4 = ccspriteframe::createwithtexture (texture, Ccrectmake (132*0, 132*1, 132, 132));
    Ccspriteframe *frame5 = ccspriteframe::createwithtexture (texture, Ccrectmake (132*1, 132*1, 132, 132));
    Create Sprite ccsprite* sprite0 = Ccsprite::createwithspriteframe (FRAME0) by Sprite frame; Sprite0->setposition (cCP (MB, S.HEIGHT/2));

    AddChild (SPRITE0);
    ccsprite* sprite1 = Ccsprite::createwithspriteframe (frame1);
    Sprite1->setposition (CCP (S.HEIGHT/2));

    AddChild (SPRITE1);
    ccsprite* Sprite2 = Ccsprite::createwithspriteframe (frame2);
    Sprite2->setposition (CCP (S.HEIGHT/2));

    AddChild (SPRITE2);
    ccsprite* sprite3 = Ccsprite::createwithspriteframe (Frame3);
    Sprite3->setposition (CCP (S.HEIGHT/2));

    AddChild (Sprite3);
    ccsprite* sprite4 = Ccsprite::createwithspriteframe (frame4);
    Sprite4->setposition (CCP (S.HEIGHT/2));

    AddChild (SPRITE4);
    ccsprite* sprite5 = Ccsprite::createwithspriteframe (FRAME5);
    Sprite5->setposition (CCP (S.HEIGHT/2));
AddChild (SPRITE5); }

Picture of the map:

The effect of the operation:


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.