[Cocos2Dx-一个都不能死]3.添加游戏人物对象

当给游戏页面添加了边界框后,下一步就是在边框内添加游戏角色!其中包括游戏人物对象,是一个动画,其中需要使用到flash,另外一个就是游戏的滑块对象,其中滑块对象是自动运动的,下面我们先添加人物对象!

新建一个Hero.h头文件继承自Sprite精灵类,并新建一个Hero类,里面创建一个人物对象,并声明初始化方法

class Hero :public Sprite
{
private:
public:
virtual bool init();
CREATE_FUNC(Hero);
};

因为人物是有动画的,所以需要借助Flash来生成两个文件,动作描述的json文件和图片文件

[Cocos2Dx-一个都不能死]3.添加游戏人物对象_第1张图片
Paste_Image.png
[Cocos2Dx-一个都不能死]3.添加游戏人物对象_第2张图片
Hero.png

{"frames": {

"hero0000":
{
"frame": {"x":0,"y":0,"w":44,"h":52},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":44,"h":52},
"sourceSize": {"w":44,"h":52}
},
"hero0001":
{
"frame": {"x":44,"y":0,"w":42,"h":52},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":2,"y":0,"w":44,"h":52},
"sourceSize": {"w":44,"h":52}
},
"hero0002":
{
"frame": {"x":86,"y":0,"w":42,"h":52},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":2,"y":0,"w":44,"h":52},
"sourceSize": {"w":44,"h":52}
},
"hero0003":
{
"frame": {"x":0,"y":52,"w":42,"h":52},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":2,"y":0,"w":44,"h":52},
"sourceSize": {"w":44,"h":52}
},
"hero0004":
{
"frame": {"x":42,"y":52,"w":42,"h":52},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":2,"y":0,"w":44,"h":52},
"sourceSize": {"w":44,"h":52}
}},
"meta": {
"app": "Adobe Flash Professional",
"version": "13.1.0.226",
"image": "Hero.png",
"format": "RGBA8888",
"size": {"w":128,"h":128},
"scale": "1"
}
}


我们需要写一个解析Json数据的工具类,代码如下:

Animate * FlashTool::readJsonSpriteSheet(std::string jsonFile,float delayPerUnit){

rapidjson::Document doc;
std::string fileContent = FileUtils::getInstance()->getStringFromFile(jsonFile);
fileContent.erase(0,fileContent.find_first_of('{'));

doc.Parse<0>(fileContent.c_str());

std::string imgFileName = doc["meta"]["image"].GetString();

auto &frames = doc["frames"];
auto sfc = SpriteFrameCache::getInstance();

Vector animFrames;
for (auto m=frames.MemberBegin(); m!=frames.MemberEnd(); m++) {
    auto frameName = m->name.GetString();
    auto & frameProperties = m->value["frame"];
    auto & spriteSourceSize = m->value["spriteSourceSize"];
    
    auto sf = sfc->getSpriteFrameByName(frameName);
    if (!sf) {
        sf = SpriteFrame::create(imgFileName, Rect(frameProperties["x"].GetInt(), frameProperties["y"].GetInt(), frameProperties["w"].GetInt(), frameProperties["h"].GetInt()), m->value["rotated"].GetBool(), Vec2(spriteSourceSize["x"].GetInt(), spriteSourceSize["y"].GetInt()), Size(spriteSourceSize["w"].GetInt(), spriteSourceSize["h"].GetInt()));
        sfc->addSpriteFrame(sf, frameName);
    }
    animFrames.pushBack(AnimationFrame::create(sf, delayPerUnit, ValueMapNull));
}

Animation * animation = Animation::create(animFrames,delayPerUnit);
return Animate::create(animation);

}


如何应用呢?

之后,我们在Hero.cpp里面来实现人物对象的初始化

bool Hero::init() {
Sprite::init();
Size herobox = Size(44, 52);//设置任务的宽高

//限制宽高
setContentSize(herobox);
setPhysicsBody(PhysicsBody::createBox(herobox));

//设置锚点
setAnchorPoint(Point(0, 0));
getPhysicsBody()->setRotationEnable(false);

//添加碰撞检测
getPhysicsBody()->setContactTestBitmask(1);

action = RepeatForever::create(FlashTool::readJsonSpriteSheet(“Hero.png”, 0.2f));
runAction(action);
return true;

}

最后我们把Hero对象放入Scene里面

hero = Hero::create();
hero->setPosition(30, positionY);
addChild(hero);

这样,我们就实现了一个人物的动画效果!

项目git地址:https://github.com/marco115/NoOneDies.git
对文章有什么优化改进的地方,请留言!谢谢大家

你可能感兴趣的:([Cocos2Dx-一个都不能死]3.添加游戏人物对象)