欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Cocos2dx 帧动画创建精灵

程序员文章站 2024-03-25 20:08:04
...

用一张张图片创建

Animation* animation = Animation::create();
	for (int i = 1; i <= 17; i++) {
		auto frameName = __String::createWithFormat("fire/campFire%02d.png", i);
		log("frameName = %s", frameName->getCString());
		animation->addSpriteFrameWithFileName(frameName->getCString());//用文件名添加精灵帧
	}
	animation->setDelayPerUnit(0.08f);//帧延迟时间
	animation->setRestoreOriginalFrame(true);//播放完回到第一帧状态
	
	Animate* action = Animate::create(animation);//action为可执行的动画
	//创建精灵
	auto sprite = Sprite::create("fire/campFire02.png");
	sprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
	sprite->runAction(RepeatForever::create(action));//使用动画
	this->addChild(sprite);

精灵帧缓存创建

		Animation* animation = Animation::create();
		for (int i = 1; i <= 4; i++) {
			auto frameName = __String::createWithFormat("h%d.png", i);
			log("frameName = %s", frameName->getCString());
			SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()
				->getSpriteFrameByName(frameName->getCString());
			animation->addSpriteFrame(spriteFrame);//从精灵帧缓存中添加
		}

		animation->setDelayPerUnit(0.15f);
		animation->setRestoreOriginalFrame(true);
		Animate* action = Animate::create(animation);

相关标签: 精灵 帧动画