8 cocos2dx添加场景切换效果,控制场景切换彻底完成之后再执行动画

8 cocos2dx添加场景切换效果,控制场景切换彻底完成之后再执行动画,第1张

概述 1 添加场景切换效果 供场景切换的类: CCTransitionJumpZoom CCTransitionProgressRadialCCW CCTransitionProgressRadialCW CCTransitionProgressHorizontal CCTransitionProgressVertical CCTransitionProgressInOut CCTransitio 

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(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",21); Font-family:新宋体; Font-size:9.5pt">"T03LayerZorder.h",21); Font-family:新宋体; Font-size:9.5pt">"T04colorLayer.h",21); Font-family:新宋体; Font-size:9.5pt">"T05Coordinate.h",21); Font-family:新宋体; Font-size:9.5pt">"T06BatchNode.h",21); Font-family:新宋体; Font-size:9.5pt">"T07Schedule.h",21); Font-family:新宋体; Font-size:9.5pt">"T08touch.h",21); Font-family:新宋体; Font-size:9.5pt">"T09Memory.h",21); Font-family:新宋体; Font-size:9.5pt">"T10Label.h",21); Font-family:新宋体; Font-size:9.5pt">"T11Ccmenu.h",21); Font-family:新宋体; Font-size:9.5pt">"T12UI.h",21); Font-family:新宋体; Font-size:9.5pt">"T13Action.h",21); Font-family:新宋体; Font-size:9.5pt">"T14Animation.h",21); Font-family:新宋体; Font-size:9.5pt">"T15Speed.h",21); Font-family:新宋体; Font-size:9.5pt">"T16CCFollow.h",21); Font-family:新宋体; Font-size:9.5pt">"T17CCProgressto.h",21); Font-family:新宋体; Font-size:9.5pt">"T18GrID3D.h",21); Font-family:新宋体; Font-size:9.5pt">"T19Draw.h",21); Font-family:新宋体; Font-size:9.5pt">"T20Music.h",21); Font-family:新宋体; Font-size:9.5pt">"T21Particle.h",21); Font-family:新宋体; Font-size:9.5pt">"T22UserDefault.h"

};

CCScene * TMenu::scene()

{

scene = CCScene::create();

TMenu * layer = create();

scene->addChild(layer);

return scene;

}

bool 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(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,closeCallback));

Ccmenu * menu2 = create(closeItem,138); Font-family:新宋体; Font-size:9.5pt">NulL);

menu2->setposition(CCPointZero);

closeItem->setposition(ccp(winSize.wIDth - closeItem->getContentSize().wIDth / 2,138); Font-family:新宋体; Font-size:9.5pt">winSize.height - closeItem->getContentSize().height / 2));

addChild(menu2);

settouchEnabled(true);

settouchMode(kCCtouchesOneByOne);

return true;

}

voID CCObject * sender)

{

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 CCObject * sender)

{

exit(0);

}

bool CCEvent *pEvent)

{

return true;

}

voID CCEvent *pEvent)

{

CCRect rcWin = CCRect(0,40,138); Font-family:新宋体; Font-size:9.5pt">winSize.wIDth,138); Font-family:新宋体; Font-size:9.5pt">winSize.height-80);

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

CcmenuItem * lastItem = (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);

}

运行效果:

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

voID onEnter();

voID onEnterTransitionDIDFinish();

案例:

#include "AppMacros.h"

CCScene *T14Animation * layer = TBack::init();

return true;

}

//在进入场景的时候做以下 *** 作

voID onEnter()

{

onEnter();

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

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

//设置精灵的显示位置

spr->winSize.wIDth / 2,138); Font-family:新宋体; Font-size:9.5pt">winSize.height / 2));

addChild(spr);

}

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

voID onEnterTransitionDIDFinish()

{

onEnterTransitionDIDFinish();

//pList中是图片信息

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithfile("animation/plant.pList");

//创建动画

CCAnimation * animation = CCAnimation::create();

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

char nameBuf[100];

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

{

memset(nameBuf,sizeof(nameBuf));

sprintf(nameBuf,21); Font-family:新宋体; Font-size:9.5pt">"p_2_0%d.png",i + 1);

animation->addSpriteFrame(spriteFrameByname(nameBuf));

}

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

animation->setDelayPerUnit(0.1f);

//这只循环两次

animation->setLoops(2);

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

spr->runAction(animate);

}

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

总结

以上是内存溢出为你收集整理的8 cocos2dx添加场景切换效果,控制场景切换彻底完成之后再执行动画全部内容,希望文章能够帮你解决8 cocos2dx添加场景切换效果,控制场景切换彻底完成之后再执行动画所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1007224.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存