最近太忙,基本都沒有時間寫博文了,閒來無事,就把以前剛剛學習cocos2d-x時瞎寫的代碼拿來充個數吧,也懶得改了,大俠們就不用看了lol,剛學的童鞋可以大概看看,別太認真,也別太較真^_^
#include "cocos2d.h"
USING_NS_CC ;
/************************************************************************/
/*
作用:創建一個動畫,在調用running()函數時,以圖片集的名字組成的
字符串數組做爲參數傳入,第二個爲圖片集的個數。
by:深圳-xo
cocos2d-x學習羣:277525242
*/
/************************************************************************/
class ActionSprite : public cocos2d::CCNode
{
public:
ActionSprite();
virtual ~ActionSprite();
static CCSprite* boyFight();
static CCSprite* fire();
static CCSprite* runing(const std::string a[],const int max);
};
#include "ActionSprite.h"
ActionSprite::ActionSprite()
{
}
ActionSprite::~ActionSprite()
{
}
CCSprite* ActionSprite::boyFight()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite* s1 = CCSprite::create("Boys/0.png");
CCAnimation* mAnimation = CCAnimation::create();
char str[20];
for (int i = 1; i < 20; i++)
{
_snprintf(str, 19, "Boys/%d.png", i);
mAnimation->addSpriteFrameWithFileName(str);
}
mAnimation->setDelayPerUnit(2.8f / 14.0f);
mAnimation->setLoops(-1);
CCFiniteTimeAction* animation = CCAnimate::create(mAnimation);
s1->runAction(animation);
return s1;
}
CCSprite* ActionSprite::fire()
{
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("fire.plist","fire.png");
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->textureForKey("fire.png");
CCSpriteBatchNode* batchNode = CCSpriteBatchNode::createWithTexture(texture);
CCSprite* f1 = CCSprite::createWithSpriteFrameName("23-1.png");
CCAnimation* mAnimation = CCAnimation::create();
char str[8];
for(int i=23;i<30;i++)
{
_snprintf(str,7,"Fire/%d-1.png",i);
mAnimation->addSpriteFrameWithFileName(str);
}
mAnimation->setDelayPerUnit(2.8f / 14.0f);
mAnimation->setLoops(-1);
CCFiniteTimeAction* action = CCAnimate::create(mAnimation);
f1->runAction(action);
return f1;
}
CCSprite* ActionSprite::runing(const std::string a[],const int max)
{
CCSprite* s1 = CCSprite::create(a[0].c_str());
CCAnimation* mAnimation = CCAnimation::create();
for (int i = 1; i < max; i++)
{
mAnimation->addSpriteFrameWithFileName(a[i].c_str());
}
mAnimation->setDelayPerUnit(2.8f / 14.0f);
mAnimation->setLoops(-1);
CCFiniteTimeAction* action = CCAnimate::create(mAnimation);
s1->runAction(action);
return s1 ;
}
const int max = 10 ;
std::string heroMove[] = {"hero/h1.png","hero/h2.png","hero/h3.png","hero/h4.png","hero/h5.png"
,"hero/h6.png","hero/h7.png","hero/h8.png","hero/h9.png","hero/h10.png"};
std::string heroStand[] = {"hero/h1.png","hero/h2.png","hero/h3.png","hero/h4.png"};
std::string fire[] = {"Fire/23-1.png","Fire/24-1.png","Fire/25-1.png","Fire/26-1.png",
"Fire/27-1.png","Fire/28-1.png","Fire/29-1.png","Fire/30-1.png",};
//獲得英雄對象
CCSprite* hr = ActionSprite::runing(heroMove,max);
hr->setPosition(ccp(250,hr->getContentSize().height+20));
hr->setScale(1.8f);
this->addChild(hr,10);
//火堆
CCSprite* f = ActionSprite::runing(fire,8);
f->setPosition(ccp(500,f->getContentSize().height/2));
this->addChild(f,10);