前面的教程中我们已经完美的运行起了游戏工程,Cocos2dx是C/C++编写的呢,一个面向对象编程的游戏引擎哦,是不是有些心动呢,那么一起共勉吧!
代码是最好的老师,我们从添加一幅图开始吧~
错了,下面才是
一、认识工程目录结构
前面的教程已经认识了工程的目录结构,默认的目录有两个类
AppDelegate.cpp //程序入口文件
HelloWorldScene.cpp //默认场景文件
1.AppDelegate类
我们可以仔细看一下AppDelegate.cpp这个文件,我们可以看到引用了HelloWorldScene.h
#include "HelloWorldScene.h"
在函数
AppDelegate::applicationDidFinishLaunching(){}
中我们可以看到如下的代码,该代码的作用是创建一个场景,然后跳转到这个场景中。
auto director = Director::getInstance();
//省略内容
// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();
// run
director->runWithScene(scene);
当我们想初始跳转到其他场景的时候,在这个地方修改想跳转的场景类。
2.HelloWorld类
我们先看一下头文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
CREATE_FUNC()是一个宏定义,我们可以看到定义了一个create方法哦,调用了init()方法
/** @def CREATE_FUNC(__TYPE__)
* Define a create function for a specific type, such as Layer.
*
* @param __TYPE__ class type to add create(), such as Layer.
*/
#define CREATE_FUNC(__TYPE__) \
static __TYPE__* create() \
{ \
__TYPE__ *pRet = new(std::nothrow) __TYPE__(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
return pRet; \
} \
else \
{ \
delete pRet; \
pRet = nullptr; \
return nullptr; \
} \
}
我们在继续打开HelloWorldScene.cpp文件,我们可以看到首先创建了一个场景,创建代码如下
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
我们知道这方法在哪了吧,一开始学习的是突然看到了这个方法都没反应过来,想想当时还是太年轻了
auto layer = HelloWorld::create();
我们继续往下看,init方法中有如下代码,这个就是创建一幅图片并把这幅图片加载到该层上,上面的createScene则是吧这个层加到该场景上。有其他相关代码大家可以具体看文件哦。
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
二、认识Director,Node,Scene,Layer,Sprite
相信大家看建立工程后浏览一遍默认生成的工程文件都应该知道相关对象的含义了吧,在这边在给大家介绍下,这里只做简单的介绍,在后面的游戏实现中将会涉及。
1.Director(导演类)
导演类Director是cocos2d-x引擎中非常重要的一个类,它是一个抽象类(mainloop空实现),主要完成以下工作:
(1)初始化游戏和销毁游戏
(2)管理调度场景
(3)调整设置openGL信息
(4)获取、设置游戏的相关细节
2.Node(节点类)
Node类是绝大部分类的父类,如Scene、Layer、Sprite等等的父类都是Node。
Node类包含了一些基本的属性、节点相关、Action动作的执行、以及定时器等相关的操作。
当然Node也有父类,其父类为Ref。
3.Scene(场景类)
Scene是继承与Node类的。作为场景类,它却只有函数create。因为场景就像是一个容器,将不同的布景层(Layer)组合在一起,方便管理。一个游戏会有很多的场景,比如,主界面,游戏界面,载入界面等等都是一个场景。而每一个场景都是由多个图层组合在一起,形成一个完整的游戏画面。
4.Layer(层类)
Layer继承于Node。Layer不仅继承了Node的所有操作,还附加触控、重力加速度计、支持键盘输入的事件代理。
一个布景层(Layer)可以包含多个元素,如标签(Label)、菜单(Menu)、精灵(Sprite)等等。
5.Sprite(精灵类,对应的图片资源)
Sprite继承于Node,简单一点,其实就是一个2D的图片,一般加载在Layer上
Sprite不仅继承了Node,还继承了纹理协议接口TextureProtocol。
三、添加自己的第一幅图片
上面我们了解了基本的概念,先不必要详细的分析底层的关系,我们带着问题来做下去。
我们都知道了如何添加图片,为了自己的游戏制作,然我们添加自己第一幅图片吧
我们把init方法清空,保留如下,并将HelloWorld.png替换自己的图片,来试一试
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("/ch/level/bg_level.jpg");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
return true;
}
图片替换了吧,是不是和前面我展示过的截图不一样呢?
这个就涉及到了屏幕尺寸还有设计尺寸的适配喽。
我们前面在AppDelegate.cpp中应该看到过如下的代码,这个就是默认的几个尺寸,当然可以修改哈。
static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768);
static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536);
我们在
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if (!glview) {
glview = GLViewImpl::create("jfcz");
glview->setFrameSize(384, 683);
director->setOpenGLView(glview);
}
glview->setDesignResolutionSize(720, 1280, ResolutionPolicy::EXACT_FIT);
// turn on display FPS
director->setDisplayStats(false);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0 / 60);
register_all_packages();
type = Application::getCurrentLanguage();
if (type == LanguageType::CHINESE) {
FileUtils::getInstance()->addSearchPath("ch");
FileUtils::getInstance()->addSearchPath("en");
}
else {
FileUtils::getInstance()->addSearchPath("en");
FileUtils::getInstance()->addSearchPath("ch");
}
// create a scene. it's an autorelease object
auto scene = WelcomeScene::createScene();
// run
director->runWithScene(scene);
这个方法中设置一下需要的尺寸就可以啦,后面会具体介绍相关的使用方法
四、既然诚心诚意的做了,那么就完美的做下去
做一个游戏,从添加一幅图开始——游戏图片资源添加,既然已经决定了,那么就继续做下去吧,小伙伴们一起加油ヾ(◍°∇°◍)ノ゙
图片已经添加成功了,游戏还会远么~~