0

我正在创建一个供工程专业学生使用的健康和安全测验。

我收到了一个 .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 || _

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

谢谢你。

::EDIT:: 我遇到的问题是我什至不明白如何开始通过 Actionscript 3.0 将此文件调用到我的场景中。

4

1 回答 1

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);
于 2013-03-29T01:32:12.973 回答