建立号相应的菜单项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));
}
我们在这个函数里建立动态的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));
}