Cocos2d-x_CCAction(常用动作类)介绍

前端之家收集整理的这篇文章主要介绍了Cocos2d-x_CCAction(常用动作类)介绍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();

    CREATE_FUNC(HelloWorld);

    void callBackC();
    void callBackN();
    void callBackND();
};

#endif

//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.cpp"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    
    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    
    float widthX = winSize.width*0.5;
    float heightY = winSize.height*0.5;
    
    CCSprite *pSpr = CCSprite::create("Icon-57.png");
    pSpr->setPosition(ccp(pSpr->getContentSize().width*0.5,heightY));
    this->addChild(pSpr);
    
    /* 带“To”类代表对象移动到目标位置;带“By”类代表对象移动有时间或者速率效果,都是相对坐标,都可以执行反序动作“reverse” */
    /* Cocos2d-x常用动作 */
    // CCMoveTo
    CCActionInterval *moveTo = CCMoveTo::create(5,ccp(widthX,heightY));
    pSpr->runAction(moveTo);
    
    // CCMoveBy
    CCActionInterval *moveBy = CCMoveBy::create(5,heightY));
    CCActionInterval *actionByBack = moveBy->reverse();
    pSpr->runAction(actionByBack);
    
    // CCScaleTo
    CCActionInterval *scaleTo = CCScaleTo::create(2,2);
    pSpr->runAction(scaleTo);
    
    // CCScaleBy
    CCActionInterval *scaleBy = CCScaleBy::create(2,2);
    CCActionInterval *actionByBack = scaleBy->reverse();
    pSpr->runAction(actionByBack);
    
    // CCRotateTo
    CCActionInterval *rotateTo = CCRotateTo::create(2,90);
    pSpr->runAction(rotateTo);
    
    // CCRotateBy
    CCActionInterval *rotateBy = CCRotateBy::create(2,90);
    CCActionInterval *actionByBack = rotateBy->reverse();
    pSpr->runAction(actionByBack);
    
    // CCSkewTo
    CCActionInterval *skewTo = CCSkewTo::create(2,10,10);
    pSpr->runAction(skewTo);
    
    // CCSkewBy
    CCActionInterval *skewBy = CCSkewBy::create(2,10);
    CCActionInterval *actionByBack = skewBy->reverse();
    pSpr->runAction(actionByBack);
    
    // CCJumpTo
    CCActionInterval *jumpTo = CCJumpTo::create(2,ccp(300,200),50,4);
    pSpr->runAction(jumpTo);
    
    // CCJumpBy
    CCActionInterval *jumpBy = CCJumpBy::create(2,4);
    CCActionInterval *actionByBack = jumpBy->reverse();
    pSpr->runAction(actionByBack);
    
    // ccBezierConfig
    ccBezierConfig bezierCon;
    bezierCon.controlPoint_1 = ccp(200,150);
    bezierCon.controlPoint_2 = ccp(200,260);
    bezierCon.endPosition = ccp(340,100);
    
    // CCBezierTo
    CCActionInterval *bezierTo = CCBezierTo::create(2,bezierCon);
    pSpr->runAction(bezierTo);
    
    CCActionInterval *bezierBy = CCBezierBy::create(2,bezierCon);
    CCActionInterval *actionByBack = bezierBy->reverse();
    pSpr->runAction(actionByBack);
    
    // CCFadeIn
    CCActionInterval *fadeIn = CCFadeIn::create(2);
    pSpr->runAction(fadeIn);
    
    // CCFadeOut
    CCActionInterval *fadeOut = CCFadeOut::create(2);
    pSpr->runAction(fadeOut);
    
    // CCTintTo
    CCActionInterval *tintTo = CCTintTo::create(2,255,0);
    pSpr->runAction(tintTo);
    
    // CCTintBy
    CCActionInterval *tintBy = CCTintBy::create(2,0);
    CCActionInterval *actionByBack = tintBy->reverse();
    pSpr->runAction(actionByBack);
    
    // CCBlink
    CCActionInterval *blink = CCBlink::create(2,3);
    pSpr->runAction(blink);
    
    // CCDelayTime
    CCActionInterval *delayTime = CCDelayTime::create(2);
    pSpr->runAction(delayTime);
    
    // CCOrbitCamera
    CCActionInterval *orbitCamera = CCOrbitCamera::create(3,1,45,180,90,0);
    pSpr->runAction(orbitCamera);
    
    // CCCardinalSpline
    CCPointArray *array = CCPointArray::create(20);
    array->addControlPoint(ccp(0,0));
    array->addControlPoint(ccp(210,240));
    array->addControlPoint(ccp(0,160));
    array->addControlPoint(ccp(0,0));
    
    // CCCardinalSplineTo
    CCActionInterval *splineTo = CCCardinalSplineTo::create(4,array,0);
    pSpr->runAction(splineTo);
    
    // CCCardinalSplineBy
    CCActionInterval *splineBy = CCCardinalSplineBy::create(4,0);
    CCActionInterval *actionByBack = splineBy->reverse();
    pSpr->runAction(actionByBack);
    
    // CCCatmullRom
    CCPointArray *array = CCPointArray::create(5);
    array->addControlPoint(ccp(240,30));
    array->addControlPoint(ccp(400,240));
    array->addControlPoint(ccp(240,30));
    
    // CCCatmullRomTo
    CCActionInterval *romTo = CCCatmullRomTo::create(2,array);
    pSpr->runAction(romTo);
    
    // CCCatmullRomBy
    CCActionInterval *romBy = CCCatmullRomBy::create(2,array);
    CCActionInterval *actionByBack = romBy->reverse();
    pSpr->runAction(actionByBack);
    
    // CCFollw
    // 为了看到跟随效果,创建一个参考物
    CCSprite *pSprT = CCSprite::create("Icon-57.png");
    pSpr->setPosition(ccp(420,40));
    this->addChild(pSprT);
    
    CCActionInterval *moveTo = CCMoveTo::create(3,pSpr->getPositionY()));
    pSpr->runAction(moveTo);
    
    CCFollow *follow = CCFollow::create(pSpr);
    this->runAction(follow);
    
    // CCEaseBounce
    CCActionInterval *move = CCMoveTo::create(5,pSpr->getPositionY()));
    
    // CCEaseBounceIn
    CCActionInterval *easeBounceIn = CCEaseBounceIn::create(move);
    pSpr->runAction(easeBounceIn);
    
    // CCEaseBounceOut
    CCActionInterval *easeBounceOut = CCEaseBounceOut::create(move);
    pSpr->runAction(easeBounceOut);
    
    // CCEaseBounceInOut
    CCActionInterval *easeBounceInOut = CCEaseBounceInOut::create(move);
    pSpr->runAction(easeBounceInOut);
    
    // CCEaseBack
    CCActionInterval *move = CCMoveTo::create(5,pSpr->getPositionY()));
    
    // CCEaseBackIn
    CCActionInterval *easeBackIn = CCEaseBackIn::create(move);
    pSpr->runAction(easeBackIn);
    
    // CCEaseBackOut
    CCActionInterval *easeBackOut = CCEaseBackOut::create(move);
    pSpr->runAction(easeBackOut);
    
    // CCEaseElastic
    CCActionInterval *move = CCMoveTo::create(5,pSpr->getPositionY()));
    
    // CCEaseElasticIn
    CCActionInterval *easeElasticIn = CCEaseElasticIn::create(move);
    pSpr->runAction(easeElasticIn);
    
    // CCEaseElasticOut
    CCActionInterval *easeElasticOut = CCEaseElasticOut::create(move);
    pSpr->runAction(easeElasticOut);
    
    // CCEaseElasticInOut
    CCActionInterval *easeElasticInOut = CCEaseElasticInOut::create(move);
    pSpr->runAction(easeElasticInOut);
    
    // CCEaseExponential
    CCActionInterval *move = CCMoveTo::create(5,pSpr->getPositionY()));
    
    // CCEaseExponentialIn
    CCActionInterval *easeExponentialIn = CCEaseExponentialIn::create(move);
    pSpr->runAction(easeExponentialIn);
    
    // CCEaseExponentialOut
    CCActionInterval *easeExponentialOut = CCEaseExponentialOut::create(move);
    pSpr->runAction(easeExponentialOut);
    
    // CCEaseExponentialInOut
    CCActionInterval *easeExponentialInOut = CCEaseExponentialInOut::create(move);
    pSpr->runAction(easeExponentialInOut);
    
    // CCEaseRateAction
    CCActionInterval *move = CCMoveTo::create(5,pSpr->getPositionY()));
    CCActionInterval *easeRateAction = CCEaseRateAction::create(move,0.5);
    pSpr->runAction(easeRateAction);
    
    // CCEaseSine
    CCActionInterval *move = CCMoveTo::create(5,pSpr->getPositionY()));
    
    // CCEaseSineIn
    CCActionInterval *easeSineIn = CCEaseSineIn::create(move);
    pSpr->runAction(easeSineIn);
    
    // CCEaseSineOut
    CCActionInterval *easeSineOut = CCEaseSineOut::create(move);
    pSpr->runAction(easeSineOut);
    
    // CCEaseSineInOut
    CCActionInterval *easeSineInOut = CCEaseSineInOut::create(move);
    pSpr->runAction(easeSineInOut);
    
    // CCSpeed
    CCActionInterval *move = CCMoveTo::create(5,pSpr->getPositionY()));
    CCSpeed *speed = CCSpeed::create(move,10);
    pSpr->runAction(speed);
    
    // CCSpawn
    CCActionInterval *move = CCMoveTo::create(5,pSpr->getPositionY()));
    CCActionInterval *scale = CCScaleTo::create(2,3);
    CCActionInterval *rotate = CCRotateTo::create(4,190);
    CCActionInterval *spawn = CCSpawn::create(move,scale,rotate,NULL);
    pSpr->runAction(spawn);
    
    // CCSequence
    CCActionInterval *move = CCMoveTo::create(5,3);
    CCActionInterval *seq = CCSequence::create(move,NULL);
    pSpr->runAction(seq);
    
    // Reverse CCSequence
    CCActionInterval *move = CCMoveBy::create(5,pSpr->getPositionY()));
    CCActionInterval *scale = CCScaleBy::create(2,NULL);
    CCActionInterval *actionByBack = CCSequence::create(seq,seq->reverse(),NULL);
    pSpr->runAction(actionByBack);
    
    // CCRepeat
    CCActionInterval *move = CCMoveTo::create(5,NULL);
    CCActionInterval *repeat = CCRepeat::create(seq,2);
    pSpr->runAction(repeat);
    
    // CCRepeatForever
    CCActionInterval *move = CCMoveTo::create(5,NULL);
    CCActionInterval *repeatForever = CCRepeatForever::create(seq);
    pSpr->runAction(repeatForever);
    
    // CCCallFunc
    CCActionInterval *move = CCMoveTo::create(1,pSpr->getPositionY()));
    CCCallFunc *funCall = CCCallFunc::create(this,callfunc_selector(HelloWorld::callBackC));
    CCActionInterval *seq = CCSequence::create(move,funCall,NULL);
    pSpr->runAction(seq);
    
    // CCCallFuncN
    CCActionInterval *move = CCMoveTo::create(1,pSpr->getPositionY()));
    CCCallFuncN *funCall = CCCallFuncN::create(this,callfuncN_selector(HelloWorld::callBackN));
    CCActionInterval *seq = CCSequence::create(move,NULL);
    pSpr->runAction(seq);
    
    // CCCallFuncND
    CCActionInterval *move = CCMoveTo::create(1,pSpr->getPositionY()));
    CCCallFuncND *funCall = CCCallFuncND::create(this,callfuncND_selector(HelloWorld::callBackND),(void *)0xbebabeba);
    CCActionInterval *seq = CCSequence::create(move,NULL);
    pSpr->runAction(seq);

    return true;
}

void HelloWorld::callBackC()
{
    CCLOG("HelloWorld::callBackC");
}

void HelloWorld::callBackN()
{
    CCLOG("HelloWorld::callBackN");
}

void HelloWorld::callBackND()
{
    CCLOG("HelloWorld::callBackND");
}

//
// HelloWorldScene.h
//

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

class HelloWorld : public cocos2d::CCLayer
{
public:
    virtual bool init();
    static cocos2d::CCScene* scene();

    CREATE_FUNC(HelloWorld);
};

#endif
//
// HelloWorldScene.cpp
//

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    
    return scene;
}

bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    // Cocos2d-x常用特效
    CCSprite *pSpr = CCSprite::create("Icon-57.png");
    pSpr->setPosition(ccp(pSpr->getContentSize().width*0.5,170));
    this->addChild(pSpr);
    
    // CCShaky3D
    CCActionInterval *shaky3D = CCShaky3D::create(5,CCSizeMake(16,12),true);
    pSpr->runAction(shaky3D);
    
    // CCShakyTiles3D
    CCActionInterval *shakyTiles3D = CCShakyTiles3D::create(5,true);
    pSpr->runAction(shakyTiles3D);
    
    // CCWaves
    CCActionInterval *waves = CCWaves::create(5,3,2.0f,true,true);
    pSpr->runAction(waves);
    
    // CCWavesTiles3D
    CCWavesTiles3D *wavesTiles3D = CCWavesTiles3D::create(5,2.0f);
    pSpr->runAction(wavesTiles3D);
    
    // CCFlipX3D
    CCActionInterval *flipX3D = CCFlipX3D::create(3);
    pSpr->runAction(flipX3D);
    
    // CCFlipY3D
    CCActionInterval *flipY3D = CCFlipY3D::create(3);
    pSpr->runAction(flipY3D);
    
    // CCLens3D
    CCActionInterval *lens3D = CCLens3D::create(5,ccp(winSize.width*0.5,winSize.height*0.5),160);
    pSpr->runAction(lens3D);
    
    // CCRipple3D
    CCActionInterval *ripple3D = CCRipple3D::create(5,160,1.0f);
    pSpr->runAction(ripple3D);
    
    // CCLiquid
    CCActionInterval *liquid = CCLiquid::create(5,1.0f);
    pSpr->runAction(liquid);
    
    // CCTwirl
    CCActionInterval *twirl = CCTwirl::create(5,1.0f);
    pSpr->runAction(twirl);
    
    // CCShuffleTiles
    CCActionInterval *shuffleTiles = CCShuffleTiles::create(5,2);
    pSpr->runAction(shuffleTiles);
    
    // CCShatteredTiles3D
    CCActionInterval *shatteredTiles3D = CCShatteredTiles3D::create(5,25,true);
    pSpr->runAction(shatteredTiles3D);
    
    // CCFadeOutTRTiles
    CCActionInterval *fadeOutTRTTiles = CCFadeOutTRTiles::create(5,12));
    pSpr->runAction(fadeOutTRTTiles);
    
    // CCFadeOutBLTiles
    CCActionInterval *fadeOutBLTiles = CCFadeOutBLTiles::create(5,12));
    pSpr->runAction(fadeOutBLTiles);
    
    // CCFadeOutUpTiles
    CCActionInterval *fadeOutUpTiles = CCFadeOutUpTiles::create(5,12));
    pSpr->runAction(fadeOutUpTiles);
    
    // CCFadeOutDownTiles
    CCActionInterval *fadeOutDownTiles = CCFadeOutDownTiles::create(5,12));
    pSpr->runAction(fadeOutDownTiles);
    
    // CCTurnOffTiles
    CCActionInterval *turnOffTiles = CCTurnOffTiles::create(5,12));
    pSpr->runAction(turnOffTiles);
    
    // CCJumpTiles3D
    CCActionInterval *jumpTiles3D = CCJumpTiles3D::create(5,2.0f);
    pSpr->runAction(jumpTiles3D);
    
    // CCSplitRows
    CCActionInterval *splitRows = CCSplitRows::create(5,3);
    pSpr->runAction(splitRows);
    
    // CCSplitCols
    CCActionInterval *splitCols = CCSplitCols::create(5,3);
    pSpr->runAction(splitCols);
    
    // CCPageTurn3D
    CCActionInterval *pageTurn3D = CCPageTurn3D::create(5,CCSizeMake(15,10));
    pSpr->runAction(pageTurn3D);
    
    return true;
}
原文链接:https://www.f2er.com/cocos2dx/346774.html

猜你在找的Cocos2d-x相关文章