Cocos2D-X2.2.3学习笔记9(处理重力感应事件,移植到Android加入两次返回退出游戏效果)
这节我们来学习Cocos2d-x的最后一节。怎样处理重力感应事件。移植到Android后加入再按一次返回键退出游戏等。我这里用的Android。IOS不会也没设备呃
效果图不好弄,由于是要移植到真机上的。
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"
using namespace cocos2d;
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();
//重力感应事件
virtual void didAccelerate(CCAcceleration* pAccelerationValue);
//返回button
virtual void keyBackClicked();
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
}; #endif // __HELLOWORLD_SCENE_H__
这里重写了两个方法
我们都知道CCLayer是继承了重力感应事件和触屏事件,button事件的
#include "HelloWorldScene.h" #define FIX_POS(_pos, _min, _max) \
if (_pos < _min) \
_pos = _min; \
else if (_pos > _max) \
_pos = _max; \ USING_NS_CC; CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); // 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
this->setAccelerometerEnabled(true);
this->setKeypadEnabled(true); CCSize visibleSize= CCDirector::sharedDirector()->getVisibleSize();
CCLabelTTF *lable= CCLabelTTF::create("AccelerometerTest","Arial",34);
lable->setPosition(ccp(visibleSize.width/2,visibleSize.height-50));
this->addChild(lable,0,0); CCSprite *pSprite= CCSprite::create("ball.png");
pSprite->setPosition(ccp(visibleSize.width/2,visibleSize.height/2));
this->addChild(pSprite,0,1);
return true;
}
void HelloWorld::didAccelerate(CCAcceleration* pAccelerationValue)
{
CCObject *pObjectlable= this->getChildByTag(0);
if (pObjectlable==NULL)
{
return;
}
CCLabelTTF *lable=(CCLabelTTF*)pObjectlable;
std::ostringstream strstream;
strstream<<"X:"<<pAccelerationValue->x<<" Y:"<<pAccelerationValue->y<<" Z:"<<pAccelerationValue->z;
std::string str=strstream.str();
lable->setString(str.c_str()); //改变小球位置
CCObject *pObjectSprite= this->getChildByTag(1);
if (pObjectSprite==NULL)
{
return;
}
CCSprite *pSprite=(CCSprite*)pObjectSprite;
CCSize pSpriteSize= pSprite->getContentSize(); CCPoint ptNow = pSprite->getPosition();
CCPoint ptTemp=CCDirector::sharedDirector()->convertToUI(ptNow);
ptTemp.x += pAccelerationValue->x * 9.81f;
ptTemp.y -= pAccelerationValue->y * 9.81f;
CCPoint ptNext = CCDirector::sharedDirector()->convertToGL(ptTemp); CCSize visibleSize= CCDirector::sharedDirector()->getVisibleSize();
FIX_POS(ptNext.x, (pSpriteSize.width / 2.0), (visibleSize.width - pSpriteSize.width / 2.0));
FIX_POS(ptNext.y, (pSpriteSize.height / 2.0), (visibleSize.height - pSpriteSize.height / 2.0));
pSprite->setPosition(ptNext); }
void HelloWorld::keyBackClicked()
{
}
源文件代码如上,
init中我们创建了一个lable和小球的精灵
通过setAccelerometerEnabled启用重力感应事件
这里能够看下源代码,比触屏事件简单多了。
然后重写重力感应事件。这里我再事件中改动了CCLableTTF的文本。
改变小球的位置
改动文本就不多说了,非常easy,我们主要来看看怎样改变小球位置的
首先我们获得小球精灵
得到精灵位置
转换为UIKIT
这里*9.81f我也不知道什么意思,TestCpp这么写我也就这么写了
为了小球不超出手机屏幕,所以我们写了一个宏 FIX_POS
这里看看就懂了
OK,
我们移植到Android,看看效果,怎么移植。第一节专门介绍了的
效果不错,可是我们按返回键它没有退出游戏,没不论什么反应。
我们在src下的org.cocos2dx.lib中找到Cocos2dxGLSurfaceView.java打开
找到
@Override
public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {
switch (pKeyCode) {
case KeyEvent.KEYCODE_BACK:
return false;//这里是自己新增的, 返回false
case KeyEvent.KEYCODE_MENU:
this.queueEvent(new Runnable() {
@Override
public void run() {
Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleKeyDown(pKeyCode);
}
});
return true;
default:
return super.onKeyDown(pKeyCode, pKeyEvent);
}
}
然后我们在自己的java文件里重写onKeyDown,
详细代码例如以下
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package com.huc.test; import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView; import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast; public class test20140521 extends Cocos2dxActivity{
private long mExitTime;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((System.currentTimeMillis() - mExitTime) > 2000) {// 假设两次按键时间间隔大于2000毫秒,则不退出
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();// 更新mExitTime } else {
System.exit(0);// 否则退出程序 }
return true;
}
return super.onKeyDown(keyCode, event);
}
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
// test20140521 should create stencil buffer
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8); return glSurfaceView;
} static {
System.loadLibrary("cocos2dcpp");
}
}
OK。试试吧。。。
Cocos2D-X2.2.3学习笔记9(处理重力感应事件,移植到Android加入两次返回退出游戏效果)的更多相关文章
- jQuery学习笔记之DOM操作、事件绑定(2)
jQuery学习笔记之DOM操作.事件绑定(2) --------------------学习目录------------------------ 4.DOM操作 5.事件绑定 源码地址: https ...
- Cocos2d-x 3.2 学习笔记(九)EventDispatcher事件分发机制
EventDispatcher事件分发机制先创建事件,注册到事件管理中心_eventDispatcher,通过发布事件得到响应进行回调,完成事件流. 有五种不同的事件机制:EventListenerT ...
- Android开发学习笔记--给一个按钮定义事件
学习Android的第一天,了解了各种布局,然后自己动手画出了一个按钮,然后给按钮定义了一个事件是弹出一条消息显示“我成功了!”字样,具体过程如下: 1.修改布局文件activity_main.xml ...
- Android学习笔记--处理UI事件
Handling UI Events 在Android里, 有不只一种方式可以截获用户与你的应用程序交互的事件. 在你的界面上处理事件时,你需要捕获用户与某个View实例交互时所产生的事件.View类 ...
- Java基础学习笔记十二 类、抽象类、接口作为方法参数和返回值以及常用API
不同修饰符使用细节 常用来修饰类.方法.变量的修饰符 public 权限修饰符,公共访问, 类,方法,成员变量 protected 权限修饰符,受保护访问, 方法,成员变量 默认什么也不写 也是一种权 ...
- C#线程学习笔记五:线程同步--事件构造
本笔记摘抄自:https://www.cnblogs.com/zhili/archive/2012/07/23/Event_Constructor.html,记录一下学习过程以备后续查用. 前面讲的线 ...
- C#学习笔记--详解委托,事件与回调函数
.Net编程中最经常用的元素,事件必然是其中之一.无论在ASP.NET还是WINFrom开发中,窗体加载(Load),绘制(Paint),初始化(Init)等等.“protected void Pag ...
- 《C#高级编程》学习笔记------C#中的委托和事件(续)
本文转载自张子阳 目录 为什么要使用事件而不是委托变量? 为什么委托定义的返回值通常都为void? 如何让事件只允许一个客户订阅?(事件访问器) 获得多个返回值与异常处理 委托中订阅者方法超时的处理 ...
- python学习笔记10(函数一): 函数使用、调用、返回值
一.函数的定义 在某些编程语言当中,函数声明和函数定义是区分开的(在这些编程语言当中函数声明和函数定义可以出现在不同的文件中,比如C语言),但是在Python中,函数声明和函数定义是视为一体的.在Py ...
随机推荐
- 【Python学习笔记】集合
概述 集合的一般操作 内建函数进行标准操作集合 数学运算符进行标准操作集合 集合的应用 概述 python的集合(set)是无序不重复元素集,是一种容器.集合(set)中的元素必须是不可变对象,即可用 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- shell脚本操作mysql库
shell脚本操作mysql数据库-e参数执行各种sql(指定到处编码--default-character-set=utf8 -s,去掉第一行的字段名称信息-N) 2011-05-11 18:18: ...
- JVM性能优化,提高Java的伸缩性
很多程序员在解决JVM性能问题的时候,花开了很多时间去调优应用程序级别的性能瓶颈,当你读完这本系列文章之后你会发现我可能更加系统地看待这类的问题.我说过JVM的自身技术限制了Java企业级应用的伸缩性 ...
- cocos2d-x 3.2 DrawNode 绘图API
关于Cocos2d-x 3.x 版本的绘图方法有两种: 1.使用DrawNode类绘制自定义图形. 2.继承Layer类重写draw()方法. 以上两种方法都可以绘制自定义图形,根据自己的需要选择合适 ...
- 关于Ext.NET Demo程序在IIS7.5部署出现"Ext未定义"的解决方案
有以下三点 1.应用程序池请用ASP.NET4.0经典模式 2.安装ASP.NET 控制面板-->程序和功能-->打开或关闭WIndows功能-->Internet信息服务--& ...
- http://blogs.msdn.com/b/pranavwagh/archive/2007/03/03/word-2007-file-seems-to-be-deleted-when-you-open-and-save-it-using-dsoframer.aspx
http://blogs.msdn.com/b/pranavwagh/archive/2007/03/03/word-2007-file-seems-to-be-deleted-when-you-op ...
- MS 数据库存储过程加密解密
存储过程加密解密在网上有很多,刚刚好最近需要用到,所以就查询了一下资料.记录一下 加密方法:执行如下存储过程 DECLARE @sp_name nvarchar(400) DECLARE @sp_co ...
- hdu 1199 Color the Ball
http://acm.hdu.edu.cn/showproblem.php?pid=1199 Color the Ball Time Limit: 2000/1000 MS (Java/Others) ...
- 判断时间大小 yyyy-MM-dd 格式
// yyyy-MM-dd function bigThanToday(someDate){ var date = new Date(); var dateStr = date.getFullYear ...