8 cocos2dx加入场景切换效果,控制场景切换彻底完毕之后再运行动画

时间:2021-09-21 15:20:21


1
加入场景切换效果

供场景切换的类:

CCTransitionJumpZoom

CCTransitionProgressRadialCCW

CCTransitionProgressRadialCW

CCTransitionProgressHorizontal

CCTransitionProgressVertical

CCTransitionProgressInOut

CCTransitionProgressOutIn

CCTransitionCrossFade

CCTransitionFadeTR

CCTransitionFadeBL

CCTransitionFadeUp

CCTransitionFadeDown

CCTransitionTurnOffTiles

CCTransitionSplitRows

CCTransitionSplitCols

CCTransitionFade

CCTransitionShrinkGrow

CCTransitionRotoZoom

CCTransitionMoveInL

CCTransitionMoveInR

CCTransitionMoveInT

CCTransitionMoveInB

CCTransitionSlideInL

CCTransitionSlideInR

CCTransitionSlideInT

CCTransitionSlideInB

场景切换案例:

TMenu.h

#ifndef
__TMenu_H__

#define
__TMenu_H__

#include
"cocos2d.h"

USING_NS_CC;

class
TMenu:public
CCLayer

{

public:

static
CCScene *
scene();

CREATE_FUNC(TMenu);

bool
init();

CCMenu *
menu;

void
menuCallback(CCObject
* sender);

void
closeCallback(CCObject
* sender);

virtual
bool
ccTouchBegan(CCTouch
*pTouch,
CCEvent *pEvent);

virtual
void
ccTouchMoved(CCTouch
*pTouch,
CCEvent *pEvent);

};

#endif

TMenu.cpp

#include
"TMenu.h"

#include
"AppMacros.h"

#include
"T01LayerAnchorPoint.h"

#include
"T02LayerSprite.h"

#include
"T03LayerZorder.h"

#include
"T04ColorLayer.h"

#include
"T05Coordinate.h"

#include
"T06BatchNode.h"

#include
"T07Schedule.h"

#include
"T08Touch.h"

#include
"T09Memory.h"

#include
"T10Label.h"

#include
"T11CCMenu.h"

#include
"T12UI.h"

#include
"T13Action.h"

#include
"T14Animation.h"

#include
"T15Speed.h"

#include
"T16CCFollow.h"

#include
"T17CCProgressTo.h"

#include
"T18Grid3D.h"

#include
"T19Draw.h"

#include
"T20Music.h"

#include
"T21Particle.h"

#include
"T22UserDefault.h"

//供场景切换用

static CCTransitionScene * switchSceneEffect(int index,float t,CCScene *s)

{

switch (index)

{

case 0:

return CCTransitionJumpZoom::create(t, s);

break;

case 1:

return CCTransitionProgressRadialCCW::create(t, s);

break;

case 2:

return CCTransitionProgressRadialCW::create(t, s);

break;

case 3:

return CCTransitionProgressHorizontal::create(t, s);

break;

case 4:

return CCTransitionProgressVertical::create(t, s);

break;

case 5:

return CCTransitionProgressInOut::create(t, s);

break;

case 6:

return CCTransitionProgressOutIn::create(t, s);

break;

case 7:

return CCTransitionCrossFade::create(t, s);

break;

case 8:

return CCTransitionCrossFade::create(t, s);

break;

case 9:

return CCTransitionFadeTR::create(t, s);

break;

case 10:

return CCTransitionFadeBL::create(t ,s);

break;

case 11:

return CCTransitionFadeUp::create(t, s);

break;

case 12:

return CCTransitionFadeDown::create(t, s);

break;

case 13:

return CCTransitionTurnOffTiles::create(t, s);

break;

case 14:

return CCTransitionSplitRows::create(t, s);

break;

case 15:

return CCTransitionSplitCols::create(t, s);

break;

case 16:

return CCTransitionFade::create(t, s);

break;

case 17:

return CCTransitionShrinkGrow::create(t, s);

break;

case 18:

return CCTransitionRotoZoom::create(t, s);

break;

case 19:

return CCTransitionMoveInL::create(t, s);

break;

case 20:

return CCTransitionMoveInR::create(t, s);

break;

case 21:

return CCTransitionMoveInT::create(t, s);

break;

case 22:

return CCTransitionMoveInB::create(t, s);

break;

case 23:

return CCTransitionSlideInL::create(t, s);

break;

case 24:

return CCTransitionSlideInR::create(t, s);

break;

case 25:

return CCTransitionSlideInT::create(t, s);

break;

case 26:

return CCTransitionSlideInB::create(t, s);

break;

}

}

static
char *
menu_array[]

{

"T01LayerAnchorPoint.h",

"T02LayerSprite.h",

"T03LayerZorder.h",

"T04ColorLayer.h",

"T05Coordinate.h",

"T06BatchNode.h",

"T07Schedule.h",

"T08Touch.h",

"T09Memory.h",

"T10Label.h",

"T11CCMenu.h",

"T12UI.h",

"T13Action.h",

"T14Animation.h",

"T15Speed.h",

"T16CCFollow.h",

"T17CCProgressTo.h",

"T18Grid3D.h",

"T19Draw.h",

"T20Music.h",

"T21Particle.h",

"T22UserDefault.h"

};

CCScene *
TMenu::scene()

{

CCScene *
scene =
CCScene::create();

TMenu *
layer =
TMenu::create();

scene->addChild(layer);

return
scene;

}

bool
TMenu::init()

{

CCLayer::init();

menu =
CCMenu::create();

for (int
i = 0;
i <
sizeof(menu_array)
/ sizeof(*menu_array);
i++)

{

CCMenuItem *
item =
CCMenuItemFont::create(menu_array[i],

this,

menu_selector(TMenu::menuCallback));

menu->addChild(item);

}

menu->alignItemsVertically();

addChild(menu);

CCArray * array =
menu->getChildren();

CCObject *
obj;

int
i = 0;

CCARRAY_FOREACH(array,obj)

{

CCMenuItem *
item = (CCMenuItem*)obj;

item->setTag(i
+ 1 + 10000);

i++;

}

CCMenuItem *
closeItem =
CCMenuItemImage::create("CloseNormal.png",
"CloseSelected.png");

closeItem->setTarget(this,
menu_selector(TMenu::closeCallback));

CCMenu *
menu2 =
CCMenu::create(closeItem,
NULL);

menu2->setPosition(CCPointZero);

closeItem->setPosition(ccp(winSize.width
- closeItem->getContentSize().width
/ 2,

winSize.height
- closeItem->getContentSize().height
/ 2));

addChild(menu2);

setTouchEnabled(true);

setTouchMode(kCCTouchesOneByOne);

return
true;

}

void
TMenu::menuCallback(CCObject
* sender)

{

CCMenuItem *
item = (CCMenuItem
*)sender;

CCScene *
pScene =
NULL;

switch (item->getTag()-10000)

{

case 1:

pScene =
T01LayerAnchorPoint::scene();

break;

case 2:

pScene =
T02LayerSprite::scene();

break;

case 3:

pScene =
T03LayerZorder::scene();

break;

case 4:

pScene =
T04ColorLayer::scene();

break;

case 5:

pScene =
T05Coordinate::scene();

break;

case 6:

pScene =
T06BatchNode::scene();

break;

case 7:

pScene =
T07Schedule::scene();

break;

case 8:

pScene =
T08Touch::scene();

break;

case 9:

pScene =
T09Memory::scene();

break;

case 10:

pScene =
T10Label::scene();

break;

case 11:

pScene =
T11CCMenu::scene();

break;

case 12:

pScene =
T12UI::scene();

break;

case 13:

pScene =
T13Action::scene();

break;

case 14:

pScene =
T14Animation::scene();

break;

case 15:

pScene =
T15Speed::scene();

break;

case 16:

pScene =
T16CCFollow::scene();

break;

case 17:

pScene =
T17CCProgressTo::scene();

break;

case 18:

pScene =
T18Grid3D::scene();

break;

case 19:

pScene =
T19Draw::scene();

break;

case 20:

pScene =
T20Music::scene();

break;

case 21:

pScene =
T21Particle::scene();

break;

case 22:

pScene =
T22CCUserDefault::scene();

break;

}

//以下的代码用于做控制场景切换用

if (pScene != NULL)

{

static int idx = 0;

CCTransitionScene * scene = switchSceneEffect(idx++, 2, pScene);

if (idx == 27)

{

idx = 0;

}

CCDirector::sharedDirector()->replaceScene(scene);

}

}

void
TMenu::closeCallback(CCObject
* sender)

{

exit(0);

}

bool
TMenu::ccTouchBegan(CCTouch
*pTouch,
CCEvent *pEvent)

{

return
true;

}

void
TMenu::ccTouchMoved(CCTouch
*pTouch,
CCEvent *pEvent)

{

CCRect
rcWin =
CCRect(0, 40,
winSize.width,
winSize.height-80);

CCMenuItem *
firstItem = (CCMenuItem
*)menu->getChildren()->objectAtIndex(0);

CCMenuItem *
lastItem = (CCMenuItem
*)menu->getChildren()->lastObject();

CCPoint
firstInWorld =
menu->convertToWorldSpace(firstItem->getPosition());

CCPoint
lastInWorld =
menu->convertToWorldSpace(lastItem->getPosition());

if (rcWin.containsPoint(firstInWorld)&&pTouch->getDelta().y<0)

return;

if (rcWin.containsPoint(lastInWorld)
&& pTouch->getDelta().y
>0)

return;

menu->setPositionY(menu->getPositionY()
+ pTouch->getDelta().y);

}

执行效果:

8 cocos2dx加入场景切换效果,控制场景切换彻底完毕之后再运行动画

2
有时候我们想等场景切换彻底完毕之后再让动画播放,这时候须要做重写下面代码:

void
onEnter();

void
onEnterTransitionDidFinish();

案例:

#include
"T14Animation.h"

#include
"AppMacros.h"

CCScene *T14Animation::scene()

{

CCScene *
scene =
CCScene::create();

T14Animation *
layer =
T14Animation::create();

scene->addChild(layer);

return
scene;

}

bool
T14Animation::init()

{

TBack::init();

return
true;

}

//在进入场景的时候做下面操作

void
T14Animation::onEnter()

{

TBack::onEnter();

//以图片的方式创建一个精灵

spr =
CCSprite::create("animation/p_2_01.png");

//设置精灵的显示位置

spr->setPosition(ccp(winSize.width
/ 2, winSize.height
/ 2));

addChild(spr);

}

//当场景切换彻底完毕之后运行的动作。

void
T14Animation::onEnterTransitionDidFinish()

{

TBack::onEnterTransitionDidFinish();

//plist中是图片信息

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/plant.plist");

//创建动画

CCAnimation *
animation =
CCAnimation::create();

//这个用于存储图片的名字

char 
nameBuf[100];

for (int
i = 0;
i < 8;
i++)

{

memset(nameBuf,
0, sizeof(nameBuf));

sprintf(nameBuf,
"p_2_0%d.png",
i + 1);

animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf));

}

//设置每次动画运行的时候的延时

animation->setDelayPerUnit(0.1f);

//这仅仅循环两次

animation->setLoops(2);

CCAnimate *
animate =
CCAnimate::create(animation);

spr->runAction(animate);

}

上面案例在场景彻底切换完毕之后新场景中的动画等效果才运行。

8 cocos2dx加入场景切换效果,控制场景切换彻底完毕之后再运行动画的更多相关文章

  1. jQuery实现多种切换效果的图片切换的五款插件

    1:Nivo SliderNivoslider:丰富的图片切换效果 官方网址:https://themeisle.com/plugins/nivo-slider 查看演示:https://www.he ...

  2. iOS7之定制View Controller切换效果

    在iOS5和iOS6前,View Controller的切换主要有4种: 1. Push/Pop,NavigationViewController常干的事儿 2. Tab,TabViewControl ...

  3. iOS开发之虾米音乐频道选择切换效果分析与实现

    今天博客的内容比较简单,就是看一下虾米音乐首页中频道选择的一个动画效果的实现.之前用mask写过另外一种Tab切换的一种效果,网易云音乐里边的一种Tab切换效果,详情请移步于"视错觉:从一个 ...

  4. Android-Activity的切换效果

    Android-Activity的切换效果 Android-Activity的切换效果 Activity有一个默认的切换效果,但是有时候单一的切换效果未免单调,Activity的切换效果也是我们可以自 ...

  5. cocos2dx基础篇&lpar;24&rpar; 场景切换效果CCTransitionScene

    [3.x]     (1)去掉 "CC"     (2)卡牌翻转 TransitionFlip 中的样式 tOrientation // //1: kCCTransitionOri ...

  6. Learning Cocos2d-x for WP8(6)——场景切换和场景过渡效果

    原文:Learning Cocos2d-x for WP8(6)--场景切换和场景过渡效果 C#(wp7)兄弟篇 Learning Cocos2d-x for XNA(6)——场景切换和场景过渡效果 ...

  7. cocos2d-x 2&period;x版本中,场景切换各方法调用顺序

    假设从A场景切换到B场景,调用各场景方法的顺序为: 如果没有切换效果(transition),则先调用B的init(),再调用A的onExitTransitionStart(),接着调用A的onExi ...

  8. ‎Cocos2d-x 学习笔记&lpar;3&period;1&rpar; Scene 场景与场景切换

    1. Scene 简介 游戏中我们看到/看不到的所有元素都是展示在场景之Scene上. 我们可以把场景比作放在地上的没盖纸箱,层Layer是纸箱里堆放的玻璃,Sprite等元素画在玻璃Layer上,这 ...

  9. Cocos2d-x学习笔记(九)场景切换

    这里只介绍两个简单的两个场景相互切换的方法. void pushScene(Scene *sn)和void popScene(Scene *sn)是一对.前者将当前场景放入到场景堆栈中,并切换到下一个 ...

随机推荐

  1. &lbrack;LeetCode&rsqb; Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  2. 如何设置Java开发环境

    傻瓜式 设置Java开发环境 跟我来. 对与win7操作系统.有两种方法. 第一种: 1.下载JDK,完成安装. 2.右键我的电脑,选择属性,在左边找到高级系统设置左键单击打开,在高级选项中找到 环境 ...

  3. Linux服务器开机没响应,BIOS信息都没有

    于2015-10-16,记得是4月份装的服务器,上边ineedle都部署完毕,当时没有派上用场,这次华为测试需要一台ineedle测试机,便把这个安装好的ineedle请出来了,插上电源后,接上网线, ...

  4. web安全及防护

    本文将对web方面的安全问题以及相应的防护方法进行一个简单的介绍. SQL注入(SQL Injection) 原理:就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺 ...

  5. Linux diable ipv6

    在RHEL 5下面测试成功     linux下面禁止ipv6的方法: 来自: 杨志刚 博客 (http://yangzhigang.cublog.cn)   这里我所做的是Redhat Linux, ...

  6. Scrapy安装问题

    按照说明直接使用pip install scrapy会有两个问题: fatal error: 'ffi.h' file not found fatal error: 'libxml/xmlversio ...

  7. Kafka&sol;Zookeeper集群的实现&lpar;二&rpar;

    [root@kafkazk1 ~]# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12. ...

  8. 前端使用 validate &comma; 根据条件进行动态的验证添加

    需求如下: 审核操作的时候,选择“通过” 就不需要验证审核意见,但是选择的是“不通过”,那么需要进行审核意见验证 <script> $(function () { InitValidate ...

  9. chrome如何在选项卡打开网页

    1.鼠标中间点击,会打开新选项卡 2.按住ctrl,然后鼠标点击,也是可以的 3.

  10. Unity经验之谈-DoTween动画结束匿名委托之巨坑

    产生问题: 成百上千个物体放在List列表里面循环,每个物体都要使用移动和移动结束事件. BUG: 动画结束之后我想隐藏该物体,结果却没有正常的隐藏,代码如下 foreach (var item in ...