当前位置: 首页 > news >正文

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
}

http://www.dtcms.com/a/393673.html

相关文章:

  • 亚马逊云科技重磅推出 Amazon S3 Vectors:首款大规模支持原生向量的云存储服务
  • SQLite Expert:一款功能强大的SQLite管理工具
  • Python 2025:供应链安全威胁与防御实战
  • 队列+宽搜(BFS)-429.N叉树的层序遍历-力扣(LeetCode)
  • 【Linux命令从入门到精通系列指南】rm 命令详解:安全删除文件与目录的终极实战手册
  • Springboot使用dockerfile-maven-plugin部署镜像
  • 安卓蓝牙键盘和鼠标6.10.4去更新汉化版 手机变为蓝牙键盘和鼠标
  • 工作笔记-----lwip的内存管理策略解析
  • 量子计算学习笔记(1)
  • Python爬虫基础与应用
  • Rabbitmq 集群初始化,配置导入
  • 云计算与虚拟化技术详解
  • elasticsearch 的配制
  • React学习教程,从入门到精通,React Hook 详解 —— 语法知识点、使用方法与案例代码(26)
  • ELK日志分析性能瓶颈问题排查与解决实践指南
  • 【Unity】【Photon】Fusion2中的匹配API 学习笔记
  • (3-1) Html
  • 《人机协同的边界与价值:开放世界游戏系统重构中的AI工具实战指南》
  • 数据库造神计划第十九天---事务(2)
  • Python到剪映草稿生成及导出工具,构建全自动化视频剪辑/混剪流水线
  • WordPress给指定分类文章添加一个自动化高亮(一键复制)功能
  • 5分钟使用Dify实现《射雕英雄传》问答智能体Agent
  • 3. 认识 const
  • 云原生 vs 传统部署
  • 2.1、机器学习-模型评估指标与参数调优
  • 设计模式(C++)详解—享元模式(2)
  • Linux实用操作以及基础命令
  • 深入理解 Vue 插槽:从基础到高级用法
  • 自动排班系统:劳动力管理新选择
  • Word和WPS文字中设置了倍数行距却没有变化?原因和调整方法