COCOS2DX multiple ways to create a sprite

Source: Internet
Author: User
Tags addchild

Method One

The most common and simplest method

Ccsprite *bg=ccsprite::create ("sprite/background.jpg", Ccrectmake (0,0 ,480,());    BG->setanchorpoint (CCP (0,0));    BG-setposition (Ccpointzero);    AddChild (BG);
Method Two

Creating a Sprite with textures

    Ccimage *image=New  ccimage ();    Image->initwithimagefile ("sprite/plant.png");     *texture=New  cctexture2d ();    Texture, autorelease ();    Texture-initwithimage (image);     *plant=ccsprite::createwithtexture (texture);    Plant->setposition (CCP (2, winsize.height/));    AddChild (plant);

Method Three

Load a picture into the cache

    Cctexture2d * Texture2=cctexturecache::sharedtexturecache ()->addimage ("sprite/bullet.png " );     *bullet=ccsprite::createwithtexture (texture2);    Bullet->setposition (CCP (Max, winsize.height/2+));    AddChild (bullet);

Method Four

Ccspriteframe method

Ccspriteframecache::sharedspriteframecache ()->addspriteframeswithfile ("sprite/zombie.plist"); //ccspriteframe *sf=ccspriteframecache::sharedspriteframecache ()->spriteframebyname ("Z_1_attack_05.png"); //ccsprite * Zombie=ccsprite::createwithspriteframe (SF);Ccsprite *zombie=ccsprite::createwithspriteframename ("Z_1_attack_06.png"); Zombie->setposition (CCP ( -, winsize.height/2- -));    AddChild (zombie); Ccjumpby*by=ccjumpby::create (2, CCP (0,0), +,3); Zombie->runaction (Ccrepeatforever::create (by));

You can also load sprites directly using Spriteframe

    Ccspriteframe * Frame=ccspriteframe::create ("sprite/p_1_01.png", Ccrectmake (0 ,0,());     *sun=Ccsprite::createwithspriteframe (frame);    Sun->setposition (CCP (winsize.height/2+));    AddChild (Sun);

Run results

COCOS2DX multiple ways to create a sprite

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.