《入门级-Cocos2d 4.0塔防游戏开发》---第二课:游戏加载界面开发

一、开发环境介绍

操作系统:UOS1060专业版本。

cocos2dx:版本

环境搭建教程:

统信UOS下配置安装cocos2dx开发环境_三雷科技的博客-CSDN博客

二、开发内容

游戏在开始时都需要加载大量的资源,为了让用户有等待时间,因此最先开发的场景为加载游戏资源场景,用于加载游戏资源,当然其中还可以处理升级等操作。

2.1 修改窗口的大小。

在AppDelegate.cpp文件中修改为以下内容:

cpp 复制代码
static cocos2d::Size designResolutionSize = cocos2d::Size(960, 640);

2.2 添加加载场景相关代码

在Classes目录中创建Scene目录用于存储关于场景相关的代码。

bash 复制代码
mkdir Sence
touch LoadingScene.cpp
touch LoadingScene.h

在CMakeLists.txt文件中加入场景代码文件

2.3 代码内容如下:

  • LoadingScene.cpp
cpp 复制代码
/****************************************************************************
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#include "LoadingScene.h"

USING_NS_CC;


Scene* LoadingScene::createScene()
{
    return LoadingScene::create();
}


// on "init" you need to initialize your instance
bool LoadingScene::init()
{
    //
    // 1. super init first
    if ( !Scene::init() )
    {
        return false;
    }
    numberOfLoadedRes = 0;
    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto background = Sprite::create("Loadding.png");
    background->setRotation(-90.0f);
    background->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
    addChild(background);
    // 定时更新,用于检测资源是否被加载完成,如果加载完成才跳入到正真的欢迎页面。
    schedule(CC_SCHEDULE_SELECTOR(LoadingScene::logic),1.0f);
    return true;

}
// 该代码处理加载资源的操作。
// 加载资源包括图片,声音等。
void LoadingScene::loadSource(){

}
void LoadingScene::logic(float dt)
{
     // 如果你愿意可以在这里通过监听numberOfLoadedRes的值来显示加载进度。
    if(0 == numberOfLoadedRes){
        loadSource();
    }else if(100 == numberOfLoadedRes){
        // 处理跳转动作。
    }
}
  • LoadingScene.h
cpp 复制代码
/****************************************************************************
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#ifndef __LOADING_SCENE_H__
#define __LOADING_SCENE_H__

#include "cocos2d.h"

class LoadingScene : public cocos2d::Scene
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();
    
    
    // implement the "static create()" method manually
    CREATE_FUNC(LoadingScene)
        void loadSource();
        void logic(float dt);
        int numberOfLoadedRes;
};

#endif // __LOADING_SCENE_H__

2.3 添加资源

由于加载场景中需要使用到资源。因此我们需要将资源拷贝到Resources目录中。

注意:在使用QT调试的时候有可能出现无法找到资源的情况,需要手动将Resources目录内容拷贝的debug/bin/Resources目录中!!!!

三、显示效果

相关推荐
X_StarX1 小时前
【Unity笔记02】订阅事件-自动开门
笔记·学习·unity·游戏引擎·游戏开发·大学生
霸王•吕布5 小时前
游戏引擎中顶点着色&像素着色
游戏引擎·顶点着色器·像素着色器·顶点颜色·顶点uv·顶点法向
Thomas_YXQ8 小时前
Unity URP法线贴图实现教程
开发语言·unity·性能优化·游戏引擎·unity3d·贴图·单一职责原则
徐子竣15 小时前
[学习记录]Unity-Shader-几何着色器
unity·游戏引擎·着色器
TESmart碲视21 小时前
HKS201-M24 大师版 8K60Hz USB 3.0 适用于 2 台 PC 1台显示器 无缝切换 KVM 切换器
单片机·嵌入式硬件·物联网·游戏·计算机外设·电脑·智能硬件
翻滚吧键盘1 天前
查看linux中steam游戏的兼容性
linux·运维·游戏
幻世界1 天前
【Unity智能模型系列】Unity + MediaPipe + Sentis + ArcFace模型:构建高效人脸识别比对系统
unity·游戏引擎
m0_552200821 天前
《UE5_C++多人TPS完整教程》学习笔记40 ——《P41 装备(武器)姿势(Equipped Pose)》
c++·游戏·ue5
漫游者Nova1 天前
虚幻引擎Unreal Engine5恐怖游戏设计制作教程,从入门到精通从零开始完整项目开发实战详细讲解中英字幕
ue5·游戏引擎·虚幻·游戏开发完整教程·恐怖游戏开发
死也不注释2 天前
【Unity 编辑器工具开发:GUILayout 与 EditorGUILayout 对比分析】
unity·编辑器·游戏引擎