最常用,也是最簡單的一種方法
CCSprite *bg=CCSprite::create("sprite/background.jpg",CCRectMake(0,0,480,320)); bg->setAnchorPoint(ccp(0,0)); 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(100,winSize.height/2)); addChild(plant);
將圖片加載到緩存中
CCTexture2D * texture2=CCTextureCache::sharedTextureCache()->addImage("sprite/bullet.png"); CCSprite *bullet=CCSprite::createWithTexture(texture2); bullet->setPosition(ccp(200,winSize.height/2+10)); 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(400,winSize.height/2-50)); addChild(zombie); CCJumpBy *by=CCJumpBy::create(2,ccp(0,0),40,3); zombie->runAction(CCRepeatForever::create(by));
也可以直接使用spriteFrame加載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);