決定好好總結下testCpp裡面的知識,這裡我把testCpp中AccelerometerTest剝離出來了,總結下~
我用的最新版的2dx。。。。cocos2d-2.1rc0-x-2.1.3
ndk是android-ndk-r8d
class檔案:http://download.csdn.net/detail/bill_ming/5568563
#include "HelloWorldScene.h"#include "cocos2d.h"#define FIX_POS(_pos, _min, _max) \if (_pos < _min) \_pos = _min; \else if (_pos > _max) \_pos = _max; \HelloWorld::HelloWorld(){m_fLastTime = 0.0;}HelloWorld::~HelloWorld(){m_pBall->release();}CCScene* HelloWorld::scene(){ CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::create(); CC_BREAK_IF(! scene); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene;}std::string HelloWorld::titile(){return "hehehaha~";}// on "init" you need to initialize your instancebool HelloWorld::init(){ bool bRet = false; do { CC_BREAK_IF(! CCLayer::init());//開啟重力感應setAccelerometerEnabled(true);CCLabelTTF *label = CCLabelTTF::create(titile().c_str(),"Arial",32); addChild(label,1); label->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y-50) );m_pBall = CCSprite::create("ball.png");//m_pBall->setPosition(ccp(winSize.height/2,winSize.width/2));m_pBall->setPosition(ccp(VisibleRect::center().x, VisibleRect::center().y));addChild(m_pBall);/************************************************************************//* 必須要調用了對象的autoRelease函數之後,retain和release函數才會生效, 否則,一切都是徒勞。create已經包含了autoRelease 注意:addChild函數已經包含了retain 參考:http://www.xue5.com/Mobile/Mobile/680881.html *//************************************************************************/ bRet = true; } while (0); return bRet;}void HelloWorld::didAccelerate(CCAcceleration* pAccelerationValue){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);}void HelloWorld::menuCloseCallback(CCObject* pSender){ // "close" menu item clicked CCDirector::sharedDirector()->end();}
1.先調用CCLayer的方法setAccelerometerEnabled(true);啟用重力感應
2.在重力方向改變時,會調用CCLayer的didAccelerate(CCAcceleration *pAccelerationValue),所以自己的HelloWorld類繼承了CCLayer,這裡重寫這個方法
問題總結:
1.原來testCpp裡實在controller.ccp中在CreateTestScene函數中調用的,他用的是
new AccelerometerTestScene();進入到AccelerometerTest.cpp,我們會發現,它還是用CCLayer* pLayer = new AccelerometerTest();而不是create方法,這兩種有什麼區別呢?我們知道當我們調用create()方法時,會調用init來初始化,但直接用new AAA();這種方式時,會調用onEnter()而不會是init...可以參考下:http://blog.csdn.net/bill_ming/article/details/9080271
2.testCpp裡,它的onEnter()函數中,建立ball精靈後有一句:m_pBall->retain();
但我記得調用addChild(m_pBall);這個函數後,就不必要再retain()了啊。。。可以參考下:
笨木頭的一篇博文:http://www.xue5.com/Mobile/Mobile/680881.html
希望看到的朋友能稍微解釋下哈,是不是我理解錯了。。。感謝感謝~~~
3.或許很多朋友也和我一樣,被各種getWinSize(),getContentSize(),getVisibleSize(),以及一些座標轉換搞混了、、、
可以參考下我總結的另一篇文章:http://blog.csdn.net/bill_ming/article/details/9078781
它根據重力移動的過程:
(1.取得小球的邏輯尺寸:為了在後邊移動時修正小球位置,不讓它跑出邊界
(2.取得當前位置,然後轉化為螢幕座標,然後根據當前位置以及加速度(9.81)計算出下一處的座標。
(3.把座標系轉換為openGl座標,然後根據小球邏輯尺寸,修正位置,最後setPosition就好了
4.但還有一個問題,小球可以根據重力移動,但如何設定加速減速的速度大小呢?
可以參考下:http://blog.csdn.net/jinglijun/article/details/8048013
這裡我沒用到,希望看到的朋友推薦一些資料~