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

前端之家收集整理的这篇文章主要介绍了Cocos2d-x特效:场景切换效果、控件动作、逐帧动画前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

第三阶段:常用功能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 );

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中
"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 );
addChild(s);
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)@H_882_301@//淡出即将离任的场景,然后消失在传入的场景。
  3. TransitionFadeBL.create(t,scene)@H_882_301@//向左下波浪退出
  4. TransitionFadeDown.create(t,0); background-color:inherit">//向下百叶窗式换场景
  5. TransitionFadeUp.create(t,0); background-color:inherit">//向上百叶窗式换场景

  6. TransitionJumpZoom.create(t,0); background-color:inherit">//跳跃式替换,场景缩小,再加载进来
  7. TransitionMoveInB.create(t,0); background-color:inherit">//创建一个在底部,覆盖当前场景
  8. TransitionMoveInR.create(t,0); background-color:inherit">//创建一个在右边,覆盖当前场景
  9. TransitionMoveInT.create(t,0); background-color:inherit">//创建一个在左边,覆盖当前场景
  10. TransitionPageTurn.create(t,backwards)@H_882_301@//前翻页式场景替换
  11. TransitionRadialCW.create(t,0); background-color:inherit">//逆时针切入
  12. TransitionRotoZoom.create(t,0); background-color:inherit">//转换角度替换
  13. TransitionFlipAngular.create(t,o)@H_882_301@//按一定角度左翻
  14. TransitionFlipX.create(t,0); background-color:inherit">//X轴左边翻换
  15. TransitionFlipY.create(t,0); background-color:inherit">//Y轴左边翻换
  16. TransitionZoomFlipAngular.create(t,0); background-color:inherit">//带有缩放效果,有角度的转左翻
  17. TransitionZoomFlipX.create(t,0); background-color:inherit">//带有缩放效果,在X轴左翻
  18. TransitionZoomFlipY.create(t,0); background-color:inherit">//带有缩放效果,左Y轴左翻
  19. TransitionShrinkGrow.create(t,0); background-color:inherit">//交叉着替换场景
  20. TransitionSlideInB.create(t,0); background-color:inherit">//场景有底部进入
3.Cocos2d-x动作
instant /'ɪnst(ə)nt/ 片刻,顷刻,刹那/ 立即的
Size visibleSize = Director::getInstance()->getVisibleSize();

auto label = LabelTTF::create( "jikexueyuan" ,216)">30 );
label->setPosition(visibleSize.width/
2 );
addChild(label);

auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [label](Touch *t,162)">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,100)),RotateBy::create(1,360),NULL));

// label->runAction(Sequence::create(MoveBy::create(1,NULL));


label->runAction(Sequence::create(
MoveBy::create(
100 ,216)">100 )),
RotateBy::create(
360 ),
CallFunc::create([](){
MessageBox(
"Action complete" ,27)">"complete" );
}),162)">NULL
));
}

false ;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,label);
// label->runAction(Repeat::create(RotateBy::create(1,180),3));
// label->runAction(RepeatForever::create(RotateBy::create(1,180)))

true ;


4.Cocos2d-x动作反转
label->runAction(MoveBy::create(1,-50))->reverse());
5.Cocos2d-x动作重复
6.Cocos2d-x动作混合
同时执行:

8.Cocos2d-x动作侦听
RotateBy之后执行 CallFunc:
label->runAction(Sequence::create(
MoveBy::create(
"complete" );
}),NULL));

9.Cocos2d-x动画
auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile(
"anim.plist" );

Vector<SpriteFrame*> vec;
char name[ 15 ];
memset(name,216)">0
,216)">15 );

for ( int i= 0 ; i< 20 ; i++) {
sprintf(name,27)">"anim%04d"
,i);
vec.pushBack(cache->getSpriteFrameByName(name));
}
//每秒十帧
Animation *animation = Animation::createWithSpriteFrames(vec,216)">0.1f );
Animate *animate = Animate::create(animation);

auto sprite = Sprite::create();
addChild(sprite);
sprite->setPosition(
200 ,216)">200 );
sprite->runAction(RepeatForever::create(animate));

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