TestCpp里面常用代码摘抄<重力感应和动作管理类>

一、保证pos的位置在min,max内

#define FIX_POS(_pos, _min, _max) \
    if (_pos < _min)        \
    _pos = _min;        \
else if (_pos > _max)   \
    _pos = _max;        \
用法,保证一个CCSprite在屏幕内。

    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);
}
四、addAction最后一个参数设为ture暂停动作,然后resumeTarget恢复动作,3秒后执行移动动作。
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);
}
六、先暂停动作,然后pauseTager,然后runAction一个动作,这样resumeTager的话,就可以同时执行2个动作了。
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);
}





参考资料:cocos2d-x基础知识(四) 简单菜单及坐标系


你可能感兴趣的:(TestCpp里面常用代码摘抄<重力感应和动作管理类>)