调用.text文件以在flash测验中填充文本框

时间:2013-03-28 23:05:11

标签: xml flash

我正在创建一个健康和安全测验,供工科学生使用。

我收到了一个.txt文件,里面有16类问题。

我自己搜索过如何做到这一点的教程,但我无法理解指南的格式。

有人能够在这个问题上启发我吗?我正在使用Actionscript 3.0

以下是该文件的前几行供参考:

Ref || Question || AnswerA || AnswerB || AnswerC || AnswerD || AnswerE || Correct || Answer || Type || File
1.1 || Who has responsibility for health and safety on site ? || The client and main contractor only || Self - employed contractors only and employees || Employers, employees and sub - contractors || Everyone on site no matter who employs them || || D || Everyone at work has a legal duty to look after their own health and safety. || o_4 || _
1.2 || Which of the following is correct for risk assessment ? || It is a good idea to do, but not essential || Only do it if it is a big job || It is a legal requirement and must always be done || Only needs to be done for hazardous work || || C || Risk assessments are always necessary because they show how people are likely to be harmed. || o_4 || _
1.3 || Why should regular inspections of the workplace take place ? || To check whether the working environment is safe || To check that everyone is doing their job || To prepare for a visit from an HSE Inspector || To check that all staff are present || || A || If regular inspections are not carried out, the workplace could become an unsafe place. || o_4 || _
1.4 || The letters CDM stand for : || Control of Demolition(and Management) Regulations || Construction(Demolition Management) Regulations || Construction(Design and Management) Regulations || Control of Dangerous Materials Regulations || || C || The CDM Regulations aim to ensure that health and safety is addressed in a structured and organised manner during the design, construction, maintenance and demolition phases of all projects to which the regulations apply. || o_4 || _

很抱歉,如果这令人困惑,如果有人回复并且理解有困难,我很乐意向您展示您可能需要进一步了解的任何内容。

谢谢。

::编辑::我遇到的问题是我不明白如何通过Actionscript 3.0开始将这个文件调用到我的场景中。

1 个答案:

答案 0 :(得分:1)

首先,您可以使用加载程序读取数据。

创建一个存储问题的数组:

var questions:Array = [ ];

实例化一个加载器并在加载完成时添加一个事件监听器:

var request:URLRequest = new URLRequest("questions.txt");
var loader:URLLoader = new URLLoader(request);

loader.addEventListener(Event.COMPLETE, completeHandler);

完成后,解析||分隔符的数据文件并将问题存储在数组中:

function completeHandler(event:Event):void
{
    // loader data - the questions.txt file
    var data:String = event.target.data;

    // split data by newline for each question
    var lines:Array = data.split("\n");

    // for every line
    for each (var line:String in lines)
    {
        // split line by "||" delimiter
        var question:Array = line.split("||");

        // add the question to the questions array:
        questions.push({ref: question[0],
                        question: question[1],
                        answerA: question[2],
                        answerB: question[3],
                        answerC: question[4],
                        answerD: question[5],
                        answerE: question[6],
                        correct: question[7],
                        answer: question[8],
                        type: question[9],
                        file: question[10]});
    }
}

现在,每个问题都是你的问题数组的一个元素。

例如,迭代问题:

for each (var question:Object in questions)
{
    trace("question: " + question.question);
    trace("answer:   " + question.answer);
    trace("type:     " + question.type);
    trace("file:     " + question.file);
}

或者,随机选择一个问题:

var question:Object = questions[Math.floor(Math.random() * questions.length)];

trace("question: " + question.question);
trace("answer:   " + question.answer);
trace("type:     " + question.type);
trace("file:     " + question.file);