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

先看头文件:

  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>

最新文章

  1. Windows Server 2008设置远程桌面连接的最大数量
  2. tomcat重启脚本
  3. git初学习体会
  4. Jquery中的checkbox 及radio的问题
  5. Java学习心得之 Linux下搭建Java环境
  6. UITableView相关知识点
  7. QQ邮箱
  8. 从零開始学android&amp;lt;RelativeLayout相对布局.十六.&amp;gt;
  9. 【转载】Linux的inode的理解
  10. 51 nod 1188 最大公约数之和 V2
  11. MVC简单增删改
  12. Python中函数的嵌套及闭包
  13. (转载)利用SIFT和RANSAC算法(openCV框架)实现物体的检测与定位,并求出变换矩阵(findFundamentalMat和findHomography的比较) 置顶
  14. Struts2,Spring,Hibernate框架的优缺点
  15. HDU 2187 - 悼念512汶川大地震遇难同胞——老人是真饿了 - [大水题]
  16. 使用require.js编写模块化JS
  17. 记一次spring boot中MongoDB Prematurely reached end of stream的异常解决
  18. Bootstrap如何适配移动浏览器
  19. g++动态库静态库混合链接
  20. Scrapy中用xpath/css爬取豆瓣电影Top250:解决403HTTP status code is not handled or not allowed

热门文章

  1. docker 搭建nginx
  2. es6的解构赋值用途
  3. java删除文件操作代码备忘
  4. restful开发API
  5. CoordTools
  6. FastDFS安装注意事项
  7. 伪造请求IP
  8. JS中JSON.parse和eval的异同
  9. 《西部世界》S2E9:蝶化庄周,浮生若梦
  10. [web前端] yarn和npm命令使用