Cocos2d-x特效:场景切换效果、控件动作、逐帧动画

时间:2023-02-06 19:03:12

第三阶段:常用功能2

1.Cocos2d-x场景切换
    Size visibleSize = Director::getInstance()->getVisibleSize();
   
   
    Sprite *bg = Sprite::create(
"bg.jpg" );
    bg->setPosition(visibleSize.width/
2 , visibleSize.height/ 2 );
    addChild(bg);
   

    LabelTTF *label = LabelTTF::create(
"Show Next Scene" , "Courier" , 36 );
    addChild(label);
   
    label->setPosition(visibleSize.width/
2 , visibleSize.height/ 2 );
   
    EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = [label,
this ](Touch* t,Event *e){
       
       
if (label->getBoundingBox().containsPoint(t->getLocation())) {
           
            Director::getInstance()->replaceScene(TransitionFadeBL::create(
1 , ImageScene::createScene()));
        }
       
       
return false ;
    };
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, label);
   
   
    return true;


在ImageScene.h中
#include <iostream>
#include
<cocos2d.h>
USING_NS_CC;  
class ImageScene: public Layer {  
public :
    static Scene* createScene();  
    virtual bool init();
    CREATE_FUNC(ImageScene);
};

在ImageScene.cpp中
#include "ImageScene.h"
Scene* ImageScene::createScene(){    
    Scene* scene = Scene::create();
   
ImageScene * layer = ImageScene::create();
    scene->addChild(layer);   
    return scene;
}
bool ImageScene ::init(){    
    Size size = Director::getInstance()->getVisibleSize();
    Sprite *s = Sprite::create(
"HelloWorld.png" );
    s->setPosition(size.width/
2 , size.height/ 2 );
    addChild(s);    
    return true ;
}


2.Cocos2d-x场景切换效果
            Director::getInstance()->replaceScene(TransitionFadeBL::create( 1 , ImageScene::createScene()));

  1. TransitionCrossFade.create(t,scene)   // 交叉消失两个场景使用cc.RenderTexture对象。  
  2. TransitionFad.create(t,scene,color)  // 淡出即将离任的场景,然后消失在传入的场景。  
  3.  
  4. TransitionFadeBL.create(t, scene)  // 向左下波浪退出  
  5. TransitionFadeDown.create(t, scene)  // 向下百叶窗式换场景  
  6. TransitionFadeUp.create(t, scene)  // 向上百叶窗式换场景  

  7. TransitionJumpZoom.create(t, scene)   // 跳跃式替换,场景缩小,再加载进来  
  8. TransitionMoveInB.create(t,scene)  // 创建一个在底部,覆盖当前场景  
  9. TransitionMoveInR.create(t,scene)  // 创建一个在右边,覆盖当前场景  
  10. TransitionMoveInT.create(t,scene)  // 创建一个在左边,覆盖当前场景  
  11. TransitionPageTurn.create(t,scene, backwards)  // 前翻页式场景替换  
  12. TransitionRadialCW.create(t,scene)  // 逆时针切入  
  13. TransitionRotoZoom.create(t,scene)  // 转换角度替换  
  14. TransitionFlipAngular.create(t,scene, o)  // 按一定角度左翻  
  15. TransitionFlipX.create(t, scene,o)  // X轴左边翻换  
  16. TransitionFlipY.create(t,scene, o)  // Y轴左边翻换  
  17. TransitionZoomFlipAngular.create(t,scene, o)   // 带有缩放效果,有角度的转左翻  
  18. TransitionZoomFlipX.create(t,scene, o)   // 带有缩放效果,在X轴左翻  
  19. TransitionZoomFlipY.create(t,scene, o)   // 带有缩放效果,左Y轴左翻  
  20. TransitionShrinkGrow.create(t,scene) //交叉着替换场景  
  21. TransitionSlideInB.create(t,scene) //场景有底部进入  
3.Cocos2d-x动作
instant  /'ɪnst(ə)nt/ 片刻, 顷刻, 刹那/  立即的
Size visibleSize = Director::getInstance()->getVisibleSize();
   
   
auto label = LabelTTF::create( "jikexueyuan" , "Courier" , 30 );
    label->setPosition(visibleSize.width/
2 , visibleSize.height/ 2 );
    addChild(label);
   
   
auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = [label](Touch *t,Event *e){
       
       
if (label->getBoundingBox().containsPoint(t->getLocation())) {
           
//            label->runAction(MoveTo::create(1, Point(100, 100)));
//            label->runAction(MoveBy::create(1, Point(-50, -50))->reverse());
           
//            label->runAction(Spawn::create(MoveBy::create(1, Point(100, 100)), RotateBy::create(1, 360),NULL));
           
//            label->runAction(Sequence::create(MoveBy::create(1, Point(100, 100)),RotateBy::create(1, 360), NULL));
           
           
            label->runAction(Sequence::create(
                                              MoveBy::create(
1 , Point( 100 , 100 )),
                                              RotateBy::create(
1 , 360 ),
                                              CallFunc::create([](){
                MessageBox(
"Action complete" , "complete" );
            }),
NULL ));
        }
       
       
return false ;
    };
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, label);
   
//    label->runAction(Repeat::create(RotateBy::create(1, 180), 3));
//    label->runAction(RepeatForever::create(RotateBy::create(1, 180)))
   
   
return true ;


4.Cocos2d-x动作反转
  label->runAction(MoveBy::create(1, Point(-50, -50))->reverse());
5.Cocos2d-x动作重复
//    label->runAction(Repeat::create(RotateBy::create(1, 180), 3));重复动作三次
//    label->runAction(RepeatForever::create(RotateBy::create(1, 180)));永远重复下去
6.Cocos2d-x动作混合
同时执行:
//            label->runAction(Spawn::create(MoveBy::create(1, Point(100, 100)), RotateBy::create(1, 360),NULL));
7.Cocos2d-x动作序列
顺序执行:
//            label->runAction(Sequence::create(MoveBy::create(1, Point(100, 100)),RotateBy::create(1, 360), NULL));
8.Cocos2d-x动作侦听
RotateBy之后执行 CallFunc:
            label->runAction(Sequence::create(
                                              MoveBy::create(
1 , Point( 100 , 100 )),
                                              RotateBy::create(
1 , 360 ),
                                              CallFunc::create([](){
                MessageBox(
"Action complete" , "complete" );
            }),NULL));

9.Cocos2d-x动画
  auto cache = SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile(
"anim.plist" );
   
    Vector<SpriteFrame*> vec;
   
char name[ 15 ];
    memset(name,
0 , 15 );
   
   
for ( int i= 0 ; i< 20 ; i++) {
        sprintf(name,
"anim%04d" ,i);
        vec.pushBack(cache->getSpriteFrameByName(name));
    }
    //每秒十帧
    Animation *animation = Animation::createWithSpriteFrames(vec, 0.1f );
    Animate *animate = Animate::create(animation);
   
   
auto sprite = Sprite::create();
    addChild(sprite);
    sprite->setPosition(
200 , 200 );
    sprite->runAction(RepeatForever::create(animate));