【问题标题】:Implicit Coercion of a value of a type of text.flash:TextField to an unrelated Int将 text.flash:TextField 类型的值隐式强制转换为不相关的 Int
【发布时间】:2016-04-27 12:17:10
【问题描述】:

好的,所以我正在尝试创建一个移动 Flash 游戏(我主要是一名动画师,一个讲故事的人),但我在输入玩家姓名的文本时遇到了问题。我经常在这个网站上阅读有用的提示,所以我注册以获得一些帮助。我用来加载和保存数据的代码是 saveDataObject,但据我所知,输入文本必须与包代码一起使用。我试图将其转换为函数 var,但随后出现这些错误。我不确定如何使用 Package 类代码,而且我读过的所有内容都令人困惑。通过教程和论坛,我几乎是自学了所有关于代码的知识,所以如果没有以我能理解的方式解释,我将无法做到......

如果我不清楚,这是代码部分(错误行分开):

var playerName:int;

init(); // this line goes directly beneath the variables

function f_1(init):void
{ // call once to set everything up

    saveDataObject = SharedObject.getLocal("DataBattle/character/name"); // give the save data a location

-

 playerName = txtPlayer; 

-

    function addName(e:TouchEvent):void
    {

        var myTextBox1:TextField = new TextField(); 
        var txtPlayer:TextField = new TextField(); 
        var myText:String = "Cyan"; 

        function CaptureUserInput() 
        { 
            captureText(); 
        } 

        function captureText():void 
        { 
            myTextBox1.type = TextFieldType.INPUT; 
            myTextBox1.background = true; 
            addChild(myTextBox1); 
            myTextBox1.text = myText; 
            myTextBox1.addEventListener(TextEvent.TEXT_INPUT, textInputCapture); 
        } 

        function textInputCapture(event:TextEvent):void 
        { 
            var str:String = myTextBox1.text; 
            createOutputBox(str); 
        } 

        function createOutputBox(str:String):void 
        { 
            txtPlayer.background = false; 
            txtPlayer.x = 200; 
            addChild(txtPlayer); 
            txtPlayer.text = str; 
        } 

-

        if(saveDataObject.data.characterName = playerName == null)

-

        { // checks if there is save data
            trace("No Player data yet."); // if there isn't any data on the computer...
            saveDataObject.data.characterName = playerName; // ...set the savedScore to 0
        }
        else 
        {
            trace("Player data found."); // if we did find data...
            loadData1(); // ...load the data
        }

        function loadData1():void
        {
            playerName = saveDataObject.data.characterName; // set the current score to the saved score
            trace("Data Loaded!");
        }
    } 
}

function saveData(e:TouchEvent):void
{
    saveDataObject.data.characterName = playerName; // set the saved score to the current score
    trace("Data Saved!");
    saveDataObject.flush(); // immediately save to the local drive
    trace(saveDataObject.size); // this will show the size of the save file, in bytes
}

【问题讨论】:

    标签: actionscript-3 flash textbox


    【解决方案1】:

    如果能提供有关此错误的更多详细信息,我会很高兴。就像哪一行实际上抛出了这个错误。

    但是,即使没有它,也有一些事情会立即看起来很奇怪。

    var playerName:int;
    // And few lines below
    playerName = txtPlayer;
    // Few more lines below
    var txtPlayer:TextField = new TextField();
    

    看起来您将playerName 声明为int,因此您想存储数值,但txtPlayer 是一个TextField。所以 playerName = txtPlayer; 你试图将 TextField 存储为 Int 值,这是行不通的。

    我也能看到

    if(saveDataObject.data.characterName = playerName == null)
    

    就像 :O 我什至不确定这部分是如何编译的。你想做playerName == null 检查(这将返回真/假),然后将其分配给 saveDataObject.data.characterName ?

    【讨论】:

    • 好的,您只是放大了导致错误的确切两行,我将在此之后编辑问题以使其清楚。谢谢,但现在我有一两个后续问题。 playerName 是由记分代码的修改代码制成的,因此将其设置为数字是有道理的。什么版本的代码可以让我将 playerName 存储为子输出 txtPlayer 的显示值?而且我不完全确定是否将 saveDataObject.data.characterName 分配给 playerName。
    • 好的,所以首先我将从重命名变量开始,因为它现在非常混乱。当我看到playerName 时,我希望它是……嗯……玩家姓名?因此,如果这是一个分数,我建议将其命名为playerScore,现在很清楚它是什么以及为什么它是一个 Int。 第二次删除playerName = txtPlayer; 。我不确定它在里面做什么,但无论如何你都在loadData1() 函数中设置了它,对吧?
    • 'playerName' 是玩家姓名,但我复制粘贴了分数代码并对其进行了修改,试图使其成为名称代码。我将移动'playerName = txtPlayer;' to 'loadData1()' 我需要能够将 Player name 设置为 name 函数,我想我不是很清楚......对不起,我亲自做得更好,这就是为什么我通常会阅读其他人的帖子,但是我找不到我需要的东西...
    • 更新,我做了一些研究并显着更改了代码,所以这个问题不再适用。 (但仍然存在更多问题)
    • 不用担心,很高兴您取得了一些进展。祝您好运,进行更多更改,如果您在其他地方找不到答案,您可以随时在这里提出更多问题。如果它有帮助,也可以考虑接受答案,如果你觉得它已经过时,请考虑投票结束。
    猜你喜欢
    • 2014-08-01
    • 2011-10-19
    • 2016-09-24
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    相关资源
    最近更新 更多