Cocos2dx can be used to create a sprite. cocos2dx can be used to create a sprite.
Method 1
The most common and simplest method
CCSprite *bg=CCSprite::create("sprite/background.jpg",CCRectMake(0,0,480,320)); bg->setAnchorPoint(ccp(0,0)); bg->setPosition(CCPointZero); addChild(bg);
Method 2
Use textures to create sprite
CCImage *image=new CCImage(); image->initWithImageFile("sprite/plant.png"); CCTexture2D *texture=new CCTexture2D(); texture->autorelease(); texture->initWithImage(image); CCSprite *plant=CCSprite::createWithTexture(texture); plant->setPosition(ccp(100,winSize.height/2)); addChild(plant);
Method 3
Load the image to the cache
CCTexture2D * texture2=CCTextureCache::sharedTextureCache()->addImage("sprite/bullet.png"); CCSprite *bullet=CCSprite::createWithTexture(texture2); bullet->setPosition(ccp(200,winSize.height/2+10)); addChild(bullet);
Method 4
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(400,winSize.height/2-50)); addChild(zombie); CCJumpBy *by=CCJumpBy::create(2,ccp(0,0),40,3); zombie->runAction(CCRepeatForever::create(by));
You can also directly use spriteFrame to load the sprite.
CCSpriteFrame * frame=CCSpriteFrame::create("sprite/p_1_01.png",CCRectMake(0,0,80,80)); CCSprite *sun=CCSprite::createWithSpriteFrame(frame); sun->setPosition(ccp(400,winSize.height/2+100)); addChild(sun);
Running result