Cocos2d-x AppDelegate

//AppDelegate.h

ifndef  _APP_DELEGATE_H_

#define  _APP_DELEGATE_H_

#include "cocos2d.h"

/**

@brief    The cocos2d Application.

The reason for implement as private inheritance is to hide some interface call by Director.

*/

class  AppDelegate : private cocos2d::Application

{

public:

AppDelegate();

virtual ~AppDelegate();

virtual void initGLContextAttrs();

/**

@brief    Implement Director and Scene init code here.

@return true    Initialize success, app continue.

@return false   Initialize failed, app terminate.

*/

virtual bool applicationDidFinishLaunching();

/**

@brief  The function be called when the application enter background

@param  the pointer of the application

*/

virtual void applicationDidEnterBackground();

/**

@brief  The function be called when the application enter foreground

@param  the pointer of the application

*/

virtual void applicationWillEnterForeground();

};

#endif // _APP_DELEGATE_H_

//AppDelegate.cpp

#include "HNGame.h"

#include "Game/Game/GameManagerBase.h"

USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate()

{

}

//if you want a different context,just modify the value of glContextAttrs

//it will takes effect on all platforms

void AppDelegate::initGLContextAttrs()

{

//set OpenGL context attributions,now can only set six attributions:

//red,green,blue,alpha,depth,stencil

GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};

GLView::setGLContextAttrs(glContextAttrs);

}

// If you want to use packages manager to install more packages,

// don‘t modify or remove this function

static int register_all_packages()

{

return 0; //flag for packages manager

}

//当应用程序启动时执行,游戏程序启动入口

//在这里我们启动了第一个scene(场景)

//在具体游戏中通常在这里启动loading界面

//你的游戏从这里开始!

bool AppDelegate::applicationDidFinishLaunching() {

// 初始化 director

auto director = Director::getInstance();//初始化Director

auto glview = director->getOpenGLView(); //获得GLView,也就是游戏窗口

if(!glview) {

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)

//如果当前是以上平台,就创建一个width为960,height为540的窗口

glview = GLViewImpl::createWithRect("GameBase", Rect(0, 0, 960,540));

#else

//其他平台,则使用默认设置(在ios上是全屏窗口,其他平台不太清楚,也有可能是默认设置了一个窗口大小)

glview = GLViewImpl::create(("GameBase");

#endif

director->setOpenGLView(glview);//director获得当前新建的窗口

}

// 在屏幕上显示FPS数

// 开发阶段建议开启这个设置,可以通过这个对自己游戏性能有个大体了解

// 等游戏正式发布时关闭这个设置

director->setDisplayStats(true); //显示帧率信息

// 设置 FPS数 默认值为 1.0/60

director->setAnimationInterval(1.0 / 60); //设置动画帧率,也就是界面刷新帧率咯

register_all_packages(); //使用包管理器

// 创建一个HelloWorld的scene.这个是自动回收的对象

auto scene = HelloWorld::createScene();

//Scene *pScene = HNGame::scene();

// 告诉director运行HelloWorld的scene

director->runWithScene(scene);

//director->runWithScene(pScene);

return true;

}

//static GLViewImpl* createWithRect(const std::string& viewName, Rect size, float frameZoomFactor = 1.0f);//窗口的名字,大小,和缩放比例

// This function will be called when the app is inactive. When comes a phone call,it‘s be invoked too

void AppDelegate::applicationDidEnterBackground() {

Director::getInstance()->stopAnimation();

if (GameManagerBase::pInstanceBase())

{

GameManagerBase::InstanceBase().applicationDidEnterBackground();

}

// if you use SimpleAudioEngine, it must be pause

// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();

}

// this function will be called when the app is active again

void AppDelegate::applicationWillEnterForeground() {

Director::getInstance()->startAnimation();

if (GameManagerBase::pInstanceBase())

{

GameManagerBase::InstanceBase().applicationWillEnterForeground();

}

// if you use SimpleAudioEngine, it must resume here

// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();

}

时间: 2025-01-16 14:28:30

Cocos2d-x AppDelegate的相关文章

cocos2dx-安卓多分辨率适配方案

#include "AppDelegate.h" #include "HelloWorldScene.h" #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // Layer #include "OperateLayer.h" #include "LoginMainLayer.h" #include "OperateUILayer.h" #include "G

cocos2dx3.2helloword分析

废话不说 直接看注释 <span style="font-size:24px;color:#ff0000;">AppDelegate.cpp</span> #include "AppDelegate.h" #include "HelloWorldScene.h" //USING_NS_CC == using namespace cocos2d USING_NS_CC;//using namespace cocos2d //

Cocos2d-x实例:设置背景音乐与音效- AppDelegate实现

为了进一步了解背景音乐和音效播放的,我们通过一个实例给大家介绍一下.如下图所示有两个场景:HelloWorld和Setting.在HelloWorld场景点击"游戏设置"菜单可以切换到Setting场景,在Setting场景中可以设置是否播放背景音乐和音效,设置完成后点击"OK"菜单可以返回到HelloWorld场景. 我们需要在AppDelegate中实现背景音乐播放暂停与继续函数,AppDelegate.h文件代码如下: #ifndef _APP_DELEGAT

[cocos2dx笔记008]cocos2d 用luabridge手动绑定类

基于cocos2dx 2.2.2版本.这几天使用了cocostudio实现了,动画,骨骼动画,UI编辑,粒子效果,虽然有些不足,但已经算是非常好了.今天尝试用lua,这个非常简单,创建的时候,设置语言为lua,那就可以创建lua工程. 在cocos2d-x-2.2.2\tools\project-creator下运行: python create_project.py -project test_lua -package com.android.zdhsoft -language lua xco

cocos2d jsb 打包 Android APK

1.首先要会普通的cpp 打包成Android APK 以下所说的是在cocos2d-x 2.2.2 或者 2.3 版本中.本文在Eclipse总用ndk编译cocos2d-x. 老生常谈cocos2d-x JSB不是简单的js代码,涉及到C++代码,如果是Android的话又涉及到Java代码,有点复杂,如果搞过Android下的Jni的话会熟悉些.可以看下这篇文章:<Android Jni 例子 Hello JNI,ndk> Android为了提高开发者开发应用的速度,降低难度,选择了Ja

cocos2D 虚拟摇杆Joystick功能实现

@implementation InputLayer        - (id)init    {        if(self = [super init])        {            winSize = [[CCDirector sharedDirector] winSize];            [self addJoystick];            [self addFireButton];            [self scheduleUpdate];   

quick cocos2d x 手机(Android端)启动过程学习

简要学习下quick cocos2d x 在安卓端启动的过程. 首先需要了解一点:quick cocos2d x是依托于Android的activity和GLSurfaceView(继承自SurfaceView)的环境来显示quick层的游戏界面. (1)首先quick类的android游戏从AndroidManifest.xml文件指定的activity(假设AC)启动. (2)AC继承父类的Cocos2dxActivity. (3)调用静态初始化块,加载cocos2dx的动态库.也就是一些C

cocos2d之创建自己的场景类

|   版权声明:本文为博主原创文章,未经博主允许不得转载. 1. 首先创建.h的头文件,然后在将一些图片声音素材加到resource文件夹内,最后在创建.cpp文件:         .h头文件中创建一个类,此类一般继承自Layer,首先有一个静态的创建场景的方法,然后是一个初始化方法,还有一个CREATE_FUNC宏: .cpp中就是对.h中class中定义的方法来进行实现 效果图: 代码: MyGameScene.h: 1 #ifndef _MYGAME_SCENE_H_ 2 #defin

cocos2D(三)---- 第一cocos2d的程序代码分析

在第一讲中已经新建了第一个cocos2d程序,执行效果例如以下: 在这讲中我们来分析下里面的代码,了解cocos2d的工作原理,看看屏幕上的这个"Hello World"是怎样显示出来的. 这是HelloWorld项目的代码结构: 以下,我们開始分析项目中的这些源文件: 从程序的入口点開始 这么多源文件在这里,到底先看哪个呢?有些人可能会先挑内容少的来看,认为这样就能够轻松解决掉一个源文件了.事实上这是不正确的,这样看起来更加是一头雾水,根本搞不清楚每一个源文件之间的联系.正确的做法应