cocos2dx游戏开发加速度计

在cocos2d-x引擎中 使用了类CCAccelerometer来存储加速度计的信息 类CCAccelerometer的作用和用户操作的分发器类似 区别在于用户操作的分发器可以拥有很多委托对象 而加速度计只存在一个委托对象 这是因为一个移动设备只有一个硬件 所以接口进行了简化 CCAccelerometerDelegate就是加速度计的委托对象 

和触摸事件事件一样 重力感应的处理先被引擎抽象为一个触摸代理的协议 然后由CCLayer提供了一个接口 在实际开发中 只需要重载加速度计事件即可

virtual void didAccelerate(CCAcceleration* pAccelerationValue);

CCAcceleration是一个结构体 包含加速度计获得的三个方向的加速度

  1. typedef struct  
  2.  
  3. double x;  
  4. double y;  
  5. double z;  
  6. double timestamp;  
  7. }CCAcceleration;  

为了便于游戏中开发使用 每一个结构体中的每一个方向的加速度大小都是以一个重力加速度为单位9.8m/s的平方 举例来说 当手机放置得桌子上的时候 获得的加速度应该为(0,1,0)

 正常使用的时候 总的加速度应该在1上下波动 如果检测到一个大幅度的偏离1 可以判断为突然动作:手摇手机 会在一个或者多个方向上出现出很大的加速度 投掷或者坠落则很容易检测到一个很小的加速度 


  1. void AccelerometerTest::onEnter()  
  2.  
  3.     CCLayer::onEnter();  
  4.   
  5.     setAccelerometerEnabled(true);//想要当前CCNode对象可以接收加速度计的操作信息 必须在初始化函数中调用此函数  
  6.   
  7.   
  8.     CCLabelTTF* label CCLabelTTF::create(title().c_str(), "Arial"32);  
  9.     addChild(label, 1);  
  10.     label->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y-50) );  
  11.   
  12.     m_pBall CCSprite::create("Images/ball.png");  
  13.     m_pBall->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y));  
  14.     addChild(m_pBall);  
  15.   
  16.     m_pBall->retain();  
  17.  

重写didAccelerate这个函数 获得CCAcceleration结构体信息 获得X,Y,Z方向上的加速度 分别乘以重力加速度获得相应的加速度

  1.   
  1. void AccelerometerTest::didAccelerate(CCAcceleration* pAccelerationValue)  
  2.  
  3. //     double fNow pAccelerationValue->timestamp;  
  4. //   
  5. //     if (m_fLastTime 0.0)  
  6. //     {  
  7. //         CCPoint ptNow convertToUI  
  8. //     }  
  9. //   
  10. //     m_fLastTime fNow;  
  11.   
  12.     CCDirector* pDir CCDirector::sharedDirector();  
  13.   
  14.       
  15.     if m_pBall == NULL  
  16.         return 
  17.      
  18.   
  19.     CCSize ballSize  m_pBall->getContentSize();  
  20.   
  21.     CCPoint ptNow  m_pBall->getPosition();  
  22.     CCPoint ptTemp pDir->convertToUI(ptNow);  
  23.   
  24.     ptTemp.x += pAccelerationValue->x 9.81f;  
  25.     ptTemp.y -= pAccelerationValue->y 9.81f;  
  26.   
  27.     CCPoint ptNext pDir->convertToGL(ptTemp);  
  28.     FIX_POS(ptNext.x, (VisibleRect::left().x+ballSize.width 2.0), (VisibleRect::right().x ballSize.width 2.0));  
  29.     FIX_POS(ptNext.y, (VisibleRect::bottom().y+ballSize.height 2.0), (VisibleRect::top().y ballSize.height 2.0));  
  30.     m_pBall->setPosition(ptNext);  
  31.  
  32.   
  33. //------------------------------------------------------------------  
  34. //  
  35. // AccelerometerTestScene  
  36. //  
  37. //------------------------------------------------------------------  
  38. void AccelerometerTestScene::runThisTest()  
  39.  
  40.     CCLayer* pLayer new AccelerometerTest();  
  41.     addChild(pLayer);  
  42.     pLayer->release();  
  43.   
  44.     CCDirector::sharedDirector()->replaceScene(this);  
  45.  



初始化加速度计信息:
  1. class CC_DLL CCAccelerometer  
  2.  
  3. public 
  4.     CCAccelerometer();  
  5.     ~CCAccelerometer();  
  6.   
  7.     void setDelegate(CCAccelerometerDelegate* pDelegate);  
  8.     void setAccelerometerInterval(float interval);  
  9.     void update( double x,double y,double z,double timestamp );  
  10. private 
  11.     CCAcceleration m_obAccelerationValue;  
  12.     CCAccelerometerDelegate* m_pAccelDelegate;  
  13. };  
  14.   
  15. NS_CC_END  

  1. CCAccelerometer::CCAccelerometer()   
  2.     m_pAccelDelegate(NULL)  
  3.  
  4.     memset(&m_obAccelerationValue, 0, sizeof(m_obAccelerationValue));  
  5.  
  6.   
  7. CCAccelerometer::~CCAccelerometer()   
  8.  
  9.   
  10.  
  11.   
  12. void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)   
  13.  
  14.     m_pAccelDelegate pDelegate;  
  15.   
  16.     // Enable/disable the accelerometer.  
  17.     // Well, there isn't one on Win32 so we don't do anything other than register  
  18.     // and deregister ourselves from the Windows Key handler.  
  19.     if (pDelegate)  
  20.      
  21.         // Register our handler  
  22.         CCEGLView::sharedOpenGLView()->setAccelerometerKeyHook( &myAccelerometerKeyHook );  
  23.      
  24.     else  
  25.      
  26.         // De-register our handler  
  27.         CCEGLView::sharedOpenGLView()->setAccelerometerKeyHook( NULL );  
  28.         resetAccelerometer();  
  29.      
  30.  
  31.   
  32. void CCAccelerometer::setAccelerometerInterval(float interval)  
  33.  
  34.   
  35.  
  36.   
  37. void CCAccelerometer::update( double x,double y,double z,double timestamp   
  38.  
  39.     if (m_pAccelDelegate)  
  40.      
  41.         m_obAccelerationValue.x            x;  
  42.         m_obAccelerationValue.y            y;  
  43.         m_obAccelerationValue.z            z;  
  44.         m_obAccelerationValue.timestamp timestamp;  
  45.   
  46.         // Delegate  
  47.         m_pAccelDelegate->didAccelerate(&m_obAccelerationValue);  
  48.          
  49.  
  50.   
  51. NS_CC_END 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值