cvmachine.com - 申博开户网

查找: 您的方位主页 > 网络频道 > 阅览资讯:游戏开发之——对话体系(CocosCreator)

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

2019-03-27 13:11:11 来历:www.cvmachine.com 【

看代码(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;
  }
}

 

 
 

本文地址:http://www.cvmachine.com/a/question/99725.html
Tags: 体系 发之 游戏
修正:申博开户网
关于咱们 | 联络咱们 | 友情链接 | 网站地图 | Sitemap | App | 回来顶部