首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >小测验应用程序+单例类as3

小测验应用程序+单例类as3
EN

Stack Overflow用户
提问于 2012-07-28 05:45:16
回答 2查看 319关注 0票数 1

我有一个问题的无线电按钮,因为当我删除实例从舞台上的无线电按钮是,如果我再次创建的实例,无线电按钮保留最后的选择。

(我用西班牙语写了这门课,如果你有问题要找出这个概念,请问我)

类( crearPregunta.as或askQuestion.as )

代码语言:javascript
复制
package src.com.akkadian

{

代码语言:javascript
复制
import fl.controls.RadioButtonGroup;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.*;
import flash.events.MouseEvent;
import flash.net.*;
import flash.text.*;
import flash.utils.Timer;
import flash.xml.*;
import src.com.akkadian.manipularXML;

public class crearPregunta extends MovieClip
{

    private static var _instance:crearPregunta = null;

    private var xmlPath:String;
    private var radioGroup1:RadioButtonGroup = new RadioButtonGroup("Questions");
    private var xmlLoader:URLLoader = new URLLoader();
    private var instanciaXML:manipularXML;
    protected var time:Timer;
    private var rs:String;
    private var resp:XMLList;
    private var re:XMLList;
    private var pre:XMLList;
    private var opSeleccionada:Number;
    private var resultado:Boolean = false;

    public function crearPregunta()
    {


    }

    public static function getInstance():crearPregunta
    {

        if (_instance == null)
        {

            _instance = new crearPregunta();

        }

        return _instance;
    }

    private function generarContenido()
    {

        // Crea the XML instance from manipularXML class
        this.instanciaXML = manipularXML.generarInstancia();

        // Send the path of the file
        this.xmlPath = "src/com/akkadian/preguntas.xml";

        // Add the listener to proceed when the load is finished        
        xmlLoader.addEventListener(Event.COMPLETE, inXML);
        xmlLoader.load(new URLRequest(this.xmlPath));

        // adding a listener to the button sendResponse
        enviarRespuesta.addEventListener(MouseEvent.CLICK, recibirRespuesta);
        enviarRespuesta.buttonMode = true;

    }

    public function validarRespuesta(opcionSeleccionada:String):Boolean
    {
        // Option chosen
        opSeleccionada = Number(opcionSeleccionada);

        // Generate the content
        generarContenido();

        // Send the result
        return resultado;
    }

    private function inXML(e:Event):void
    {
        // Add the radiobuttons to an Array
        var radios:Array = [casoA, casoB, casoC];

        // Obtain the data from the XML
        var data = instanciaXML.obtenerInfo(e.target.data);


        for each (var nodo:XML in data.pregunta)
        {

            if (nodo.@id == opSeleccionada)
            {
                // Keep the answer for the option chosen
                resp = data.pregunta[opSeleccionada - 1].respuesta;

                // Keep the value for the option chosen
                re = data.pregunta[opSeleccionada - 1].respuesta.@valor;

                // Keep the question
                pre = data.pregunta[opSeleccionada - 1].@pre

                // add the value of the question to the dinamic textfield
                question.text = pre;

                for (var u:uint = 0; u < radios.length; u++)
                {

                    radios[u].group = radioGroup1;
                    radios[u].textField.multiline = true;
                    radios[u].textField.wordWrap = true;
                    radios[u].textField.width = 230;
                    radios[u].textField.height = 100;
                    radios[u].textField.autoSize = TextFieldAutoSize.LEFT;


                    // Add the answers to the labels
                    radios[u].label = resp[u];

                    // Add the answers to the values
                    radios[u].value = re[u];

                }

            }

        }
    }

    private function recibirRespuesta(m:MouseEvent)
    {

        for each (var tre:XML in re)
        {
            // if the value of the answer is equal to the value stored
            if (this.radioGroup1.selection.value == re)
            {
                // Save the result as true
                this.resultado = true;

                // Add the value of the answer to the textfield included on this Instance
                result_txt.text = "Correcto";
            }
            else
            {
                // Save the result as false
                this.resultado = false;

                // Add the value of the answer to the textfield included on this Instance
                result_txt.text = "Incorrecto";
            }
        }

        // Add a function to create a timer
        agregarTimer();

    }

    private function agregarTimer()
    {
        // Setup the timer
        time = new Timer(1000, 1);
        time.addEventListener(TimerEvent.TIMER_COMPLETE, cerrarTimer);
        time.start();

    }

    private function cerrarTimer(t:TimerEvent)
    {
        // Just for information, I verify which are the instances that at present ran in the Main instance
        Main.Instancia.obtenerNombreHijos();

        // Remove the instance askQuestion from the Main
        Main.Instancia.removeChildAt(2);

        // I certify that the instance is deleted
        Main.Instancia.obtenerNombreHijos();

        // I change the instance of the xml class to null
        instanciaXML = null;


    }

}

}

结果:现在发生了什么?

  1. 用户运行琐事:首先,选择start选项(创建选项网格)
  2. 用户选择一个选项(创建askQuestion实例)
  3. 当用户从无线电按钮中选择选项时(结果显示在result_txt.text上)
  4. 执行定时器。
  5. 从主类中删除实例( askQuestion )
  6. 如果用户选择另一个选项,问题和答案将正确显示,但是,无线电按钮保留之前选择的选项,result_txt.text显示此选择的结果。这是错误的,因为,没有选择必须做。

,我还有另外一个情况。我有这样一个例子:静态实例a(这里创建实例c)静态实例b(这里是操作的结果)。当得到结果时,将删除此实例)实例c(结果为显示)如何将信息从实例b发送到实例c,因为实例是在实例中创建的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-28 19:06:26

我相信问题是每次都重复使用同一个单选按钮组,但是没有重置单选按钮组的selectedIndex或selectedValue。我相信这会解决问题:

代码语言:javascript
复制
            radioGroup1.selectedIndex=-1;
            for (var u:uint = 0; u < radios.length; u++)
            {

                radios[u].group = radioGroup1;
                radios[u].textField.multiline = true;
                radios[u].textField.wordWrap = true;
                radios[u].textField.width = 230;
                radios[u].textField.height = 100;
                radios[u].textField.autoSize = TextFieldAutoSize.LEFT;


                // Add the answers to the labels
                radios[u].label = resp[u];

                // Add the answers to the values
                radios[u].value = re[u];

            }
票数 0
EN

Stack Overflow用户

发布于 2012-07-28 08:08:51

要删除Flash中的某些内容,或者换句话说,为垃圾收集器做好准备,尝试从对象中删除所有侦听器,从舞台上删除它,然后将其设置为null。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11698630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档