方法一
最常用,也是最简单的一种方法
CCSprite *bg=CCSprite::create("sprite/background.jpg",CCRectMake(,,,));
bg->setAnchorPoint(ccp(,));
bg->setPosition(CCPointZero);
addChild(bg);
方法二
利用纹理创建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(,winSize.height/));
addChild(plant);
方法三
将图片加载到缓存中
CCTexture2D * texture2=CCTextureCache::sharedTextureCache()->addImage("sprite/bullet.png");
CCSprite *bullet=CCSprite::createWithTexture(texture2);
bullet->setPosition(ccp(,winSize.height/+));
addChild(bullet);
方法四
CCSpriteFrame方法
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/-));
addChild(zombie); CCJumpBy *by=CCJumpBy::create(,ccp(,),,); zombie->runAction(CCRepeatForever::create(by));
也可以直接使用spriteFrame加载sprite
CCSpriteFrame * frame=CCSpriteFrame::create("sprite/p_1_01.png",CCRectMake(,,,));
CCSprite *sun=CCSprite::createWithSpriteFrame(frame);
sun->setPosition(ccp(,winSize.height/+));
addChild(sun);