0
我是新来的actionscript 3.0并试图编写一些代码。我有一门课“RadioButtonExample.as
”。通过使用这个类,我试图在舞台上显示不同的对象(单选按钮和按钮)。如何在舞台上显示对象?
RadioButtonExample.as
package
{
import flash.text.TextFieldAutoSize;
import flash.display.Sprite;
import flash.events.MouseEvent;
import fl.controls.RadioButton;
import fl.controls.RadioButtonGroup;
import fl.controls.Label;
import fl.controls.Button;
public class RadioButtonExample extends Sprite
{
private var j:uint;
private var padding:uint = 10;
private var currHeight:uint = 0;
private var verticalSpacing:uint = 30;
private var sp:Sprite = new Sprite();
private var rbg:RadioButtonGroup;
private var questionLabel:Label;
private var answerLabel:Label;
private var question:String = "What day is known internationally as Speak Like A Pirate Day?"
private var answers:Array = [ "August 12", "March 4", "September 19", "June 22" ];
public function RadioButtonExample() {
setupQuiz();
}
private function setupQuiz():void {
setupQuestionLabel();
setupRadioButtons();
setupButton();
setupAnswerLabel();
}
private function setupQuestionLabel():void {
questionLabel = new Label();
questionLabel.text = question;
questionLabel.autoSize = TextFieldAutoSize.LEFT;
questionLabel.move(padding,padding + currHeight);
currHeight += verticalSpacing;
addChild(questionLabel);
}
private function setupAnswerLabel():void {
answerLabel = new Label();
answerLabel.autoSize = TextFieldAutoSize.LEFT;
answerLabel.move(padding + 120,padding + currHeight);
addChild(answerLabel);
}
private function setupRadioButtons():void {
rbg = new RadioButtonGroup("question1");
createRadioButton(answers[0], rbg);
createRadioButton(answers[1], rbg);
createRadioButton(answers[2], rbg);
createRadioButton(answers[3], rbg);
}
private function setupButton():void {
var b:Button = new Button();
b.move(padding,padding + currHeight);
b.label = "Check Answer";
b.addEventListener(MouseEvent.CLICK,checkAnswer);
addChild(b);
}
private function createRadioButton(rbLabel:String,rbg:RadioButtonGroup):void {
var rb:RadioButton = new RadioButton();
rb.group = rbg;
rb.label = rbLabel;
rb.move(padding, padding + currHeight);
addChild(rb);
currHeight += verticalSpacing;
}
private function checkAnswer(e:MouseEvent):void {
if(!rbg.selection.label) answerLabel.text = "Select an answer.";
else {
var resultStr:String = rbg.selection.label == answers[2] ? "Correct" : "Incorrect";
answerLabel.text = resultStr;
}
}
}
}
在另一方面,我有main.fla
文件具有时间轴的第一帧上的下面的代码。
main.fla
import RadioButtonExample;
var rbe:RadioButtonExample = new RadioButtonExample();
在编译FLA文件,它不提供任何编译器错误,但不显示在舞台上的任何单选按钮和简单的按钮。
请注意,我已经通过在FLA文件的属性面板下的类文本框中编写类名尝试此操作,并且它工作但我不想这样做。我想用代码来做到这一点。请让我知道应该添加到代码中以在舞台上显示对象。请帮我解决我做错了什么。