本文没你想象的那么,,复杂。事实上就是通过重力感应控制个小球移动而已。

先看头文件:

  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3. #include "cocos2d.h"
  4. USING_NS_CC;
  5. class HelloWorld : public cocos2d::CCLayer
  6. {
  7. public:
  8. HelloWorld(void);
  9. ~HelloWorld(void);
  10. // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
  11. virtual bool init();
  12. // there's no 'id' in cpp, so we recommand to return the exactly class pointer
  13. static cocos2d::CCScene* scene();
  14. // a selector callback
  15. void menuCloseCallback(CCObject* pSender);
  16. virtual void didAccelerate(CCAcceleration* pAccelerationValue);
  17. // implement the "static node()" method manually
  18. CREATE_FUNC(HelloWorld);
  19. protected:
  20. CCSprite* m_pBall;
  21. double    m_fLastTime;
  22. };
  23. #endif  // __HELLOWORLD_SCENE_H__

看.cpp

  1. #include "HelloWorldScene.h"
  2. using namespace cocos2d;
  3. #define FIX_POS(_pos, _min, _max) \
  4. if (_pos < _min)        \
  5. _pos = _min;        \
  6. else if (_pos > _max)   \
  7. _pos = _max;        \
  8. HelloWorld::HelloWorld()
  9. : m_fLastTime(0.0)
  10. {
  11. }
  12. HelloWorld::~HelloWorld()
  13. {
  14. m_pBall->release();
  15. }
  16. CCScene* HelloWorld::scene()
  17. {
  18. CCScene * scene = NULL;
  19. do
  20. {
  21. // 'scene' is an autorelease object
  22. scene = CCScene::create();
  23. CC_BREAK_IF(! scene);
  24. // 'layer' is an autorelease object
  25. HelloWorld *layer = HelloWorld::create();
  26. CC_BREAK_IF(! layer);
  27. // add layer as a child to scene
  28. scene->addChild(layer);
  29. } while (0);
  30. // return the scene
  31. return scene;
  32. }
  33. // on "init" you need to initialize your instance
  34. bool HelloWorld::init()
  35. {
  36. bool bRet = false;
  37. do
  38. {
  39. CC_BREAK_IF(! CCLayer::init());
  40. CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  41. "CloseNormal.png",
  42. "CloseSelected.png",
  43. this,
  44. menu_selector(HelloWorld::menuCloseCallback));
  45. CC_BREAK_IF(! pCloseItem);
  46. // Place the menu item bottom-right conner.
  47. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
  48. // Create a menu with the "close" menu item, it's an auto release object.
  49. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  50. pMenu->setPosition(CCPointZero);
  51. CC_BREAK_IF(! pMenu);
  52. // Add the menu to HelloWorld layer as a child layer.
  53. this->addChild(pMenu, 1);
  54. //add Accelerometer
  55. CSize size = CCDirector::sharedDirector()->getWinSize();
  56. setAccelerometerEnabled(true);//打开重力感应
  57. m_pBall = CCSprite::create("ball.png");
  58. m_pBall->setPosition(ccp(size.width/2, size.height/2));
  59. addChild(m_pBall);
  60. m_pBall->retain();
  61. bRet = true;
  62. } while (0);
  63. return bRet;
  64. }
  65. <pre name="code" class="cpp">void HelloWorld::menuCloseCallback(CCObject* pSender)
  66. {
  67. // "close" menu item clicked
  68. CCDirector::sharedDirector()->end();
  69. }
  70. void HelloWorld::didAccelerate(CCAcceleration* pAccelerationValue)
  71. {
  72. //     double fNow = pAccelerationValue->timestamp;
  73. //
  74. //     if (m_fLastTime > 0.0)
  75. //     {
  76. //         CCPoint ptNow = convertToUI
  77. //     }
  78. //
  79. //     m_fLastTime = fNow;
  80. CCSize size = CCDirector::sharedDirector()->getWinSize();
  81. CCDirector* pDir = CCDirector::sharedDirector();
  82. /*FIXME: Testing on the Nexus S sometimes m_pBall is NULL */
  83. if ( m_pBall == NULL ) {
  84. return;
  85. }
  86. CCSize ballSize  = m_pBall->getContentSize();
  87. CCPoint ptNow  = m_pBall->getPosition();
  88. CCPoint ptTemp = pDir->convertToUI(ptNow);
  89. //9.8 重力加速度
  90. ptTemp.x += pAccelerationValue->x * 9.81f;
  91. ptTemp.y -= pAccelerationValue->y * 9.81f;
  92. CCPoint ptNext = pDir->convertToGL(ptTemp);
  93. FIX_POS(ptNext.x, (0+ballSize.width / 2.0), (size.width - ballSize.width / 2.0));
  94. FIX_POS(ptNext.y, (0+ballSize.height / 2.0), (size.height - ballSize.height / 2.0));
  95. m_pBall->setPosition(ptNext);
  96. }</pre>
  97. <p></p>
  98. <pre></pre>

cocos2d-x 重力感应的更多相关文章

  1. cocos2dx 3.x(让精灵随着重力感应的方向移动而移动)

    // // GameScene.hpp // HelloWord // // Created by apple on 2017/1/7. // // #ifndef GameScene_hpp #de ...

  2. Cocos2D-X2.2.3学习笔记9(处理重力感应事件,移植到Android加入两次返回退出游戏效果)

    这节我们来学习Cocos2d-x的最后一节.怎样处理重力感应事件.移植到Android后加入再按一次返回键退出游戏等.我这里用的Android.IOS不会也没设备呃 效果图不好弄,由于是要移植到真机上 ...

  3. H5案例分享:html5重力感应事件

    html5重力感应事件 一.手机重力感应图形分析 1.设备围绕z轴的旋转角度为α,α角度的取值范围在[0,360). 设备在初始位置,与地球(XYZ)和身体(XYZ)某个位置对齐. 设备围绕z轴的旋转 ...

  4. 【Android开发学习笔记】【第九课】重力感应

    概念 使用重力感应技术的Android游戏已经屡见不鲜,不知道自己以后会不会用到,所以先研究了一下. 在网上学习了一下,貌似没有api,所以得自己去分析手机处在怎样状态下.注意: 下面提供的demo程 ...

  5. iOS 重力感应 学习1 陀螺仪 水平仪 指南针

    小球可以随着重力感应 四处乱撞. 放大了坐标位移 就可以看见小球动了. 然后规定小球的路径 当滑到中间时候 弹出一张图片 作为提示. 我做了一个小demo 效果不错哦 CMMotionManager ...

  6. 移动终端学习2:触屏原生js事件及重力感应

    如今智能移动设备已经渗透到人们生活的方方面面,用户数量也在不断迅速增长(市场研究机构 eMarketer 在今年初发表的趋势报告中预测,2014年至2018年,中国智能手机用户从总人口的 38.3%增 ...

  7. Android重力感应开发

    http://blog.csdn.net/mad1989/article/details/20848181 一.手机中常用的传感器 在Android2.3 gingerbread系统中,google提 ...

  8. 基于野火M3开发板(STM32F103VET6)的迷宫小球(重力感应控制)游戏开发

    2013-03-03 这是研一上学期<实时嵌入式系统实验>课程的大作业,是利用野火板的资源,加上一个AHRS(Attitude and Heading Reference System,姿 ...

  9. iOS设备的重力感应

    重力感应是每台iOS设备都具备的功能,所以在应用用好重力感应会有意想不到的效果 1.添加CoreMotion框架 2.在需要使用重力感应的类中添加头文件 #import <CoreMotion/ ...

随机推荐

  1. Python开发之日志记录模块:logging

    1 引言 最近在开发一个应用软件,为方便调试和后期维护,在代码中添加了日志,用的是Python内置的logging模块,看了许多博主的博文,颇有所得.不得不说,有许多博主大牛总结得确实很好.似乎我再写 ...

  2. 谷歌浏览器升级引起的BUG

    问题描述:在谷歌浏览器中,电脑打开A系统,添加附件时无法弹框. 预期结果:添加附件,可以打开弹框. 问题分析:谷歌浏览器自动升级,自55.0.2883.75后发布的新版本均自动停用Flash插件. 解 ...

  3. 模拟页面获取的php数据(三)

    <?php return array( "aData" => [//通勤方式 "trafficType" => [ 0 => [ &qu ...

  4. loj#2012. 「SCOI2016」背单词

    题目链接 loj#2012. 「SCOI2016」背单词 题解 题面描述有点不清楚. 考虑贪心 type1的花费一定不会是优的,不考虑, 所以先把后缀填进去,对于反串建trie树, 先填父亲再填儿子, ...

  5. HDU.1848.Fibonacci again and again(博弈论 Nim)

    题目链接 //求三堆石子的SG函数,异或起来就是整个游戏的SG值 #include <cstdio> #include <cstring> const int N=1005; ...

  6. SpringMybatisMapper

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" ...

  7. GPSCamera隐私声明

    GPSCamera获取了以下敏感隐私权限用于照片水印展示: 1:修改或删除您的SD卡中的内容 2:访问确切位置信息(使用 GPS 和网络进行定位) 3:访问大致位置信息(使用网络进行定位) 4:拍摄照 ...

  8. ext2文件系统学习(二)—— 目录磁盘结构

    创建镜像.mount等操作和上一篇一样,测试目录结构如下: 一些文件系统信息如下: Block size:             1024 Inodes per group:  128 Inode ...

  9. Spring Date JPA 更新部分字段

    在Spring Data JPA 中,新增和更新操作都是用save()的方式进行,JPA是通过什么方法来知道我们是要进行insert还是update呢? 经过测试,JPA对程序调用的save()方法判 ...

  10. 使用Python登录Github网站

    在下面的代码中, 展示了使用Python脚本登录Github的方法. 如果需要登录别的网站,那么请使用Chrome的Inspect的功能寻找到目标的object,对代码进行替换. 代码先登录了gith ...