将 text.flash:TextField 类型的值隐式强制转换为不相关的 Int

Implicit Coercion of a value of a type of text.flash:TextField to an unrelated Int

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

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

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
}

最好能提供有关此错误的更多详细信息。比如哪一行实际上抛出了这个错误。

然而,即使没有它,也有一些东西看起来很奇怪。

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 检查(这会 return true/false)然后将它分配给 saveDataObject.data.characterName ?