這是HERO.CPP的代碼頁,融合了兩個影片的代碼,即人物搖桿走動和回合制戰斗
#include "Hero.h"
USING_NS_CC;
Hero* Hero::createHeroSprite(Point position, int direction, const char* name)
{
Hero* hero = new Hero();
if (hero && hero->init())
{
hero->autorelease();
hero->heroInit(position, direction, name);
return hero;
}
CC_SAFE_DELETE(hero);
return NULL;
}
bool Hero::init()
{
if (!Layer::init())
{
return false;
}
return true;
}
void Hero::heroInit(Point position, int direction, const char* name)
{
this->isRun = false;
this->position = position;
sprite = Sprite::create(String::createWithFormat("%s11.png", name)->getCString());
sprite->setPosition(position);
addChild(sprite);
auto* action = createAnimate1(1, "stand", 7);
action->setTag(100);
sprite->runAction(action);
}
Animate* Hero::createAnimate1(int direction, const char *action, int num)
{
auto* m_frameCache = SpriteFrameCache::getInstance();
m_frameCache->addSpriteFramesWithFile("hero.plist", "hero.png");
Vector<SpriteFrame*> frameArray;
for (int i = 1; i <= num; i++)
{
auto* frame = m_frameCache->getSpriteFrameByName(String::createWithFormat("%s%d%d.png", action, direction, i)->getCString());
frameArray.pushBack(frame);
}
Animation* animation = Animation::createWithSpriteFrames(frameArray);
animation->setLoops(-1);//表示無限回圈播放
animation->setDelayPerUnit(0.1f);//每兩張圖片的時間隔,圖片數目越少,間隔最小就越小
//將影片包裝成一個動作
return Animate::create(animation);
}
Animate* Hero::createAnimate(int direction, const char *action, int num, int time)
{
auto* m_frameCache = SpriteFrameCache::getInstance();
m_frameCache->addSpriteFramesWithFile("hero.plist", "hero.png");
Vector<SpriteFrame*> frameArray;
for (int i = 1; i <= num; i++)
{
auto* frame = m_frameCache->getSpriteFrameByName(String::createWithFormat("%s%d%d.png", action, direction, i)->getCString());
frameArray.pushBack(frame);
}
Animation* animation = Animation::createWithSpriteFrames(frameArray);
animation->setLoops(time);//表示無限回圈播放
animation->setDelayPerUnit(0.1f);//每兩張圖片的時間隔,圖片數目越少,間隔最小就越小
//將影片包裝成一個動作
return Animate::create(animation);
}
void Hero::setAction(int direction, const char *action, int num)
{
sprite->stopActionByTag(100);
auto* animate = createAnimate1(direction, action, num);
animate->setTag(100);
sprite->runAction(animate);
}
void Hero::moveTo(float x, float y)
{
float r = sqrt(x*x + y*y);
position.x += x / r;
position.y += y / r;
sprite->setPosition(position);
}
void Hero::runAttack(Hero* enemy)
{
sprite->stopAllActions();
Point target;
if (enemy->direction == 1)
{
target = Vec2(enemy->position.x - 20, enemy->position.y + 30);
}
else
{
target = Vec2(enemy->position.x + 20, enemy->position.y + 10);
}
auto* moveto = MoveTo::create(0.5f, target);
auto* run = createAnimate(direction, "run", 5, 1);
auto* spawn = Spawn::create(moveto, run, NULL);
auto* callFunc = CallFunc::create(CC_CALLBACK_0(Hero::doAttack, this));
auto* sequence = Sequence::create(spawn, callFunc, NULL);
sprite->runAction(sequence);
}
void Hero::doAttack()
{
auto* animate = createAnimate(direction, "attack", 14, 1);
auto* callFunc = CallFunc::create(CC_CALLBACK_0(Hero::runBack, this));
auto* sequence = Sequence::create(animate, callFunc, NULL);
sprite->runAction(sequence);
}
void Hero::runBack()
{
sprite->stopAllActions();
auto* back = createAnimate(direction, "back", 5, 1);
auto* moveto = MoveTo::create(0.5f, position);
auto* spawn = Spawn::create(back, moveto, NULL);
auto* callFunc = CallFunc::create(CC_CALLBACK_0(Hero::doStand, this));
auto* sequence = Sequence::create(spawn, callFunc, NULL);
sprite->runAction(sequence);
}
void Hero::doStand()
{
sprite->stopAllActions();
auto* animate = createAnimate(direction, "stand", 8, -1);
sprite->runAction(animate);
}
void Hero::waitingAttack()
{
scheduleOnce(schedule_selector(Hero::beAttacked), 1.1f);
}
void Hero::beAttacked(float dt)
{
sprite->stopAllActions();
auto* animate = createAnimate(direction, "underatt", 2, 1);
auto* callFunc = CallFunc::create(CC_CALLBACK_0(Hero::waiting, this));
auto* sequence = Sequence::create(animate, callFunc, NULL);
sprite->runAction(sequence);
}
void Hero::waiting()
{
scheduleOnce(schedule_selector(Hero::waitingUpdate), 0.5f);
}
void Hero::waitingUpdate(float dt)
{
doStand();
}
這是hero.h的代碼頁
#ifndef __HERO_SCENE_H__
#define __HERO_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class Hero : public Layer
{
public:
bool isRun;
int direction;
Point position;
Sprite* sprite;
void runAttack(Hero* enemy);
void doAttack();
void runBack();
void doStand();
void beAttacked(float dt);
void waiting();
void waitingAttack();
void waitingUpdate(float dt);
static Hero* createHeroSprite(Point position, int direction, const char* name);
void heroInit(Point position, int direction, const char* name);
virtual bool init();
Animate* createAnimate1(int direction, const char *action, int num);
Animate* createAnimate(int direction, const char *action, int num, int time);
void setAction(int direction, const char *action, int num);
void moveTo(float x, float y);
CREATE_FUNC(Hero);
};
#endif
求大神指教啊,答辯專案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/62891.html
標籤:Cocos2d-x
