cocos2d_x开发平台吧 关注:1,246贴子:2,258
  • 2回复贴,共1

cocos2d练习(三)--添加多个png制成动态图

只看楼主收藏回复

建立号相应的菜单项CCMenuItemImage或CCMenuItemFont后,为其建立回调函数
我们在这个函数里建立动态的png图片显示代码
比如如下几张图
fish001.png
fish002.png
fish003.png
fish004.png
要在回调函数void NewLayer::heroMove(CCObject *p)中依次循环显示这四张图片
代码如下
void NewLayer::heroMove(CCObject *p)
{
CCAnimation *animation=CCAnmiation::create();
char buf[100];//用于存放图片的存储路径
for(int i=1;i<5;i++)
{
sprintf(buf,"fish//fish00%d.png",i);//图片存在了Resouces文件夹下的fish文件夹里
CCText2D *player=CCTextCache::shareTextureCache()->addImage(buf);
animation.addSpriteFrame(CCSpriteFrame::createWithTexture(player,cocos2d::CCRectMaker(0,0,100,100)));
}
animation->setDelayPerUnit(0.1f);//每帧图片的切换时间
CCAnimate *action=CCAnimate::create(animation);
CCText2D *paly0=CCTextureCache::shareTexture->addImage("fish/fish001.png");
CCSprite *pp=CCSprite::createWithSpriteFrame(CCSpriteFrame::createWithTexture(play0,cocos2d::CCRectMake(0,0,100,100)));
pp->setPosition(ccp(200,200));
addChild(pp,0);
pp->runAction(CCRepeatForever::create(action));
}


1楼2013-08-30 15:37回复
    sprintf(buf,"fish//fish00%d.png",i);
    要改成sprintf(buf,"fish/fish00%d.png",i);
    否则生成的apk无法加载图片,


    2楼2013-08-30 16:58
    回复


      IP属地:浙江3楼2023-04-25 09:33
      回复