Cocos2d-x 基础元素
看過(guò)本章,然后實(shí)踐之后,應(yīng)該會(huì)掌握以下的認(rèn)識(shí):
?1、Cocos2d-x引擎的基本運(yùn)轉(zhuǎn)過(guò)程
2、Cocos2d-x引擎的一些初始設(shè)置
3、對(duì)導(dǎo)演及圖層及現(xiàn)實(shí)對(duì)象的認(rèn)識(shí)
4、如何定義自己的顯示對(duì)象
1.引擎的運(yùn)轉(zhuǎn)
游戲設(shè)計(jì)的問(wèn)題: 在游戲設(shè)計(jì)之初,我們就需要設(shè)置游戲是橫屏的還是豎屏的。 展示圖像的清晰度是多少? 幀數(shù)? 適配哪些屏幕?
在處理以上問(wèn)題之前,我們需要了解引擎的運(yùn)轉(zhuǎn)是怎么樣的? (以后補(bǔ)充)
關(guān)于XCode設(shè)置游戲屏幕豎屏的方式:
2.顯示對(duì)象
每一個(gè)節(jié)點(diǎn)對(duì)象都繼承一個(gè)node節(jié)點(diǎn)對(duì)象。包含了設(shè)置節(jié)點(diǎn)對(duì)象的位置,旋轉(zhuǎn)角度,縮放,錨點(diǎn)以及管理子對(duì)象的功能。
導(dǎo)演Director是單例類,是cocos2d的引擎的核心 Scene的切換由導(dǎo)演來(lái)處理。 在Cocos2d工程中的Resources文件夾下是放置圖片、字體等資源的地方。場(chǎng)景:scene,包含多個(gè)層 層:layer,包含多個(gè)精靈 精靈:Sprite
先刪除掉官方的部分代碼,保留下面的部分
bool HelloWorld::init() {//// 1. super init firstif ( !Layer::init() ){return false;}Size visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();return true; }然后把,需要的圖片文件拷貝進(jìn)工程中的Resources文件夾里
bool HelloWorld::init() {//// 1. super init firstif ( !Layer::init() ){return false;}Size visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();//1、創(chuàng)建一個(gè)精靈對(duì)象,并且綁定到資源圖片文件auto logo = Sprite::create("logo.png");//2、設(shè)置精靈的大致位置logo->setPosition(visibleSize/2);//3、添加到場(chǎng)景中addChild(logo);return true; }其實(shí)我們不這么添加顯示圖片精靈,我們可以這樣:
bool HelloWorld::init() {//// 1. super init firstif ( !Layer::init() ){return false;}Size visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();//1、先通過(guò)導(dǎo)演類獲取紋理對(duì)象,然后通過(guò)紋理對(duì)象的添加圖片方法auto img = Director::getInstance()->getTextureCache()->addImage("logo2.png");//2、獲取紋理圖片的尺寸auto logoSize = img->getContentSize();CCLOG("logo size:%f %f",logoSize.width,logoSize.height);//3、通過(guò)精靈類的加載紋理的方法創(chuàng)建精靈對(duì)象auto logo = Sprite::createWithTexture(img);logo->setPosition(visibleSize/2);//4、添加到場(chǎng)景中addChild(logo);return true; }下面通過(guò)layer圖層對(duì)象調(diào)用添加加載好的三張圖片
// on "init" you need to initialize your instance bool HelloWorld::init() {//// 1. super init firstif ( !Layer::init() ){return false;}Size visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();auto layer1 = Layer::create();auto layer2 = Layer::create();auto layer3 = Layer::create();auto img1 = Sprite::create("layer1.png");auto img2 = Sprite::create("layer2.png");auto img3 = Sprite::create("layer3.png");img1->setAnchorPoint(Vec2(0, 0));img2->setAnchorPoint(Vec2(0, 0));img3->setAnchorPoint(Vec2(0, 0));layer1->addChild(img1);layer2->addChild(img2);layer3->addChild(img3);addChild(layer1);addChild(layer2);addChild(layer3);layer1->setPosition(Vec2(0, 20));layer2->setPosition(Vec2(50, 60));layer3->setPosition(Vec2(100, 100));return true; }這里有一個(gè)問(wèn)題:6s的蘋(píng)果手機(jī)可能坐標(biāo)系統(tǒng)變化了。 最后我們通過(guò)一種方式切換場(chǎng)景:
// on "init" you need to initialize your instance bool HelloWorld::init() {//// 1. super init firstif ( !Layer::init() ){return false;}Size visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();auto layer1 = Layer::create();auto layer2 = Layer::create();auto layer3 = Layer::create();auto img1 = Sprite::create("layer1.png");auto img2 = Sprite::create("layer2.png");auto img3 = Sprite::create("layer3.png");img1->setAnchorPoint(Vec2(0, 0));img2->setAnchorPoint(Vec2(0, 0));img3->setAnchorPoint(Vec2(0, 0));layer1->addChild(img1);layer2->addChild(img2);layer3->addChild(img3);addChild(layer1);addChild(layer2);addChild(layer3);layer1->setPosition(Vec2(0, 20));layer2->setPosition(Vec2(50, 60));layer3->setPosition(Vec2(100, 100));//需要計(jì)時(shí)器時(shí)間表切換layerschedule([visibleSize,this](float f){auto scene2 = Scene::create();auto scene2Layer=Layer::create();scene2->addChild(scene2Layer);auto logo= Sprite::create("logo2.png");logo->setPosition(visibleSize/2);scene2Layer->addChild(logo);Director::getInstance()->replaceScene(scene2);}, 3, "test");return true; }3.自定義顯示對(duì)象
首先定義一個(gè)繼承于Node的最基本的顯示對(duì)象。 首先創(chuàng)建一個(gè)C++ File 的C++類,取名LogoNode,然后就有了LogoNode.h和LogoNode.cpp文件,因?yàn)樾枰褂胏ocos引擎,所以要在h文件中導(dǎo)入 include “cocos2d.h”,并且要導(dǎo)入它的命名空間,直接另一行寫(xiě):USINGNSCC,
然后在LogoNode.cpp中完成頭文件中public聲明的方法
#include "LogoNode.hpp"// 1、構(gòu)造函數(shù)主要目的是對(duì)剛剛兩個(gè)成員變量進(jìn)行初始化 LogoNode::LogoNode():_logo(nullptr),_cocoslogo(nullptr) {} // 2、虛函數(shù)的析構(gòu)函數(shù)則沒(méi)有實(shí)現(xiàn) LogoNode::~LogoNode(){}// 3、然后需要一個(gè)虛構(gòu)函數(shù)的并且返回的是bool值得初始化函數(shù) bool LogoNode::init(){ // 4、如果創(chuàng)建失敗了,就停止創(chuàng)建if (!Node::init()) {return false} // 5、創(chuàng)建Logo_logo = Sprite::create("logo.png");_cocoslogl = Sprite::create("logo2.png");// 6、添加到顯示列表,也就是當(dāng)前的node中addChild(_logo);addChild(_cocoslogl);_cocoslogl->setVisible(false);// 添加一個(gè)計(jì)時(shí)器schedule([this](float f){_logo->setVisible(!_logo->isVisible());_cocoslogl->setVisible(!_logo->isVisible());}, 1, "test");return true; }然后新建一個(gè)場(chǎng)景類,GameScene.hpp和GameScene.cpp文件,
GameScene.hpp代碼如下:#ifndef GameScene_hpp#define GameScene_hpp#include <stdio.h>#include "cocos2d.h"USING_NS_CC;class GameScene:public Layer{ public:GameScene();virtual ~GameScene();virtual bool init();CREATE_FUNC(GameScene);static Scene* createScene(); };#endif /* GameScene_hpp */GameScene.cpp代碼如下:
#include "GameScene.hpp"#include "LogoNode.hpp" GameScene::GameScene() {} GameScene::~GameScene() {} bool GameScene::init() {if (!Layer::init()) {return false;}auto logo = LogoNode::create();addChild(logo);//將logo設(shè)置在居中的位置logo->setPosition(Director::getInstance()->getVisibleSize()/2);return true; } Scene* GameScene::createScene() {//創(chuàng)建一個(gè)空的場(chǎng)景auto scene = Scene::create();auto layer = GameScene::create();scene->addChild(layer);return scene; }最后我們回到AppDelegate.cpp文件中: 首先添加
#include "GameScene.hpp"然后將后面之前的
// create a scene. it's an autorelease object auto scene = HelloWorld::createScene();改為
auto scene = GameScene::createScene();總結(jié)
以上是生活随笔為你收集整理的Cocos2d-x 基础元素的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java 堆与栈的区别
- 下一篇: Android加载/处理超大图片神器!S