cocos2d-x重力感应
来源:互联网 发布:指南针淘宝店是正品吗 编辑:程序博客网 时间:2024/05/17 03:32
一、保证pos的位置在min,max内
- #define FIX_POS(_pos, _min, _max) \
- if (_pos < _min) \
- _pos = _min; \
- else if (_pos > _max) \
- _pos = _max; \
- 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));
二、设置CCLayer层的onEnter里面设置重力感应。设置了重力感应。如果是PC,没有重力感应,就是用方向键控制。
- void xxxLayer::onEnter()
- {
- CCLayer::onEnter();
- setAccelerometerEnabled(true);
- }
当重力感应方向变化时候,会调用
- void AccelerometerTest::didAccelerate(CCAcceleration* pAccelerationValue)
- {
- 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);
- }
pAccelerationValue包含x,y,z三个方向的重力值。
三、removeChild删除子控件不奔溃的处理办法。
- void CrashTest::onEnter()
- {
- ActionManagerTest::onEnter();
- CCSprite* child = CCSprite::create(s_pPathGrossini);
- child->setPosition( VisibleRect::center() );
- addChild(child, 1);
- //Sum of all action's duration is 1.5 second.
- child->runAction(CCRotateBy::create(1.5f, 90));
- child->runAction(CCSequence::create(
- CCDelayTime::create(1.4f),
- CCFadeOut::create(1.1f),
- NULL)
- );
- //After 1.5 second, self will be removed.
- runAction( CCSequence::create(
- CCDelayTime::create(1.4f),
- CCCallFunc::create(this, callfunc_selector(CrashTest::removeThis)),
- NULL)
- );
- }
- void CrashTest::removeThis()
- {
- m_pParent->removeChild(this, true);
- }
- void PauseTest::onEnter()
- {
- //
- // This test MUST be done in 'onEnter' and not on 'init'
- // otherwise the paused action will be resumed at 'onEnter' time
- //
- ActionManagerTest::onEnter();
- CCLabelTTF* l = CCLabelTTF::create("After 5 seconds grossini should move", "Thonburi", 16);
- addChild(l);
- l->setPosition( ccp(VisibleRect::center().x, VisibleRect::top().y-75) );
- //
- // Also, this test MUST be done, after [super onEnter]
- //
- CCSprite* grossini = CCSprite::create(s_pPathGrossini);
- addChild(grossini, 0, kTagGrossini);
- grossini->setPosition(VisibleRect::center() );
- CCAction* action = CCMoveBy::create(1, ccp(150,0));
- CCDirector* pDirector = CCDirector::sharedDirector();
- pDirector->getActionManager()->addAction(action, grossini, true);
- schedule( schedule_selector(PauseTest::unpause), 3);
- }
- void PauseTest::unpause(float dt)
- {
- unschedule( schedule_selector(PauseTest::unpause) );
- CCNode* node = getChildByTag( kTagGrossini );
- CCDirector* pDirector = CCDirector::sharedDirector();
- pDirector->getActionManager()->resumeTarget(node);
- }
五、停止动作stopActionByTag。特别注意的是动作和CCSprite加了标志,比如setTag(kTagSequence)方便以后查找。下面代码是先用getChildByTab(kTagGrossini)获取节点。然后stopActionByTay(kTagSequence);
- void RemoveTest::onEnter()
- {
- ActionManagerTest::onEnter();
- CCMoveBy* pMove = CCMoveBy::create(2, ccp(200, 0));
- CCCallFunc* pCallback = CCCallFunc::create(this, callfunc_selector(RemoveTest::stopAction));
- CCActionInterval* pSequence = CCSequence::create(pMove, pCallback, NULL);
- pSequence->setTag(kTagSequence);
- CCSprite* pChild = CCSprite::create(s_pPathGrossini);
- pChild->setPosition( VisibleRect::center() );
- addChild(pChild, 1, kTagGrossini);
- pChild->runAction(pSequence);
- }
- void RemoveTest::stopAction()
- {
- CCNode* pSprite = getChildByTag(kTagGrossini);
- pSprite->stopActionByTag(kTagSequence);
- }
- void ResumeTest::onEnter()
- {
- ActionManagerTest::onEnter();
- CCSprite* pGrossini = CCSprite::create(s_pPathGrossini);
- addChild(pGrossini, 0, kTagGrossini);
- pGrossini->setPosition(VisibleRect::center());
- pGrossini->runAction(CCScaleBy::create(2, 2));
- CCDirector* pDirector = CCDirector::sharedDirector();
- pDirector->getActionManager()->pauseTarget(pGrossini);
- pGrossini->runAction(CCRotateBy::create(2, 360));
- this->schedule(schedule_selector(ResumeTest::resumeGrossini), 3.0f);
- }
- void ResumeTest::resumeGrossini(float time)
- {
- this->unschedule(schedule_selector(ResumeTest::resumeGrossini));
- CCNode* pGrossini = getChildByTag(kTagGrossini);
- CCDirector* pDirector = CCDirector::sharedDirector();
- pDirector->getActionManager()->resumeTarget(pGrossini);
- }
转自:http://blog.csdn.net/hayyoung/article/details/8804909
- cocos2d-x重力感应
- cocos2d-x重力感应
- cocos2d-x 重力感应
- cocos2d-x 3.0 重力感应
- 【cocos2d-x】重力感应----移动小球
- cocos2d-x 重力感应 加速器的使用
- cocos2d-x 3.2 |重力感应实现方法
- box2d弹球 cocos2d-x重力感应(cocos2d-x2.1)
- box2d弹球 cocos2d-x重力感应(cocos2d-x2.1)
- cocos2d-x 菜鸟进阶篇(二) 重力感应
- Cocos2d-x之testCpp---Accelerometer(重力感应)分析
- Cocos2d-x添加Android重力感应切换屏幕方向
- [寒江孤叶丶的Cocos2d-x之旅_11]Cocos2d-x的重力感应Demo。如何使用重力感应。
- cocos2d - JS 重力感应监听
- cocos2dx3.x开启重力感应
- 关于cocos2d-html5 中使用重力感应于重力加速
- 重力感应
- 重力感应
- C++与C语言对照学习(设计模式)
- VB查询数据库之结账——机房收费系统总结(五)
- 笔记2 (标准库类型string、vector和bitset)
- 第十四周 数组
- 依赖注入框架Autofac的简单使用
- cocos2d-x重力感应
- 如何在word2013(2007、2010)中添加带滚动条的文本框
- Django ModelForm的使用
- const int *pi 与int *const pi区别 (void*)
- HBase安装步骤_伪分布式模式
- 外设位宽为8、16、32时,CPU与外设之间地址线的连接方法
- 各大主流.Net的IOC框架性能测试比较
- Django Form
- hdu 4804 Campus Design (2013 南京 轮廓线dp)