zoukankan      html  css  js  c++  java
  • cocos2d-x 重力感应

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

    先看头文件:

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


    看.cpp
    1. #include "HelloWorldScene.h"  
    2.   
    3. using namespace cocos2d;  
    4.   
    5. #define FIX_POS(_pos, _min, _max)   
    6.     if (_pos < _min)          
    7.     _pos = _min;          
    8. else if (_pos > _max)     
    9.     _pos = _max;          
    10.   
    11. HelloWorld::HelloWorld()  
    12. : m_fLastTime(0.0)  
    13. {  
    14. }  
    15.   
    16. HelloWorld::~HelloWorld()  
    17. {  
    18.      m_pBall->release();  
    19. }  
    20.   
    21. CCScene* HelloWorld::scene()  
    22. {  
    23.     CCScene * scene = NULL;  
    24.     do   
    25.     {  
    26.         // 'scene' is an autorelease object  
    27.         scene = CCScene::create();  
    28.         CC_BREAK_IF(! scene);  
    29.   
    30.         // 'layer' is an autorelease object  
    31.         HelloWorld *layer = HelloWorld::create();  
    32.         CC_BREAK_IF(! layer);  
    33.   
    34.         // add layer as a child to scene  
    35.         scene->addChild(layer);  
    36.     } while (0);  
    37.   
    38.     // return the scene  
    39.     return scene;  
    40. }  
    41.   
    42. // on "init" you need to initialize your instance  
    43. bool HelloWorld::init()  
    44. {  
    45.     bool bRet = false;  
    46.     do   
    47.     {  
    48.         CC_BREAK_IF(! CCLayer::init());  
    49.   
    50.         CCMenuItemImage *pCloseItem = CCMenuItemImage::create(  
    51.             "CloseNormal.png",  
    52.             "CloseSelected.png",  
    53.             this,  
    54.             menu_selector(HelloWorld::menuCloseCallback));  
    55.         CC_BREAK_IF(! pCloseItem);  
    56.   
    57.         // Place the menu item bottom-right conner.  
    58.         pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));  
    59.   
    60.         // Create a menu with the "close" menu item, it's an auto release object.  
    61.         CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);  
    62.         pMenu->setPosition(CCPointZero);  
    63.         CC_BREAK_IF(! pMenu);  
    64.   
    65.         // Add the menu to HelloWorld layer as a child layer.  
    66.         this->addChild(pMenu, 1);  
    67.   
    68.     //add Accelerometer  
    69.     CSize size = CCDirector::sharedDirector()->getWinSize();  
    70.   
    71.         setAccelerometerEnabled(true);//打开重力感应  
    72.   
    73.     m_pBall = CCSprite::create("ball.png");  
    74.     m_pBall->setPosition(ccp(size.width/2, size.height/2));  
    75.     addChild(m_pBall);  
    76.   
    77.     m_pBall->retain();  
    78.   
    79.         bRet = true;  
    80.     } while (0);  
    81.   
    82.     return bRet;  
    83. }  
    84.   
    85. <pre name="code" class="cpp">void HelloWorld::menuCloseCallback(CCObject* pSender)  
    86. {  
    87.     // "close" menu item clicked  
    88.     CCDirector::sharedDirector()->end();  
    89. }  
    90.   
    91. void HelloWorld::didAccelerate(CCAcceleration* pAccelerationValue)  
    92. {  
    93. //     double fNow = pAccelerationValue->timestamp;  
    94. //   
    95. //     if (m_fLastTime > 0.0)  
    96. //     {  
    97. //         CCPoint ptNow = convertToUI  
    98. //     }  
    99. //   
    100. //     m_fLastTime = fNow;  
    101.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
    102.   
    103.     CCDirector* pDir = CCDirector::sharedDirector();  
    104.   
    105.      /*FIXME: Testing on the Nexus S sometimes m_pBall is NULL */  
    106.      if ( m_pBall == NULL ) {  
    107.             return;  
    108.         }  
    109.   
    110.      CCSize ballSize  = m_pBall->getContentSize();  
    111.   
    112.     CCPoint ptNow  = m_pBall->getPosition();  
    113.         CCPoint ptTemp = pDir->convertToUI(ptNow);  
    114.   
    115.     //9.8 重力加速度  
    116.         ptTemp.x += pAccelerationValue->x * 9.81f;  
    117.         ptTemp.y -= pAccelerationValue->y * 9.81f;  
    118.   
    119.     CCPoint ptNext = pDir->convertToGL(ptTemp);  
    120.     FIX_POS(ptNext.x, (0+ballSize.width / 2.0), (size.width - ballSize.width / 2.0));  
    121.     FIX_POS(ptNext.y, (0+ballSize.height / 2.0), (size.height - ballSize.height / 2.0));  
    122.     m_pBall->setPosition(ptNext);  
    123. }</pre>  
    124. <p></p>  
    125. <pre></pre> 
  • 相关阅读:
    vue-cli 安装一直失败
    如果不存在公缀,返回空字符串
    .sh文件格式问题dos转linux或unix
    Kettle串联多个Spark任务
    云效自动化部署+部署包备份
    云效分支管理
    云效IDE综合插件Alibaba Cloud Toolkit
    流水线自动化部署-中转部署-目标机器不联网情况下应用
    云效流水线自动化部署
    云效流水线自动发布到Maven仓
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/7397844.html
Copyright © 2011-2022 走看看