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