cocos2dx游戏开发加速度计 心已赠人 2022-08-25 13:58 220阅读 0赞 在cocos2d-x引擎中 使用了类CCAccelerometer来存储加速度计的信息 类CCAccelerometer的作用和用户操作的分发器类似 区别在于用户操作的分发器可以拥有很多委托对象 而加速度计只存在一个委托对象 这是因为一个移动设备只有一个硬件 所以接口进行了简化 CCAccelerometerDelegate就是加速度计的委托对象 和触摸事件事件一样 重力感应的处理先被引擎抽象为一个触摸代理的协议 然后由CCLayer提供了一个接口 在实际开发中 只需要重载加速度计事件即可 virtual void didAccelerate(CCAcceleration\* pAccelerationValue); CCAcceleration是一个结构体 包含加速度计获得的三个方向的加速度 **\[cpp\]** [view plain][][copy][view plain] 1. typedefstruct 2. \{ 3. doublex; 4. doubley; 5. doublez; 6. doubletimestamp; 7. \}CCAcceleration; 为了便于游戏中开发使用 每一个结构体中的每一个方向的加速度大小都是以一个重力加速度为单位9.8m/s的平方 举例来说 当手机放置得桌子上的时候 获得的加速度应该为(0,1,0) 正常使用的时候 总的加速度应该在1上下波动 如果检测到一个大幅度的偏离1 可以判断为突然动作:手摇手机 会在一个或者多个方向上出现出很大的加速度 投掷或者坠落则很容易检测到一个很小的加速度 **\[cpp\]** [view plain][][copy][view plain] 1. voidAccelerometerTest::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方向上的加速度 分别乘以重力加速度获得相应的加速度 **\[cpp\]** [view plain][][copy][view plain] 1. **\[cpp\]** [view plain][][copy][view plain] 1. voidAccelerometerTest::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. voidAccelerometerTestScene::runThisTest() 39. \{ 40. CCLayer\* pLayer = newAccelerometerTest(); 41. addChild(pLayer); 42. pLayer->release(); 43. 44. CCDirector::sharedDirector()->replaceScene(this); 45. \} 初始化加速度计信息: **\[cpp\]** [view plain][][copy][view plain] 1. classCC\_DLL CCAccelerometer 2. \{ 3. public: 4. CCAccelerometer(); 5. ~CCAccelerometer(); 6. 7. voidsetDelegate(CCAccelerometerDelegate\* pDelegate); 8. voidsetAccelerometerInterval(floatinterval); 9. voidupdate( doublex,doubley,doublez,doubletimestamp ); 10. private: 11. CCAcceleration m\_obAccelerationValue; 12. CCAccelerometerDelegate\* m\_pAccelDelegate; 13. \}; 14. 15. NS\_CC\_END **\[cpp\]** [view plain][][copy][view plain] 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. voidCCAccelerometer::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. voidCCAccelerometer::setAccelerometerInterval(floatinterval) 33. \{ 34. 35. \} 36. 37. voidCCAccelerometer::update( doublex,doubley,doublez,doubletimestamp ) 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 [view plain]: http://blog.csdn.net/oqixilaotou/article/details/18286567#
还没有评论,来说两句吧...