quick-quick cocos2d x-x想制作一个退出游戏的按钮,怎么实现

Cocos2d-x V3.2+Cocos Studio1.6 实现一个简单的uibutton点击功能
好久没写博客了
这几天在学习cocos studio,这个软件能够非常方便的设计游戏的一些界面,并导入到cocos2dx中,今天就用按钮来做个例子
首先我们打开Cocos Studio1.6,选择UIEditor,进去后我们发现有很多的示例,我们就悬着一个叫demologin的示例
选择好后我们发现如下图,简单介绍了下功能
这里我们选中那个login按钮,发现这个按钮的实例名叫login_Button,这个名字我们等会代码里要用,然后我们保存一下
在保存路径下找到这个工程文件夹,打开
有这些文件,我们把Json和Resources文件夹下的资源复制到cocos2dx中的资源文件夹中
这个json文件就是记录了这个widget的各种数据,cocos2dx会解析这些数据然后还原这个界面,感兴趣的可以打开研究下
好了到了代码阶段,我就简单的创建了个hellowZ喎"/kf/ware/vc/" target="_blank" class="keylink">vcmxko6y1q8rHztLDx9KqsNHI/bj2zeKyv7XEuaSzzL/itbzI69XiuPa94r72t72wuLKi0v3TwzxpbWcgc3JjPQ=="/uploadfile/Collfiles/40.png" alt="\">并在头文件中包含
好了,下面就是代码了
//HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
//导入这三个头文件
//注意在这个工程的属性->C\C++ ->常规->附加包含目录 中添加这三个文件的路径
#include "cocostudio/CocoStudio.h"
#include "extensions/cocos-ext.h"
#include "ui/CocosGUI.h"
class HelloWorld : public cocos2d::Layer
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender,cocos2d::ui::TouchEventType type);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
#endif // __HELLOWORLD_SCENE_H__
//HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
//这个是命名空间
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
// on "init" you need to initialize your instance
bool HelloWorld::init()
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
//获取我们的Cocos Studio
//这里我们创建了一个widget,这里说明下貌似cocostudio有自己的一套代码风格和api
//有些功能能和cocos2dx混合着用
//这里的widget有点类似一个layer,反正我是这样理解的
ui::Widget * pNode=cocostudio::GUIReader::getInstance()->widgetFromJsonFile("DemoLogin.json");
this->addChild(pNode);
//我们从widget中找到那个button的名字然后实例出来(跟android有点像)
ui::Button * button=(ui::Button *)ui::Helper::seekWidgetByName(pNode,"login_Button");
//给这个按钮增加一个touch的侦听(这边就和cocos2dx有不同了)
button->addTouchEventListener(this,toucheventselector(HelloWorld::menuCloseCallback));
//这里是侦听回调函数
void HelloWorld::menuCloseCallback(cocos2d::Ref* pSender,ui::TouchEventType type)
//cocostudio的侦听,你可以根据type的类型来执行相应的代码
//这里用switch来判断,非常的简洁明了
switch (type)
case ui::TouchEventType::TOUCH_EVENT_BEGAN:
log("touch began");
case ui::TouchEventType::TOUCH_EVENT_MOVED:
log("touch moved");
case ui::TouchEventType::TOUCH_EVENT_ENDED:
log("touch ended");
case ui::TouchEventType::TOUCH_EVENT_CANCELED:
log("touch canceled");
点击按钮测试下
好的,完成
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'cocos2d-x 游戏暂停界面,监听home键,返回键,Menu键 解决方案
游戏暂停界面:
cocos2d-x中游戏暂停界面提供的思路是用pushScene()和popScne(),即推进和弹出场景,当游戏暂停时,推进(pushScene())暂停场景,之前运行的场景将会自动暂停,然后我们可以在暂停场景中操作,如Resume,ReStart,Quit等,当我们不再需要暂停场景时,可以popScene()将暂停场景弹出。(场景就像一张纸,我们推进一个场景,相当于在这张纸上再盖上一张,弹出场景相当于将最表面的那张纸拿掉)。
推进暂停场景的相关代码如下:
CCRenderTexture *renderTexture = CCRenderTexture::create(800,600); &
& & renderTexture-&begin(); &
& & this-&getParent()-&visit(); &
& & renderTexture-&end(); &//这里实际是通过CCRenderTexture保存当前界面(相当于截屏),然后传递给暂停界面,当成背景精灵 &
& & CCDirector::sharedDirector()-&pushScene(PauseLayer::scene(renderTexture,true)); &
暂停场景PauseLayer的相关代码如下:
CCScene* PauseLayer::scene(CCRenderTexture* sqr,bool isFlip){ &
& & CCScene *m_scene = CCScene::create(); &
& & CCSprite *_spr = CCSprite::createWithTexture(sqr-&getSprite()-&getTexture()); &
& & _spr-&setPosition(ccp(400, 300)); &
& & _spr-&setFlipY(isFlip); &
& & _spr-&setColor(ccGRAY); &
& & m_scene-&addChild(_spr); &
& & // 'layer' is an autorelease object &
& & PauseLayer* layerr = PauseLayer::create(); &
& & // add layer as a child to scene &
& & m_scene-&addChild(layerr); &
& & // return the scene &
& & return m_ &
监听返回键和Menu键:
1.继承CCKeypadDelegate
2.实现两个虚函数
virtual void
keyBackClicked ()
virtual void
keyMenuClicked ()
如查要实现监听的对象是CCLayer或者继承CCLayer的,则只需做第二步及在初始化中setKeypadEnabled(true);
因为CCLayer本身继承了CCKeypadDelegate,如下图所示
class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate &
监听home键:
在cocos2d-x中我现在还没找到明确的监听home键的方案,但可以用替代方案。
不知你们有没有发现在AppDelegate.cpp里的两个方法:
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too &
void AppDelegate::applicationDidEnterBackground() { &
& & CCDirector::sharedDirector()-&stopAnimation(); &
& & // if you use SimpleAudioEngine, it must be pause &
& & // SimpleAudioEngine::sharedEngine()-&pauseBackgroundMusic(); &
// this function will be called when the app is active again &
void AppDelegate::applicationWillEnterForeground() { &
& & CCDirector::sharedDirector()-&startAnimation(); &
& & // if you use SimpleAudioEngine, it must resume here &
& & // SimpleAudioEngine::sharedEngine()-&resumeBackgroundMusic(); &
注意这两个方法的英文解释,实际上这两个方法就是判断程序是否被切换或者说是否被扔至后台工作。因为在手机上按home键,实际就是切换将程序推至后台。So,我们就能在这两个方法做文章了。
相关代码如下:
void AppDelegate::applicationDidEnterBackground() &
& & CCDirector::sharedDirector()-&stopAnimation(); &
& & SimpleAudioEngine::sharedEngine()-&pauseBackgroundMusic(); &
& & Global* sh = Global::toIns(); &
& & CCRenderTexture* renderT &
& & switch(sh-&targetScene){ &
& & case TargetSceneFirstScene: &
& & case TargetSceneSecondScene: &
& & & & renderTexture = CCRenderTexture::create(800,600); &
& & & & renderTexture-&begin(); &
& & & & sh-&battleLayer-&visit(); &
& & & & renderTexture-&end(); &
& & & & CCDirector::sharedDirector()-&pushScene(PauseLayer::scene(renderTexture,false)); &
& & case TargetSceneInvalid: &
& & default: &
// this function will be called when the app is active again &
void AppDelegate::applicationWillEnterForeground() &
& & CCDirector::sharedDirector()-&startAnimation(); &
& & SimpleAudioEngine::sharedEngine()-&resumeBackgroundMusic(); &
在上面的代码中,我做的是,当程序InActive(推至后台)时,推进暂停界面
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'quick-cocos2d-x 游戏打包环境搭建_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
quick-cocos2d-x 游戏打包环境搭建
上传于||暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩7页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢

我要回帖

更多关于 android实现按钮退出 的文章

 

随机推荐