cocos2dx图片加载方式

cocos2dx创建sprite的多种方法
方法一
最常用,也是最简单的一种方法
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);

你可能感兴趣的:(cocos2d-x)