[Cocos2Dx-一个都不能死]2.给游戏添加边框

  1. C++语言基础
  2. 创建物理世界
  3. 添加边框

在游戏世界中,任务跳起来后会掉下来,这个涉及到世界的重力加速度,不过我们首先要说一下C++语言的基础!
1.C++作为面向对象语言编程,由头文件.h和源文件.cpp组成,其中头文件负责对象的声明,方法的声明,源文件负责对象的具体实现!
如果要使用类里面的对象,使用.字符,则Class.object
如果要使用类里面的方法,使用->字符,Director::getInstance()->getVisibleSize()
而::是类作用域运算符,看定义

#pragma once

include

include

USING_NS_CC;
class EdgeBox :public Node//创建EdgeBox继承自Node
{
public:
virtual bool init();//声明初始化函数
CREATE_FUNC(EdgeBox);//创建类
private://定义私有变量
};

#include "EdgeBox.h"
//负责EdgeBox的实现
bool EdgeBox::init() {
Node::init();//调用Node父类的init方法

//设置锚点
Size visiableSize = Director::getInstance()->getVisibleSize();
setAnchorPoint(cocos2d::Vec2(0.5,0.5));
setContentSize(visiableSize);
setPhysicsBody(PhysicsBody::createEdgeBox(visiableSize));
//配置边界框
return true;

}

2.创建物理世界
在Cocos2D的代码构成主要由下图组成


Cocos2D

而我们知道Cocos2D项目的入口是从AppDelegate进入的,而里面有三个比较重要的方法

  • applicationDidFinishLaunching()
    应用程序启动后调用。默认的实现中已经包含了游戏启动的必要准备。
    因为我们的游戏是竖屏的,所以需要添加屏幕的宽高设置
    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



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;

}

  • applicationDidEnterBackground()
    程序进入后台时调用。接打电话时。暂停音乐和动作.

    // 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();
    }

  • applicationDidEnterForeground()
    与applicationDidEnterBG()成对出现,应用程序回到前台时使用。


    // 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();
    }


    auto scene = HelloWorld::createScene();
    director->runWithScene(scene);


    这句就是程序的场景入口!

而我们进入了HelloScene场景后,在创建场景的createScene()方法内将Scene::create()改为 Scene::createWithPhysics(),则创建了物理世界
scene->getPhysicsWorld()->setGravity(Vec2(0, -1000));
设置了重力加速度是1000
之后我们要设置边界框!!

3.添加边界框场景
首先声明一个C++头文件,命名为EdgeBox,继承于Node
在头文件里面,新建一个类EdgeBox,声明初始化方法并调用CREATE_FUNC(EdgeBox)创建

#pragma once
#include #include
USING_NS_CC;
class EdgeBox :public Node
{
public:
virtual bool init();
CREATE_FUNC(EdgeBox);
};

而设置边界框,需要先获取屏幕的宽高,并在物理世界配置的边界框

include "EdgeBox.h"

bool EdgeBox::init() {
Node::init();
Size visiableSize = Director::getInstance()->getVisibleSize();//获取屏幕宽高
setAnchorPoint(cocos2d::Vec2(0.5,0.5)); //设置锚点
setContentSize(visiableSize); //设置内容宽高
setPhysicsBody(PhysicsBody::createEdgeBox(visiableSize));//配置边界框
return true;
}


最后将边界框添加到HelloWorld场景中

auto edge = EdgeBox::create();
edge->setContentSize(visiableSize);
edge->setPosition(visiableSize.width / 2, visiableSize.height / 2+ positionY);
edge->setContentSize(visiableSize);
addChild(edge);

大功告成!

项目git地址:https://github.com/marco115/NoOneDies.git
对文章有什么优化改进的地方,请留言!谢谢大家

你可能感兴趣的:([Cocos2Dx-一个都不能死]2.给游戏添加边框)