ActionScript 2.0:.swf 文件中的输入数据无法将变量正确保存在 .txt 文件中。错误:未定义

问题描述 投票:0回答:1
  • 我有一个使用 ActionScript 2 在 Flash CS6 中构建的实时聊天系统。除了一个实例名称/变量在 .sol 共享对象文件中一直显示为“未定义”之外,一切都运行良好。这是代码:

/////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// ////////

// Starts the action when user left clicks with mouse button:

on (release) {
if (UserName eq "") {
    _root.LiveChat.Status = "Please enter a user name.";
} else if (Message eq "") {
    _root.LiveChat.Status = "Please enter a valid message.";
} else {

// Creates the shared object in Flash CS6 Shared Object directory folder:       
var so:SharedObject = SharedObject.getLocal("Chat.txt.sol");

// Sends the newly fresh input data from 3 textboxes called UserName, Message, and ChangeColorText to the .sol file in HTML format:         
so.data.User = "\r\n" + "<font color=" + ChangeColorText + ">" + UserName + ":</font> " + Message + " " + "<font color=" + "'#B4B65A'" + "><font size=" + "'13'" + ">" + "Posted: " + new Date + "</font></font>";
so.flush();
    }}

// All code above this line below works perfectly except ChangeColorText shows up in the .sol file as undefined.

/////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// ////////

// I also tried this code to see if I can send variable data to a new Chat.txt file in the same directory folder as the .fla and .swf file but no avail:

on (release) {
if (UserName eq "") {
    _root.LiveChat.Status = "Please enter a user name.";
} else if (Message eq "") {
    _root.LiveChat.Status = "Please enter a valid message.";
} else {
    var UserName:String;
    var Message:String;
    var ChangeColorText:String;     
    var fileRef:String;
    textToSave = UserName +"\n" + Message +"\n" + ChangeColorText;
    fileRef = new String();
    fileRef.String(textToSave,"Chat.txt");
    so.flush();
}}

请让我知道我在处理代码时犯下的任何需要纠正的错误。

更新:我找到了我自己问题的答案。结果我忘记在 .fla 和导出的 .swf 文件中正确设置路径,因此实例名称/变量“ChangeColorText”的代码实际上是“_root.ChangeColorText”

注意:我意识到在 ActionScript 2 中,当您在按钮中创建实例名称/变量时,您必须确保创建一条直接到达源的路径。在本例中是名为“ChangeColorText”的文本框。

我的问题解决了。

variables flash actionscript undefined computer-science
1个回答
0
投票

嗯,我的 AS2 非常生锈了,我从 2008 年起就没有使用过它,但是这个概念应该是对的,所以我相信你会明白的。

// First of all, let's initialize a SO that is visible everywhere.
// Also, you don't specify file name here, just any String identifier.
_global.ChatSO = SharedObject.getLocal("My Chat");

// Now you need to set up data if it is the first time.
if (_global.ChatSO.data.chatLog == undefined)
{
    _global.ChatSO.data.chatLog = "";
    _global.ChatSO.data.userName = "";
    _global.ChatSO.data.messageCount = 0;
}

然后,带有 TextField 的框架。假设它们分别具有助记且不言自明的实例名称 UserNameUserInputChatLog

// Initialize.
UserName.text = _global.ChatSO.data.userName;
UserInput.text = "";

// If the cookie is not empty, the ChatLog TextField will display something now.
ChatLog.htmlText= _global.ChatSO.data.chatLog;

function sendMessage():void
{
    if (UserInput.text == "")
    {
        // The input TextField is empty. Don't do anything.
        return;
    }
    
    var htmlMessage:String = "";
    
    // Let's form the entry.
    if (_global.ChatSO.data.messageCount > 0)
    {
        htmlMessage += "<br/>";
    }

    htmlMessage += "<font color=\"#DEFACE\">" + UserName.text + ":</font> ";
    htmlMessage += UserInput.text + " ";
    htmlMessage += "<font color=\"#B4B65A\" size=\"13\">";
    htmlMessage += "Posted: " + new Date() + "</font>";
    
    // Add the new entry to the chat...
    ChatLog.htmlText += htmlMessage;
    
    // ...and record it as well.
    _global.ChatSO.data.messageCount += 1;
    _global.ChatSO.data.userName = UserName.text;
    _global.ChatSO.data.chatLog += htmlMessage;
}

现在您只需在按钮 on (release) 处理程序中调用 sendMessage() 函数,仅此而已。

© www.soinside.com 2019 - 2024. All rights reserved.