【问题标题】:How to get string from .txt file and fill it to Label String in Cocos CreatorCocos Creator中如何从.txt文件中获取字符串并填充到标签字符串中
【发布时间】:2022-11-29 11:43:40
【问题描述】:

我想用 Cocos Creator 中 sometext.txt 文件中的文本填充标签字符串。

我这样做了:

@property({type:Node})
cptxt = null;
.
.
.
this.copyrightText = jsb.fileUtils.getStringFromFile('Text/copyright.txt');
this.cptxt.string = this.copyrightText;

我在场景中用标签填充了cptxt。 但它不起作用。标签字符串未填充。 我在安卓上运行

【问题讨论】:

    标签: string cocoscreator


    【解决方案1】:

    您可以使用“resources.load()”获取文本资产,然后使用“.toString()”获取文本对象的字符串表示形式。

    附言要使用“resources.load()”,您必须在项目目录的资产文件夹下创建一个名为“resources”的文件夹,然后在“resources”下您可以创建子文件夹“Text”,并在该文件夹中放入您的 copyright.txt文件

    如果您尝试直接获取 Label 组件的字符串属性,则 cptxt 属性的类型也应为“Label”

    import {. . ., Label} from 'cc';
    
     @property({type:Label})
        cptxt: Label = null;
        .
        .
        .
    resources.load("Text/copyright", (err, textAsset) => {
        this.cptxt.string = textAsset.toString();;
        });
    

    【讨论】:

    • 这有效,只需要改变这个:(this.cptxt.string = this.copyright Text;)到(this.cpt.string = textAsset;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2015-08-06
    • 2013-01-10
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    相关资源
    最近更新 更多