如何在flash cs3中随机提问

时间:2011-10-18 09:19:34

标签: xml actionscript-3 flash-cs5

我使用flash cs5创建了一个多选类型的游戏。我创建的程序正在运行,但它没有显示我希望它发生的事情。


我遇到了一些错误(代码中没有真正的错误)。其中一些所谓的错误是:

  1. 你知道,我想从xml文件中获取我的问题是随机的,我知道必须使用的代码是var random=Math.ceil(Math.random()*30);,其中它应该只从问题中随机出现1个数字xml总共30。我不知道在哪里或如何插入代码。

  2. 在游戏中,玩家必须正确回答问题,如果答案正确,则应进入记分牌。否则它应该显示下一个问题并计算错误答案。对于每个错误的答案,将扣除5%的总分。但问题是,是否显示正确或错误的答案,它仍然没有进入记分牌

  3. 我还没有插入一个对游戏来说必不可少的计时器。我仍然试图找出如何这样做,因为计时器将指示得分中的扣除。这里的想法是:例如在每场比赛遭遇(或战斗)中将给予玩家一定的时间(让我们说20秒)玩家必须在限定时间内正确回答问题。如果玩家回答正确,它应该进入下一帧(显示分数)否则总分的5%将被扣除

  4. 我想我可以处理计时器,但我真的需要你在前2的帮助,我的伙伴仍然是非常新的闪光灯。我们花了很多时间调试代码,但无法找到问题的解决方案。感谢那些愿意帮助的人。

    这是程序中的一些代码(实际上是主代码)

    函数QuizItem(问题) {     this.question =问题;     this.answers = new Array();     this.numOfAnswers = 0;     this.correctAnswer = 0;     this.getQuestion =()的函数     {         归还这个。     }     this.addAnswer = function(answer,isCorrectAnswer)     {         this.answers [this.numOfAnswers] =答案;         if(isCorrectAnswer)             this.correctAnswer = this.numOfAnswers;         this.numOfAnswers ++;     }

    this.getAnswer=function(answerNumberToGet)
    {
        return this.answers[answerNumberToGet];
    }
    
    this.getCorrectAnswerNumber=function()
    {
        return this.correctAnswer;
    }
    
    this.checkAnswerNumber=function(userAnswerNumber)
    {
        if (userAnswerNumber==this.getCorrectAnswerNumber())
            gotoAndPlay("Correct");
        else
            gotoAndPlay("Wrong");
    }
    this.getNumOfAnswers=function()
    {
        return this.answers.length; 
    }
     }
    
     function onQuizData(success)
     {
    var quizNode=this.firstChild;
    var quizTitleNode=quizNode.firstChild;  
    title=quizTitleNode.firstChild.nodeValue;
    
    var i=0;
    // <items> follows <title>
    var itemsNode=quizNode.childNodes[1];
    while (itemsNode.childNodes[i])
    {
        var itemNode=itemsNode.childNodes[i];
        // <item> consists of  <question> and one or more <answer>
        // <question> always comes before <answer>s (node 0 of <item>)
        var questionNode=itemNode.childNodes[0];
        quizItems[i]=new QuizItem(questionNode.firstChild.nodeValue);
        var a=1;        
        // <answer> follows <question>
        var answerNode=itemNode.childNodes[a++];
        while (answerNode)
        {
            var isCorrectAnswer=false;
            if (answerNode.attributes.correct=="y")
                isCorrectAnswer=true;
            quizItems[i].addAnswer(answerNode.firstChild.nodeValue, isCorrectAnswer);
            // goto the next <answer>
            answerNode=itemNode.childNodes[a++];
        }
        i++;
    }
    gotoAndPlay("Start");
     }
    
     var quizItems=new Array();
     var myData=new XML();
     myData.ignoreWhite=true;
     myData.onLoad=onQuizData;
     myData.load("quiz1.xml");
     stop();
    

1 个答案:

答案 0 :(得分:0)

你在这里遇到了很多问题。我将只讨论如何加载测验XML以及从问题列表中显示随机项的一般过程:

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;

var quizItems:Array;
var quizData:XML;

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, onDataComplete);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onDataError);
xmlLoader.load(new URLRequest("INSERT XML URL HERE"));

function onDataComplete(evt:Event):void
{
    quizData = new XML(xmlLoader.data);
    var quizList:XMLList = quizData..item;

    createItems();

    showQuestion();
}

function createItems():void
{
    quizItems = new Array();

    // Grab each <item> node from the xml and stick it in an array.
    for each(var item:XML in quizList) quizItems.push(item);    
}

function showQuestion():void
{
    // TODO: Remove any previous question/answer items.

    // Double-check that the quizItems array is not at length 0, which means it should be repopulated.
    if (quizItems.length == 0) createItems();

    // Pick a random index based on the length of the quizItems array (which will shrink over time).
    var index:uint = Math.floor(Math.random() * quizItems.length);

    // Remove the item from the array and store it in a variable (so the item cannot be randomly chosen twice).
    var quizItemData:XML = quizItems.splice(index, 1)[0];

    // Grab the <question> node. 
    var questionNode:XML = quizItemData.question[0];
    // TODO: Take some action with the <question> data, such as assigning it to a text field.
    // Ex - myQuestionField.text = questionNode.toString();

    // Grab all the <answer> nodes as an XML list.
    var answerList:XMLList = quizItemData.answer;
    for each (var answerNode:XML in answerList) {
        // TODO: Take some action with each of the <answer> nodes.
        // Ex - myAnswerButton.labelText = answerNode.toString();
        // Ex - myAnswerButton.isCorrectAnswer = (answerNode.@correct == "y");
        // TODO: Position each answer item.
    }

    // TODO: Any remaining display tasks such as intro animation, etc.
}

function onDataError(evt:IOErrorEvent):void
{
    trace("ERROR: " + evt.text);
}