加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

第一节:创建自己的每一个场景

发布时间:2020-12-14 21:25:38 所属栏目:百科 来源:网络整理
导读:我是在mac环境下,使用的是xcode写的,直接创建一个新的项目 ,然后,就包含了一些基本的文件了。直接开始创建就可以了。 第一步: 先创建头文件: //// GameScene.h// study_01//// Created by Carlos on 15/6/3.////#ifndef study_01_GameScene_h#define s

我是在mac环境下,使用的是xcode写的,直接创建一个新的项目 ,然后,就包含了一些基本的文件了。直接开始创建就可以了。

第一步:

先创建头文件:

//
//  GameScene.h
//  study_01
//
//  Created by Carlos on 15/6/3.
//
//

#ifndef study_01_GameScene_h
#define study_01_GameScene_h
//防止重复包含头文件
#include "cocos2d.h"
//新建一个类,公共继承于cocos2d里面的Layer类
class GameScene:public cocos2d::Layer
{
//声明一些方法
private:
public:
    // there's no 'id' in cpp,so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();
    // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone
    virtual bool init();
    // implement the "static create()" method manually
    CREATE_FUNC(GameScene);
};
#endif



如上面所示,我都做了一些注释,头文件里面,因为就是一些声明。主要的过程,都在cpp文件里面。而这些方法,都是默认的例子里面有的,

第二点:

把自己的一些资源文件,放在resource目录下面,因为我是第一次使用xcode,对于这个,还真是不熟,我直接使用finder,往resource目录里面,拖了两张图片,以为就可以了,没想到不行,最后写完了,运行的时候,一直报错。原来,直接使用finder拖拽,xcode还不会自动刷新。我的解决办法是,直接把图片拖到xoode的项目目录中,这样就可以了。

第三步:

编写自己的cpp文件,这里面,我做了尽量详细地注释,应该不难理解。

//
//  GameScene.cpp
//  study_01
//
//  Created by carlos on 15/6/3.
//
//

#include "GameScene.h"
#include "cocos2d.h"

USING_NS_CC;

Scene* GameScene::createScene()
{
    auto scene = Scene::create();
    auto layer = GameScene::create();
    scene->addChild(layer);
    return scene;
}
bool GameScene::init()
{
    //如果父类没有初使化成功,就返回false
    if (!Layer::init()) {
        return false;
    }
    //在这里,我们往场景里面添加两张图片,需要先图片添加到我们的Resources文件夹
    //先获取窗口的尺寸
    Size size = Director::getInstance()->getVisibleSize();
    //这里先创建背景bg,里面的参数呢,就是图片的名字
    auto bg = Sprite::create("bg.jpg");
    //设置背景图片的位置
    bg->setPosition(size.width/2,size.height/2);
    //获取缩放比例
    float sx = size.width/bg->getContentSize().width;
    float sy = size.height/bg->getContentSize().height;
    //根据前面计算的比例,设置让背景全屏
    bg->setScale(sx,sy);
    //将背景添加到这个里面,第一个参数表示要添加的场景,第二个参数是Z轴的坐标,如果你再添加一个场景,Z轴为1,那它就会在0的上面
    this->addChild(bg,0);
    //再创建第二个场景
    auto button = Sprite::create("button.png");
    button->setPosition(size.width/2,size.height/2);
    this->addChild(button,1);
    return true;
    
}

这两个创建好了以后,需要把代理类里面的场景,换成我们自己的。因为新建的项目,默认的场景是HellowWorldScene,我们替换成我们的,然后,运行,就可以看到效果,如下面所示,是AppDelegate.cpp中的一个方法:

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLViewImpl::create("My Game");
        director->setOpenGLView(glview);
    }

    // turn on display FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);

    register_all_packages();

    // create a scene. it's an autorelease object
<span style="white-space:pre">	</span>//就是在这里替换的,GameScene就是我自己创建的一个场景
//    auto scene = HelloWorld::createScene();
    auto scene = GameScene::createScene();

    // run
    director->runWithScene(scene);

    return true;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读