cocos2d-x 重力感应
2024-10-11 18:59:46
本文没你想象的那么,,复杂。事实上就是通过重力感应控制个小球移动而已。
先看头文件:
- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- USING_NS_CC;
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- HelloWorld(void);
- ~HelloWorld(void);
- // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
- virtual bool init();
- // there's no 'id' in cpp, so we recommand to return the exactly class pointer
- static cocos2d::CCScene* scene();
- // a selector callback
- void menuCloseCallback(CCObject* pSender);
- virtual void didAccelerate(CCAcceleration* pAccelerationValue);
- // implement the "static node()" method manually
- CREATE_FUNC(HelloWorld);
- protected:
- CCSprite* m_pBall;
- double m_fLastTime;
- };
- #endif // __HELLOWORLD_SCENE_H__
看.cpp
- #include "HelloWorldScene.h"
- using namespace cocos2d;
- #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;
- }
- // on "init" you need to initialize your instance
- bool HelloWorld::init()
- {
- bool bRet = false;
- do
- {
- CC_BREAK_IF(! CCLayer::init());
- CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
- "CloseNormal.png",
- "CloseSelected.png",
- this,
- menu_selector(HelloWorld::menuCloseCallback));
- CC_BREAK_IF(! pCloseItem);
- // Place the menu item bottom-right conner.
- pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
- // Create a menu with the "close" menu item, it's an auto release object.
- CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
- pMenu->setPosition(CCPointZero);
- CC_BREAK_IF(! pMenu);
- // Add the menu to HelloWorld layer as a child layer.
- this->addChild(pMenu, 1);
- //add Accelerometer
- CSize size = CCDirector::sharedDirector()->getWinSize();
- setAccelerometerEnabled(true);//打开重力感应
- m_pBall = CCSprite::create("ball.png");
- m_pBall->setPosition(ccp(size.width/2, size.height/2));
- addChild(m_pBall);
- m_pBall->retain();
- bRet = true;
- } while (0);
- return bRet;
- }
- <pre name="code" class="cpp">void HelloWorld::menuCloseCallback(CCObject* pSender)
- {
- // "close" menu item clicked
- CCDirector::sharedDirector()->end();
- }
- void HelloWorld::didAccelerate(CCAcceleration* pAccelerationValue)
- {
- // double fNow = pAccelerationValue->timestamp;
- //
- // if (m_fLastTime > 0.0)
- // {
- // CCPoint ptNow = convertToUI
- // }
- //
- // m_fLastTime = fNow;
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCDirector* pDir = CCDirector::sharedDirector();
- /*FIXME: Testing on the Nexus S sometimes m_pBall is NULL */
- if ( m_pBall == NULL ) {
- return;
- }
- CCSize ballSize = m_pBall->getContentSize();
- CCPoint ptNow = m_pBall->getPosition();
- CCPoint ptTemp = pDir->convertToUI(ptNow);
- //9.8 重力加速度
- ptTemp.x += pAccelerationValue->x * 9.81f;
- ptTemp.y -= pAccelerationValue->y * 9.81f;
- CCPoint ptNext = pDir->convertToGL(ptTemp);
- FIX_POS(ptNext.x, (0+ballSize.width / 2.0), (size.width - ballSize.width / 2.0));
- FIX_POS(ptNext.y, (0+ballSize.height / 2.0), (size.height - ballSize.height / 2.0));
- m_pBall->setPosition(ptNext);
- }</pre>
- <p></p>
- <pre></pre>
最新文章
- Windows Server 2008设置远程桌面连接的最大数量
- tomcat重启脚本
- git初学习体会
- Jquery中的checkbox 及radio的问题
- Java学习心得之 Linux下搭建Java环境
- UITableView相关知识点
- QQ邮箱
- 从零開始学android&;lt;RelativeLayout相对布局.十六.&;gt;
- 【转载】Linux的inode的理解
- 51 nod 1188 最大公约数之和 V2
- MVC简单增删改
- Python中函数的嵌套及闭包
- (转载)利用SIFT和RANSAC算法(openCV框架)实现物体的检测与定位,并求出变换矩阵(findFundamentalMat和findHomography的比较) 置顶
- Struts2,Spring,Hibernate框架的优缺点
- HDU 2187 - 悼念512汶川大地震遇难同胞——老人是真饿了 - [大水题]
- 使用require.js编写模块化JS
- 记一次spring boot中MongoDB Prematurely reached end of stream的异常解决
- Bootstrap如何适配移动浏览器
- g++动态库静态库混合链接
- Scrapy中用xpath/css爬取豆瓣电影Top250:解决403HTTP status code is not handled or not allowed