游戏开发之——对话系统(CocosCreator)

栏目: 编程语言 · IOS · 发布时间: 5年前

内容简介:看代码(Typescript):

看代码(Typescript):

import { AnalyzeJSON } from "./AnalyzeJSON";

const {ccclass, property} = cc._decorator;

@ccclass
export class Dialog extends cc.Component {

    @property({type:cc.Float, tooltip:"播放速度"})
    playSpeed:number = 0;
    _speed:number;

    
    _dialogLabel:cc.Label = null;       //文本框
    _contentStr:string = "";            //当前播放的内容
    _currTextIndex:number = 0;          //当前播放内容的索引
    _currDialogIndex:number = 0;        //当前对话列表的索引
    _isPlaying:boolean = false;         //是否正在播放
    
    _contentList:Array<string> = null;  //谈话内容

    _endEvent:Function = null;          //结束处理事件

    onLoad () {
        this._dialogLabel = this.node.getComponentInChildren(cc.Label);
        this.node.on(cc.Node.EventType.TOUCH_END, this._onClickBox, this);
    }

    /**点击跳过打字机效果 */
    _onClickBox():void{
        if(this._isPlaying){
            this._dialogLabel.string = this._contentStr;
            this._reset();
        }else{
            //对话是否结束 TODO
            this._currDialogIndex++;
            if(this._contentList[this._currDialogIndex] == null)
                this._currDialogIndex = 0;
            this.playDialog(this._contentList[this._currDialogIndex]);
        }
    }

    start () {
        this._speed = this.playSpeed;
        this._contentList = AnalyzeJSON._instance.getDialog();    //从管理器里读取要显示的对话
        this.playDialog(this._contentList[this._currDialogIndex]);
    }

    update (dt) {
        //#region 打字机效果
        if(this._isPlaying){
            this._speed -= dt;
            if(this._speed <= 0){
                this._speed = this.playSpeed;
                if(this._contentStr[this._currTextIndex] == null){
                    this._reset();
                }else{
                    this._addText(this._contentStr[this._currTextIndex]);
                    this._currTextIndex++;
                }
            }else return;
        }
        //#endregion
    }

    /**开始对话 */
    playDialog(str:string, callback?:Function):void{
        this._isPlaying = true;
        this._contentStr = str;
        this._clearContent();
        
        if(callback)
            this._endEvent = callback;
    }

    /**设置速度 */
    setSpeed(val:number):void{
        this.playSpeed = val;
    }


    /**重置内容 */
    _reset():void{
        this._isPlaying = false;
        this._contentStr = "";
        this._currTextIndex = 0;
        this._speed = this.playSpeed;
    }

    /**清除对话框内容 */
    _clearContent():void{
        this._dialogLabel.string = "";
    }

    /**添加文字 */
    _addText(aStr:string):void{
        this._dialogLabel.string += aStr;
    }
}

以上所述就是小编给大家介绍的《游戏开发之——对话系统(CocosCreator)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

我知道他想看什么

我知道他想看什么

沙建军 / 中信出版社 / 2018-1 / 48.00

社交媒体迅速发展、信息快速迭代、时间碎片化;大数据、智能终端、物联网横空出世;移动支付、网红经济和传统营销失效,这些都让这个时代的媒体、内容、渠道、产品之间的边界越来越模糊,也从根本上改变了营销的逻辑,内容营销从热词变成趋势,变成营销的底层思维。未来一切都是媒体,形式也是内容。 本书作者通过对国内外36个内容营销的新近案例的故事化描述和透彻分析,提出“组织媒介化”“营销内容化”“内容情趣化”......一起来看看 《我知道他想看什么》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码