2012-07-28 18 views
1

我有一個單選按鈕的問題,因爲當我從單選按鈕的舞臺上刪除實例時,如果我再次創建實例,則單選按鈕會保留最後一個選擇。測驗應用程序+單身類as3

(我寫的類在西班牙,如果你的問題,以確定的概念,請問我)

類(crearPregunta.as或askQuestion.as)

package src.com.akkadian 

{

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 ([email protected] == 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][email protected]; 

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

       // 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. 用戶運行瑣事:第一,選擇啓動選項(在創建選項的網格)
  2. 用戶選擇一個選項(askQuestion實例被創建)
  3. 當用戶從選項
  4. 定時器執行
  5. 從主類
  6. 卸下實例(askQuestion)如果用戶選擇另一個選項,問題和涉及的單選按鈕(結果顯示在result_txt.text) swers顯示正確,但是,單選按鈕保留之前選擇的選項,並且result_txt.text顯示此選擇的結果..這是錯誤的,因爲不必進行選擇。

我有另一種情況,從這個..我有這樣的: 靜態實例(這裏創建實例C) 靜態實例B(這裏的操作的結果此實例當獲得的結果 實例C(結果顯示) 將被刪除),我該怎麼辦從實例發送信息b到實例çINF實例是在實例創建一個

回答

0

我相信問題是您每次都重複使用相同的單選按鈕組,但是您不會重置該單選按鈕組的selectedIndex或selectedValue。我相信這會解決這個問題:

  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

我有另一種情況,從這個..我有這樣的:靜態實例(這裏創建實例C)靜態實例B(這裏的操作的結果。當獲得結果時,這個實例將被刪除)實例c(結果是顯示)如何將實例b中的信息發送到實例c inf中實例是在實例a中創建的 – m4g4bu 2012-07-29 15:36:55

+0

如果這可以解決您最初的單選按鈕問題重新選擇你應該接受它併發布你的新問題,因爲它是自己的問題。 SO的目標不是幫助你完成項目,人們的目標是建立一個可以讓每個人都受益的問題和答案庫。如果事情不是零碎的,那麼它們就沒有那麼有用。 – shaunhusain 2012-07-30 16:17:48

+0

是的,我的錯誤(y) – m4g4bu 2012-07-30 17:29:05

1

要刪除的東西在閃光,或換句話說準備好垃圾C. ollector嘗試從對象中刪除所有偵聽器,將其從舞臺中刪除,然後將其設置爲null。