cocos 添加背景,帧动画,贴图
// 包含必要的头文件
#include "HelloWorldScene.h" // 引入当前场景类的头文件
#include "ui/CocosGUI.h" // 引入 Cocos2d-x 的 UI 控件模块(虽然当前代码未直接使用,但为后续扩展准备)
USING_NS_CC; // 使用 Cocos2d-x 命名空间,简化代码书写
// 创建场景的静态方法,用于初始化并返回一个 Scene 对象
Scene* HelloWorld::createScene()
{
Scene* scene = Scene::create(); // 创建一个场景对象
HelloWorld* layer = HelloWorld::create(); // 创建一个 HelloWorld 层对象
scene->addChild(layer); // 将层添加到场景中
return scene; // 返回创建好的场景
}
// 初始化层的方法,在 create() 内部自动调用
bool HelloWorld::init()
{
if (!Layer::init()) // 调用父类的初始化方法,确保基础功能正常
{
return false; // 如果初始化失败,返回 false
}
Size visibleSize = Director::getInstance()->getVisibleSize(); // 获取当前设备的可见区域尺寸
Vec2 origin = Director::getInstance()->getVisibleOrigin(); // 获取可见区域的起点坐标(某些设备可能不是从 0,0 开始)
// 背景
Sprite* background = Sprite::create("./res (2)/No.2/fengmian_bg0.png"); // 创建背景精灵
if (background != nullptr) // 检查精灵是否创建成功(防止图片加载失败)
{
background->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); // 设置背景位置为屏幕中心
this->addChild(background, 0); // 将背景添加到当前层,zOrder 为 0,表示最底层
}
// 墙壁
Sprite* wall = Sprite::create("./res (2)/no.2/fengmian_bg1.png"); // 创建墙壁精灵
if (wall != nullptr)
{
wall->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); // 同样设置到屏幕中心
this->addChild(wall, 1); // zOrder 为 1,显示在背景之上
}
// 人物
Sprite* person = Sprite::create("./res (2)/no.2/fm_renwu.png"); // 创建人物精灵
if (person != nullptr)
{
person->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); // 设置到屏幕中心
this->addChild(person, 2); // zOrder 为 2,显示在墙壁之上
}
// 音乐按钮
Sprite* music = Sprite::create("./res (2)/no.2/sound_off_btn.png"); // 创建音乐按钮精灵
if (music != nullptr)
{
music->setPosition(Vec2(50, 54)); // 设置到屏幕左下角附近
this->addChild(music, 3); // zOrder 为 3,显示在人物之上
}
// 剧情按钮
Sprite* plot = Sprite::create("./res (2)/no.2/youxiyuanzuo_btn.png"); // 创建剧情按钮精灵
if (plot != nullptr)
{
plot->setPosition(Vec2(430, 54)); // 设置到屏幕右下角附近
this->addChild(plot, 3); // zOrder 为 3,与音乐按钮同一层级
}
// 灯笼
Sprite* lantern = Sprite::create("./res (2)/no.2/fm_denglong.png"); // 创建灯笼精灵
if (lantern != nullptr)
{
lantern->setPosition(Vec2(300, 500)); // 设置到屏幕上方
lantern->setAnchorPoint(Vec2(0.5f, 0.9f)); // 设置锚点为灯笼顶部偏下一点,便于摆动效果
lantern->setRotation(35.0f); // 初始旋转角度,偏向右侧
this->addChild(lantern, 3); // zOrder 为 3
// 灯笼摆动动画
RotateTo* swingRight = RotateTo::create(2.5f, 35.0f); // 向右旋转到 35 度,耗时 2.5 秒
RotateTo* swingLeft = RotateTo::create(2.5f, -35.0f); // 向左旋转到 -35 度,耗时 2.5 秒
Sequence* swingSequence = Sequence::create(swingRight, swingLeft, nullptr); // 创建一个序列,先右后左
RepeatForever* repeatSwing = RepeatForever::create(swingSequence); // 无限重复这个序列
lantern->runAction(repeatSwing); // 让灯笼执行这个无限摆动动画
}
// 眼睛动画1
Sprite* eyex = Sprite::create("./res (2)/no.2/fm_hs_eye1.png"); // 创建第一个眼睛精灵
if (eyex != nullptr)
{
this->addChild(eyex, 4); // zOrder 为 4,显示在按钮和灯笼之上
eyex->setPosition(visibleSize.width - 120, visibleSize.height / 2 - 75); // 设置到屏幕右侧中间偏下
// 创建帧动画
Animation* animation = Animation::create(); // 创建一个动画对象
for (int i = 1; i < 4; i++)
{
std::string str = String::createWithFormat("./res (2)/no.2/fm_hs_eye%d.png", i)->_string; // 循环加载 3 张图片
animation->addSpriteFrameWithFile(str); // 将每一帧添加到动画中
}
animation->setDelayPerUnit(0.3f); // 每帧间隔 0.3 秒
animation->setRestoreOriginalFrame(true); // 动画结束后恢复到第一帧
Animate* animate = Animate::create(animation); // 创建动画动作
RepeatForever* repeat = RepeatForever::create(animate); // 无限重复动画
eyex->runAction(repeat); // 让眼睛执行这个无限循环动画
}
// 眼睛动画2
Sprite* eye = Sprite::create("./res (2)/no.2/fm_xlk_eye1.png"); // 创建第二个眼睛精灵
if (eye != nullptr)
{
this->addChild(eye, 4); // zOrder 为 4
eye->setPosition(Vec2(195, 470)); // 设置到屏幕左上方
Animation* eyeAnimation = Animation::create(); // 创建动画对象
for (int i = 1; i < 4; i++)
{
std::string str = String::createWithFormat("./res (2)/no.2/fm_xlk_eye%d.png", i)->_string; // 循环加载 3 张图片
eyeAnimation->addSpriteFrameWithFile(str); // 添加每一帧
}
eyeAnimation->setDelayPerUnit(0.3f); // 每帧间隔 0.3 秒
eyeAnimation->setRestoreOriginalFrame(true); // 动画结束后恢复第一帧
Animate* eyeAnimate = Animate::create(eyeAnimation); // 创建动画动作
RepeatForever* eyeRepeat = RepeatForever::create(eyeAnimate); // 无限重复
eye->runAction(eyeRepeat); // 执行动画
}
return true; // 初始化成功,返回 true
}
// 关闭按钮的回调函数(虽然当前代码未使用,但保留模板)
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director* director = Director::getInstance(); // 获取导演单例
director->end(); // 结束当前游戏场景
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0); // 在 iOS 平台,强制退出程序
#endif
}