cocos2d-x-3.16 实例开发之StickHero【3】英雄主角

英雄用一个单独的类Player来实现

一、加载动画

英雄的动画是做成anim1.plist,在WelcomeScene中已经加入到缓存

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("image/anim1/anim1.plist");

初始化将停留和行走的动画加到容器中待用

	_player = Sprite::createWithSpriteFrameName("stay1.png");
	_player->setAnchorPoint(Vec2(0.5,0));
	//1
	Vector<SpriteFrame*> frameVector;
	for(int i = 1;i<=5;i++)
	{
		char pngName[260] = {0};
		sprintf(pngName, "stay%d.png",i);
		frameVector.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName(pngName));
	}
	StayAnimation = Animation::createWithSpriteFrames(frameVector, 0.1f);
	StayAnimation->setRestoreOriginalFrame(false);
	StayAnimation->setLoops(-1);//无限循环
	StayAnimate = Animate::create(StayAnimation);
	animVector.pushBack(StayAnimate);//将动画装进容器
	//2
	frameVector.clear();
	for(int i = 1;i<=5;i++)
	{
		char pngName[260] = {0};
		sprintf(pngName, "walk%d.png",i);
		frameVector.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName(pngName));
	}
	WalkAnimation = Animation::createWithSpriteFrames(frameVector, 1.0f);
	WalkAnimation->setRestoreOriginalFrame(false);
	WalkAnimation->setLoops(-1);//无限循环
	WalkAnimate = Animate::create(WalkAnimation);
	animVector.pushBack(WalkAnimate);//将动画装进容器

二、实现方法

英雄主要有停留状态动画、行走动画、移动、停止动画

void Player::Stay()
{
	auto animate = animVector.at(0);
	_player->runAction(animate);
}
void Player::Stop()
{
	_player->stopAllActions();
}
void Player::Walk()
{
	auto animate = animVector.at(1);
	_player->runAction(animate);
}
void Player::Start(Vec2 _dec)
{
	MoveTo* move = MoveTo::create(0.2f, _dec);
	_player->runAction(move);
}


发表评论

(必填)

(必填)

(以便回访)

博客已稳定运行: 访问量:156,350 views 网站备案号:闽ICP备17026396号-1