cocos2dx遊戲開發加速度計
阿新 • • 發佈:2018-11-08
在cocos2d-x引擎中 使用了類CCAccelerometer來儲存加速度計的資訊 類CCAccelerometer的作用和使用者操作的分發器類似 區別在於使用者操作的分發器可以擁有很多委託物件 而加速度計只存在一個委託物件 這是因為一個移動裝置只有一個硬體 所以介面進行了簡化 CCAccelerometerDelegate就是加速度計的委託物件
和觸控事件事件一樣 重力感應的處理先被引擎抽象為一個觸控代理的協議 然後由CCLayer提供了一個介面 在實際開發中 只需要過載加速度計事件即可
virtual void didAccelerate(CCAcceleration* pAccelerationValue);
CCAcceleration是一個結構體 包含加速度計獲得的三個方向的加速度
- typedef struct
- {
- double x;
- double y;
- double z;
- double timestamp;
- }CCAcceleration;
為了便於遊戲中開發使用 每一個結構體中的每一個方向的加速度大小都是以一個重力加速度為單位9.8m/s的平方 舉例來說 當手機放置得桌子上的時候 獲得的加速度應該為(0,1,0)
正常使用的時候 總的加速度應該在1上下波動 如果檢測到一個大幅度的偏離1 可以判斷為突然動作:手搖手機 會在一個或者多個方向上出現出很大的加速度 投擲或者墜落則很容易檢測到一個很小的加速度
- void AccelerometerTest::onEnter()
- {
- CCLayer::onEnter();
- setAccelerometerEnabled(true);//想要當前CCNode物件可以接收加速度計的操作資訊 必須在初始化函式中呼叫此函式
- CCLabelTTF* label = CCLabelTTF::create(title().c_str(), "Arial", 32);
- addChild(label, 1);
- label->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y-50) );
- m_pBall = CCSprite::create("Images/ball.png");
- m_pBall->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y));
- addChild(m_pBall);
- m_pBall->retain();
- }
重寫didAccelerate這個函式 獲得CCAcceleration結構體資訊 獲得X,Y,Z方向上的加速度 分別乘以重力加速度獲得相應的加速度
- void AccelerometerTest::didAccelerate(CCAcceleration* pAccelerationValue)
- {
- // double fNow = pAccelerationValue->timestamp;
- //
- // if (m_fLastTime > 0.0)
- // {
- // CCPoint ptNow = convertToUI
- // }
- //
- // m_fLastTime = fNow;
- CCDirector* pDir = CCDirector::sharedDirector();
- if ( m_pBall == NULL ) {
- return;
- }
- CCSize ballSize = m_pBall->getContentSize();
- CCPoint ptNow = m_pBall->getPosition();
- CCPoint ptTemp = pDir->convertToUI(ptNow);
- ptTemp.x += pAccelerationValue->x * 9.81f;
- ptTemp.y -= pAccelerationValue->y * 9.81f;
- CCPoint ptNext = pDir->convertToGL(ptTemp);
- FIX_POS(ptNext.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0));
- FIX_POS(ptNext.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0));
- m_pBall->setPosition(ptNext);
- }
- //------------------------------------------------------------------
- //
- // AccelerometerTestScene
- //
- //------------------------------------------------------------------
- void AccelerometerTestScene::runThisTest()
- {
- CCLayer* pLayer = new AccelerometerTest();
- addChild(pLayer);
- pLayer->release();
- CCDirector::sharedDirector()->replaceScene(this);
- }
- class CC_DLL CCAccelerometer
- {
- public:
- CCAccelerometer();
- ~CCAccelerometer();
- void setDelegate(CCAccelerometerDelegate* pDelegate);
- void setAccelerometerInterval(float interval);
- void update( double x,double y,double z,double timestamp );
- private:
- CCAcceleration m_obAccelerationValue;
- CCAccelerometerDelegate* m_pAccelDelegate;
- };
- NS_CC_END
[cpp] view plaincopy
- CCAccelerometer::CCAccelerometer() :
- m_pAccelDelegate(NULL)
- {
- memset(&m_obAccelerationValue, 0, sizeof(m_obAccelerationValue));
- }
- CCAccelerometer::~CCAccelerometer()
- {
- }
- void CCAccelerometer::setDelegate(CCAccelerometerDelegate* pDelegate)
- {
- m_pAccelDelegate = pDelegate;
- // Enable/disable the accelerometer.
- // Well, there isn't one on Win32 so we don't do anything other than register
- // and deregister ourselves from the Windows Key handler.
- if (pDelegate)
- {
- // Register our handler
- CCEGLView::sharedOpenGLView()->setAccelerometerKeyHook( &myAccelerometerKeyHook );
- }
- else
- {
- // De-register our handler
- CCEGLView::sharedOpenGLView()->setAccelerometerKeyHook( NULL );
- resetAccelerometer();
- }
- }
- void CCAccelerometer::setAccelerometerInterval(float interval)
- {
- }
- void CCAccelerometer::update( double x,double y,double z,double timestamp )
- {
- if (m_pAccelDelegate)
- {
- m_obAccelerationValue.x = x;
- m_obAccelerationValue.y = y;
- m_obAccelerationValue.z = z;
- m_obAccelerationValue.timestamp = timestamp;
- // Delegate
- m_pAccelDelegate->didAccelerate(&m_obAccelerationValue);
- }
- }
- NS_CC_END