- C++語言基礎(chǔ)
- 創(chuàng)建物理世界
- 添加邊框
在游戲世界中酪我,任務(wù)跳起來后會掉下來翘县,這個涉及到世界的重力加速度杀饵,不過我們首先要說一下C++語言的基礎(chǔ)系吭!
1.C++作為面向?qū)ο笳Z言編程脉漏,由頭文件.h和源文件.cpp組成苞冯,其中頭文件負(fù)責(zé)對象的聲明,方法的聲明侧巨,源文件負(fù)責(zé)對象的具體實現(xiàn)舅锄!
如果要使用類里面的對象,使用.字符司忱,則Class.object
如果要使用類里面的方法皇忿,使用->字符畴蹭,Director::getInstance()->getVisibleSize()
而::是類作用域運算符,看定義
<pre>#pragma once
include <cocos2d.h>
include <iostream>
USING_NS_CC;
class EdgeBox :public Node//創(chuàng)建EdgeBox繼承自Node
{
public:
virtual bool init();//聲明初始化函數(shù)
CREATE_FUNC(EdgeBox);//創(chuàng)建類
private://定義私有變量
};
</pre><pre>#include "EdgeBox.h"
//負(fù)責(zé)EdgeBox的實現(xiàn)
bool EdgeBox::init() {
Node::init();//調(diào)用Node父類的init方法
//設(shè)置錨點
Size visiableSize = Director::getInstance()->getVisibleSize();
setAnchorPoint(cocos2d::Vec2(0.5,0.5));
setContentSize(visiableSize);
setPhysicsBody(PhysicsBody::createEdgeBox(visiableSize));
//配置邊界框
return true;
}
</pre>
2.創(chuàng)建物理世界
在Cocos2D的代碼構(gòu)成主要由下圖組成
而我們知道Cocos2D項目的入口是從AppDelegate進(jìn)入的鳍烁,而里面有三個比較重要的方法
- applicationDidFinishLaunching()
應(yīng)用程序啟動后調(diào)用叨襟。默認(rèn)的實現(xiàn)中已經(jīng)包含了游戲啟動的必要準(zhǔn)備。
因為我們的游戲是豎屏的幔荒,所以需要添加屏幕的寬高設(shè)置
<pre>glview->setDesignResolutionSize(480, 800, ResolutionPolicy::EXACT_FIT);
if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
glview->setFrameSize(360, 600);
endif
</pre>
<pre>
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
-#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect("noonedies", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
-#else
glview = GLViewImpl::create("noonedies");
-#endif
director->setOpenGLView(glview);
}
glview->setDesignResolutionSize(480, 800, ResolutionPolicy::EXACT_FIT);
-#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
glview->setFrameSize(360, 600);
endif
// 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.0f / 60);
// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();
// run
director->runWithScene(scene);
return true;
}
</pre>
-
applicationDidEnterBackground()
程序進(jìn)入后臺時調(diào)用糊闽。接打電話時。暫停音樂和動作.
<pre>// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();// if you use SimpleAudioEngine, it must be paused
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}</pre> -
applicationDidEnterForeground()
與applicationDidEnterBG()成對出現(xiàn)爹梁,應(yīng)用程序回到前臺時使用墓怀。
<pre>
// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();// if you use SimpleAudioEngine, it must be paused
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}</pre>
<h4>auto scene = HelloWorld::createScene();
director->runWithScene(scene);</h4>
這句就是程序的場景入口!
而我們進(jìn)入了HelloScene場景后卫键,在創(chuàng)建場景的createScene()方法內(nèi)將Scene::create()改為 Scene::createWithPhysics(),則創(chuàng)建了物理世界
scene->getPhysicsWorld()->setGravity(Vec2(0, -1000));
設(shè)置了重力加速度是1000
之后我們要設(shè)置邊界框J洹莉炉!
3.添加邊界框場景
首先聲明一個C++頭文件,命名為EdgeBox碴犬,繼承于Node
在頭文件里面絮宁,新建一個類EdgeBox,聲明初始化方法并調(diào)用CREATE_FUNC(EdgeBox)創(chuàng)建
<pre>#pragma once
#include <cocos2d.h>
#include <iostream>
USING_NS_CC;
class EdgeBox :public Node
{
public:
virtual bool init();
CREATE_FUNC(EdgeBox);
};
</pre>
而設(shè)置邊界框服协,需要先獲取屏幕的寬高绍昂,并在物理世界配置的邊界框
<pre>
include "EdgeBox.h"
bool EdgeBox::init() {
Node::init();
Size visiableSize = Director::getInstance()->getVisibleSize();//獲取屏幕寬高
setAnchorPoint(cocos2d::Vec2(0.5,0.5)); //設(shè)置錨點
setContentSize(visiableSize); //設(shè)置內(nèi)容寬高
setPhysicsBody(PhysicsBody::createEdgeBox(visiableSize));//配置邊界框
return true;
}
</pre>
最后將邊界框添加到HelloWorld場景中
<pre>
auto edge = EdgeBox::create();
edge->setContentSize(visiableSize);
edge->setPosition(visiableSize.width / 2, visiableSize.height / 2+ positionY);
edge->setContentSize(visiableSize);
addChild(edge);
</pre>
大功告成!
項目git地址:https://github.com/marco115/NoOneDies.git
對文章有什么優(yōu)化改進(jìn)的地方偿荷,請留言窘游!謝謝大家