您当前位置:主页 > 程序开发 >
 
[cocos2dx笔记007]一个自定义场景切换的实例
日期:2019-01-28 编辑:admin

cocos2dx框架已经提供了很多场景切换的类,但是一些自定义的场景切换,只有自己实现了。下面是实现的类。这里设计的分辨率是750*500.请根据实际的要求调整。
头文件
#ifndef _TRANSITION_GAME_H_
#define _TRANSITION_GAME_H_
#include <cocos2d.h>
namespace cocos2d 
{
    class CCTransitionGame : public CCTransitionScene
    {
    public:
        CCTransitionGame();
        virtual ~CCTransitionGame();
        void onEnter();
        static CCTransitionGame * create(float t, CCScene *scene);
    private:
        void LRFinish(void);
        void OnFirstActionFinish(void);
    private:
        int m_FinishCnt;
    };
}
#endif 源文件
#include "TransitionGame.h"
#include "xlog.h"
#include <xstring.h>
namespace cocos2d
{
    using namespace zdh;
    CCTransitionGame * CCTransitionGame::create(float t, CCScene *scene)
    {
        CCTransitionGame * pScene = new CCTransitionGame();
        if (pScene && pScene->initWithDuration(t, scene))
        {
            pScene->autorelease();
            return pScene;
        }
        CC_SAFE_DELETE(pScene);
        return NULL;
    }

    CCTransitionGame::CCTransitionGame()
    {
        m_FinishCnt = 0;
    }

    CCTransitionGame::~CCTransitionGame()
    {
    }

    void CCTransitionGame::onEnter()
    {
        CCTransitionScene::onEnter();
        CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();


        CCPoint stLeftBegin, stLeftEnd, stRightBegin, stRightEnd;
        //设置左边的起点和终点
        stLeftBegin.setPoint(-436.0f, -60);
        stLeftEnd.setPoint(visibleSize.width / 2.0f + stLeftBegin.x, -60.0f);
        //设置右边的起点和终点
        stRightBegin.setPoint(visibleSize.width, -60.0f);
        stRightEnd.setPoint(visibleSize.width / 2.0f, -60.0f);
        //加载动画序列
        CCSpriteFrameCache* pCache = CCSpriteFrameCache::sharedSpriteFrameCache();
        pCache->addSpriteFramesWithFile("middle_ani_1.plist");
        pCache->addSpriteFramesWithFile("middle_ani_2.plist");
        //生成画动图片列表和动画对象
        CCArray* pAnimFrames = CCArray::createWithCapacity(69);
        XAnsiString strAniName;
        for (int i = 1; i < 70; i++)
        {
            strAniName.printf("light%04d.png", i);
            pAnimFrames->addObject(pCache->spriteFrameByName(strAniName.c_str()));
        }
        CCAnimation* animation = CCAnimation::createWithSpriteFrames(pAnimFrames, this->m_fDuration * 2.0f/3.0f/69.0f );
        

        CCNode * pNode = CCNode::create(); //这个有两个子节点,一个是左边交换图片,一个是中间的动画,用于一起做移动的Action
        CCSprite* pLeft = CCSprite::createWithSpriteFrameName("swap_left.png");
        pLeft->setAnchorPoint(CCPointZero);
        pNode->addChild(pLeft);

        CCSprite * pMiddle = CCSprite::create();  //显示动画
        pMiddle->setAnchorPoint(CCPointZero);
        pMiddle->setPosition(ccp(436.0f - 69.0f, 250.0f + 60.0f - 72.0f));
        pMiddle->runAction(CCAnimate::create(animation));
        pNode->addChild(pMiddle);

        pNode->setAnchorPoint(ccp(0,0));
        pNode->setPosition(stLeftBegin);
        this->addChild(pNode,1);

        //右边的交换图片
        CCSprite* pRight = CCSprite::createWithSpriteFrameName("swap_right.png");
        pRight->setPosition(stRightBegin);
        pRight->setAnchorPoint(CCPointZero);
        this->addChild(pRight, 0);

        //定义动作
        //左边的向右移动活动
        CCMoveTo* pActionLeft = CCMoveTo::create(m_fDuration / 3, stLeftEnd);
        //右边的向左移动活动
        CCMoveTo * pActionRight = CCMoveTo::create(m_fDuration / 3, stRightEnd);
        //原地不动
        CCMoveTo* pActionLeft1 = CCMoveTo::create(m_fDuration / 3, stLeftEnd);
        CCMoveTo * pActionRight1 = CCMoveTo::create(m_fDuration / 3, stRightEnd);
        
        CCMoveTo* pActionLeft2 = CCMoveTo::create(m_fDuration / 3, stLeftBegin);
        CCMoveTo * pActionRight2 = CCMoveTo::create(m_fDuration / 3, stRightBegin);

        m_FinishCnt = 0;
        pNode->runAction(CCSequence::create(pActionLeft, CCCallFunc::create(this, callfunc_selector(CCTransitionGame::OnFirstActionFinish)), pActionLeft1, pActionLeft2, CCCallFunc::create(this, callfunc_selector(CCTransitionGame::LRFinish)), NULL));
        pRight->runAction(CCSequence::create(pActionRight, pActionRight1,pActionRight2, CCCallFunc::create(this, callfunc_selector(CCTransitionGame::LRFinish)), NULL));
    }

    void CCTransitionGame::LRFinish(void)
    {
        //所以的活动完成后,要执行场行的Finish
        m_FinishCnt++;
        if (m_FinishCnt >= 2)
        {
            CCTransitionScene::finish();
        }
    }

    void CCTransitionGame::OnFirstActionFinish(void)
    {
        //打开门之前,关闭显示第一个场景,显示第二个场景
        m_pInScene->setVisible(true);
        m_pOutScene->setVisible(false);
    }

} 用到的资源
/Files/zdhsoft/plist.zip 效果图:

http://www.bkjia.com/Androidjc/821918.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/821918.htmlTechArticlecocos2dx框架已经提供了很多场景切换的类,但是一些自定义的场景切换,只有自己实现了。下面是实现的类。这里设计的分辨率是750*500.请根...

本文源自: AG环亚娱乐

上一篇:Android多线程断点续传下载 下一篇:没有了
 
Copyright © 2005-2016 http://www.tecsquared.com AG环亚娱乐_AG直营网_环亚娱乐AG最佳线路_腾讯体育版权所有 AG环亚娱乐_AG直营网_环亚娱乐AG最佳线路_腾讯体育