在cocos2d-xna中加入闪屏

用过ios中cocos2d的人都知道,我们在运行默认代码中,会看到一个闪屏+淡入的效果。而现在版本的cocos2d-xna是不带这个效果的,我觉得为了和绝大多数的wp7程序统一,还是应当加上闪屏的效果。

于是,我决定仿照ios版cocos2d来做一个cocos2d-xna的闪屏。

像是这样

OK,假定我们的闪屏场景叫做Splash,闪屏之后的菜单场景叫做MainMenuScene。

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;

namespace ChalkBall.Scene
{
    class SplashScene : CCScene
    {
        public override void onEnter()
        {
            base.onEnter();
            this.addChild(Splash.node());
        }
    }
    class Splash : CCLayer
    {
        CCDelayTime delayTime;
        public override bool init()
        {
            if (!base.init())
                return false;
            CCDirector.sharedDirector().deviceOrientation = ccDeviceOrientation.CCDeviceOrientationPortrait;
            CCSprite splash = CCSprite.spriteWithFile(@"images/Splash");
            splash.position = new CCPoint(240, 400);
            this.addChild(splash);
            delayTime = CCDelayTime.actionWithDuration(3);
            splash.runAction(delayTime);
            this.schedule(mackTranstion, 1);
            return true;
        }

        void mackTranstion(float dt)
        {
            if (delayTime.isDone())
            {
                MainMenuScene ms = new MainMenuScene();
                CCScene pScene = CCTransitionFade.transitionWithDuration(0.8f, ms);
                CCDirector.sharedDirector().replaceScene(pScene);
            }
        }

        public static new CCLayer node()
        {
            Splash layer = new Splash();
            if (layer.init())
            {
                return layer;
            }
            else
            {
                layer = null;
            }
            return layer;
        }
    }
}
复制代码

需要解释的是delayTime = CCDelayTime.actionWithDuration(3);为闪屏停留的时间,CCTransitionFade为过渡时的特效,当然如果你不喜欢这个特效可以换成其他的。

这个闪屏的本质其实就是两个场景之间的切换,在闪屏场景中设定一个schedule,当时间到了之后进行切换。所以如果你想为场景间的切换增加特效,可以参考次方法,需要注意的是场景的实例化次数。

这样一个闪屏的效果就简单的完成了。

本文地址:http://www.cnblogs.com/jacklandrin/archive/2012/09/15/2687034.html

欢迎大家拍砖~

posted @   倾剑飞血  阅读(579)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示