我是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 文件的属性面板下的类文本框中写入类名来尝试此操作,但我不想这样做。我想使用代码来实现这一点。请让我知道应该在代码中添加什么以在舞台上显示对象。请帮我解决我做错了什么。