很久以前在《i时代报》看到心理测试题,就一直想开发这个好玩的程序!只是没时间开发,这次又报纸上看到测试题,突然又来兴趣了!这个程序逻辑我早就考虑过了,也不太难!正好用这个来练习自己对AS3编程感觉了!
这次编程让我掌握了AS3中的XML,Stage,URLLoader,TextField,Sprite类的用法,还有AS3自带的组件使用。Flash帮助文件一直是我的老师,我的字典。它里面的调用实例一直很不错的。建议大家要养成看帮助文件的习惯,有些问题都可以在帮助文件中找到解决办法的,这个也是此次编程的心得。
这个程序除使用Flash自带的组件外,其他全部是用代码实现的,而且采用了document class的方式,fla文件里没有一点代码。算是真正意义上的代码和设计分离吧!
演示地址: http://www.klstudio.com/demo/topic/topic.html?path=t1.xml
Main类
view plain copy to clipboard print ?
-
- package project.topic {
-
- import flash.display.*;
- import flash.net.*;
- import flash.events.*;
- import flash.errors.*;
- import flash.text.*;
- import flash.xml.*;
-
- import fl.controls.Button;
- import fl.controls.RadioButton;
- import fl.controls.RadioButtonGroup;
- import fl.controls.ScrollPolicy;
- import fl.containers.ScrollPane;
-
-
-
- public class Main extends Sprite{
-
- private var type:String;
- private var pointer:uint;
- private var value:uint;
- private var xmlData:XML;
- private var loader:URLLoader;
-
- private var preWidth:uint;
- private var preHeight:uint;
-
- private var borderColor:uint;
- private var bgColor:uint;
-
- private var bg:Shape;
- private var button:Button;
- private var title:TextField;
- private var copyright:TextField;
- private var content:Sprite;
- private var group:RadioButtonGroup;
- private var pane:ScrollPane;
-
- public function Main(){
- this.init();
- this.drawSkin();
- this.readXml(this.loaderInfo.parameters.path);
- }
-
- private function init():void{
-
- this.bgColor = 0xeeeeee;
- this.borderColor = 0x666666;
-
- this.preWidth = 500;
- this.preHeight = 400;
-
- this.type = "jump";
- this.value = 0;
- this.loader = new URLLoader();
- this.configureURLLoaderListeners(this.loader);
-
- stage.scaleMode = StageScaleMode.NO_SCALE;
- stage.align = StageAlign.TOP_LEFT;
- stage.showDefaultContextMenu = false;
- stage.addEventListener(Event.RESIZE, resizeHandler);
- }
-
- private function drawSkin():void{
-
-
- bg = new Shape();
- addChild(bg);
-
-
- title = new TextField();
- title.autoSize = TextFieldAutoSize.LEFT;
- title.selectable = false;
- title.defaultTextFormat = new TextFormat("Courier New",18,0x333333,true);
- title.x = 5;
- title.y = 5;
- title.text = "[心理测试]-";
- addChild(title);
-
-
-
- copyright = new TextField();
- copyright.autoSize = TextFieldAutoSize.LEFT;
- copyright.selectable = false;
- copyright.defaultTextFormat = new TextFormat("Verdana",9,0x666666,true,null,null,"http://www.kltudio.com","_blank");
- copyright.text = "POWERED BY KINGLONG";
- addChild(copyright);
-
-
- content = new Sprite();
-
-
- pane = new ScrollPane();
- pane.horizontalScrollPolicy = ScrollPolicy.OFF;
- pane.verticalScrollPolicy = ScrollPolicy.ON;
- pane.source = content;
- addChild(pane);
-
-
- button = new Button();
- button.width = 100;
- button.setStyle("textFormat", new TextFormat("Courier New",12));
- button.label = "开始测试";
- button.enabled = false;
- button.addEventListener(MouseEvent.CLICK, buttonClickHandler);
- addChild(button);
-
- updateSkin();
- }
-
-
- private function updateSkin():void{
-
- if(copyright != null){
- copyright.x = 10;
- copyright.y = preHeight - 24;
- }
-
- if(bg != null){
- bg.graphics.clear();
- bg.graphics.beginFill(this.bgColor);
- bg.graphics.drawRect(0,0,preWidth,34);
- bg.graphics.drawRect(0,preHeight - 34,preWidth,34);
- bg.graphics.endFill();
-
- bg.graphics.beginFill(this.borderColor);
- bg.graphics.drawRect(0,33,preWidth,1);
- bg.graphics.endFill();
- }
-
- if(button != null){
- button.move(preWidth - button.width - 5,preHeight - button.height - 5);
- }
-
- if(pane != null){
- pane.move(0,34);
- pane.setSize(preWidth,preHeight-34*2);
- updatePaneContent();
- }
- }
-
- private function updatePaneContent(){
- if(this.content.numChildren > 0){
- var dy:uint = 5;
- var sw:uint = pane.verticalScrollBar.width;
- for(var i:uint = 0;i< this.content.numChildren;i++){
- var item:Object = this.content.getChildAt(i);
- if(item is TextField){
- item.x = 0;
- item.y = dy;
- item.width = preWidth - sw;
- dy += item.height + 5;
- }else if(item is Answer){
- item.x = 0;
- item.y = dy;
- item.update(preWidth - sw);
- dy += item.getHeight() + 5;
- }
- }
- }
- pane.refreshPane();
- }
-
- public function setTitle(title:String):void{
- this.title.text = "[测试]-"+title;
- }
-
- public function clearPaneContent():void{
- while(this.content.numChildren > 0){
- this.content.removeChildAt(0);
- }
- }
-
- public function setPaneContentList(node:XMLList){
- this.clearPaneContent();
- this.button.enabled = false;
- var tt:TextField = new TextField();
- tt.autoSize = TextFieldAutoSize.LEFT;
- tt.selectable = false;
- tt.multiline = true;
- tt.wordWrap = true;
- tt.defaultTextFormat = new TextFormat("Courier New",16,0x444444,true,true,null,null,null,null,5,5,null,5);
- tt.text = node.@id+"."+node.problem;
- this.content.addChild(tt);
- var opt:XML = null;
- group = new RadioButtonGroup("opts");
- group.addEventListener(MouseEvent.CLICK, groupClickHandler);
- group.addEventListener(Event.CHANGE, groupClickHandler);
- for each(opt in node.answer.option){
- var answer:Answer = new Answer(group,opt);
- answer.update(preWidth-pane.verticalScrollBar.width);
- this.content.addChild(answer);
- }
- updatePaneContent();
- }
-
- public function setPaneContentString(ti:String,msg:String):void{
- this.clearPaneContent();
- var tt:TextField = new TextField();
- tt.autoSize = TextFieldAutoSize.LEFT;
- tt.selectable = false;
- tt.multiline = true;
- tt.wordWrap = true;
- tt.defaultTextFormat = new TextFormat("Courier New",16,0x444444,true,true,null,null,null,null,5,5,null,5);
- tt.text = ti;
- this.content.addChild(tt);
-
- var desc:TextField = new TextField();
- desc.autoSize = TextFieldAutoSize.LEFT;
- desc.selectable = false;
- desc.multiline = true;
- desc.wordWrap = true;
- desc.defaultTextFormat = new TextFormat("Courier New",12,0x666666,null,null,null,null,null,null,5,5,null,5);
- desc.htmlText = msg;
- this.content.addChild(desc);
- updatePaneContent();
- }
-
- public function readXml(url:String):void{
- this.setPaneContentString("提示信息","正在加载["+url+"]文件...");
- try{
- this.loader.load(new URLRequest(url));
- } catch (error:Error) {
- this.errorHandler(new ErrorEvent(ErrorEvent.ERROR));
- }
- }
-
- private function buttonClickHandler(event:Event):void {
- switch(event.target.label){
- case "开始测试":
- this.value = 0;
- this.pointer = 0;
- this.pointer ++;
- this.setPaneContentList(this.xmlData.list.item.(@id==this.pointer));
- this.button.label = "下一步";
- break;
- case "下一步":
- var res:String = ""+group.selectedData;
- switch(this.type){
- case "jump":
- var arr:Array = res.split("_");
- if(arr[0] == "p"){
- this.setPaneContentList(this.xmlData.list.item.(@id==arr[1]));
- }else{
- this.setPaneContentString("分析结果",this.xmlData.result.item.(@id==arr[1]));
- this.button.label = "重新测试";
- }
- break;
- case "sum":
- this.value += Number(res);
- this.pointer ++;
- if(this.pointer <= this.xmlData.list.item.length()){
- this.setPaneContentList(this.xmlData.list.item.(@id==this.pointer));
- }else{
- this.setPaneContentString("分析结果",this.getResultContent(this.value));
- this.button.label = "重新测试";
- }
- break;
- }
- break;
- case "重新测试":
- this.value = 0;
- this.pointer = 0;
- this.button.label = "开始测试";
- this.setPaneContentString("简述",xmlData.description);
- break;
- }
- }
-
- private function getResultContent(num:uint):String{
- var opt:XML = null;
- for each(opt in this.xmlData.result.item){
- var min:uint = Number(opt.@min);
- var max:uint = Number(opt.@max);
- if(((min == 0)||(num >= min)) && ((max == 0)||(num <= max))){
- return opt + "";
- }
- }
- return "";
- }
-
- private function groupClickHandler(event:Event):void {
- this.button.enabled = true;
- }
-
- private function resizeHandler(event:Event):void {
- preWidth = (stage.stageWidth > 500)?stage.stageWidth:500;
- preHeight = (stage.stageHeight > 400)?stage.stageHeight:400;
- updateSkin();
- }
-
- private function configureURLLoaderListeners(dispatcher:IEventDispatcher):void {
- dispatcher.addEventListener(Event.COMPLETE, completeHandler);
- dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
- dispatcher.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
- }
- private function completeHandler(event:Event):void{
- xmlData = new XML(this.loader.data);
- xmlData.ignoreWhitespace = true;
- xmlData.ignoreComments = true;
- this.setTitle(xmlData.title);
- this.setPaneContentString("简述",xmlData.description);
- this.type = xmlData.type;
- button.enabled = true;
- }
- private function errorHandler(event:Event):void{
- this.setPaneContentString("错误信息",event.toString());
- }
- }
- }
package project.topic { import flash.display.*; import flash.net.*; import flash.events.*; import flash.errors.*; import flash.text.*; import flash.xml.*; import fl.controls.Button; import fl.controls.RadioButton; import fl.controls.RadioButtonGroup; import fl.controls.ScrollPolicy; import fl.containers.ScrollPane; public class Main extends Sprite{ private var type:String; private var pointer:uint; private var value:uint; private var xmlData:XML; private var loader:URLLoader; private var preWidth:uint; private var preHeight:uint; private var borderColor:uint; private var bgColor:uint; private var bg:Shape; private var button:Button; private var title:TextField; private var copyright:TextField; private var content:Sprite; private var group:RadioButtonGroup; private var pane:ScrollPane; public function Main(){ this.init(); this.drawSkin(); this.readXml(this.loaderInfo.parameters.path); } private function init():void{ this.bgColor = 0xeeeeee; this.borderColor = 0x666666; this.preWidth = 500; this.preHeight = 400; this.type = "jump"; this.value = 0; this.loader = new URLLoader(); this.configureURLLoaderListeners(this.loader); stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; stage.showDefaultContextMenu = false; stage.addEventListener(Event.RESIZE, resizeHandler); } private function drawSkin():void{ //bg; bg = new Shape(); addChild(bg); //title; title = new TextField(); title.autoSize = TextFieldAutoSize.LEFT; title.selectable = false; title.defaultTextFormat = new TextFormat("Courier New",18,0x333333,true); title.x = 5; title.y = 5; title.text = "[心理测试]-"; addChild(title); //copyright; copyright = new TextField(); copyright.autoSize = TextFieldAutoSize.LEFT; copyright.selectable = false; copyright.defaultTextFormat = new TextFormat("Verdana",9,0x666666,true,null,null,"http://www.kltudio.com","_blank"); copyright.text = "POWERED BY KINGLONG"; addChild(copyright); //content content = new Sprite(); //scrollPane; pane = new ScrollPane(); pane.horizontalScrollPolicy = ScrollPolicy.OFF; pane.verticalScrollPolicy = ScrollPolicy.ON; pane.source = content; addChild(pane); //Button; button = new Button(); button.width = 100; button.setStyle("textFormat", new TextFormat("Courier New",12)); button.label = "开始测试"; button.enabled = false; button.addEventListener(MouseEvent.CLICK, buttonClickHandler); addChild(button); updateSkin(); } private function updateSkin():void{ if(copyright != null){ copyright.x = 10; copyright.y = preHeight - 24; } if(bg != null){ bg.graphics.clear(); bg.graphics.beginFill(this.bgColor); bg.graphics.drawRect(0,0,preWidth,34); bg.graphics.drawRect(0,preHeight - 34,preWidth,34); bg.graphics.endFill(); bg.graphics.beginFill(this.borderColor); bg.graphics.drawRect(0,33,preWidth,1); bg.graphics.endFill(); } if(button != null){ button.move(preWidth - button.width - 5,preHeight - button.height - 5); } if(pane != null){ pane.move(0,34); pane.setSize(preWidth,preHeight-34*2); updatePaneContent(); } } private function updatePaneContent(){ if(this.content.numChildren > 0){ var dy:uint = 5; var sw:uint = pane.verticalScrollBar.width; for(var i:uint = 0;i< this.content.numChildren;i++){ var item:Object = this.content.getChildAt(i); if(item is TextField){ item.x = 0; item.y = dy; item.width = preWidth - sw; dy += item.height + 5; }else if(item is Answer){ item.x = 0; item.y = dy; item.update(preWidth - sw); dy += item.getHeight() + 5; } } } pane.refreshPane(); } public function setTitle(title:String):void{ this.title.text = "[测试]-"+title; } public function clearPaneContent():void{ while(this.content.numChildren > 0){ this.content.removeChildAt(0); } } public function setPaneContentList(node:XMLList){ this.clearPaneContent(); this.button.enabled = false; var tt:TextField = new TextField(); tt.autoSize = TextFieldAutoSize.LEFT; tt.selectable = false; tt.multiline = true; tt.wordWrap = true; tt.defaultTextFormat = new TextFormat("Courier New",16,0x444444,true,true,null,null,null,null,5,5,null,5); tt.text = node.@id+"."+node.problem; this.content.addChild(tt); var opt:XML = null; group = new RadioButtonGroup("opts"); group.addEventListener(MouseEvent.CLICK, groupClickHandler); group.addEventListener(Event.CHANGE, groupClickHandler); for each(opt in node.answer.option){ var answer:Answer = new Answer(group,opt); answer.update(preWidth-pane.verticalScrollBar.width); this.content.addChild(answer); } updatePaneContent(); } public function setPaneContentString(ti:String,msg:String):void{ this.clearPaneContent(); var tt:TextField = new TextField(); tt.autoSize = TextFieldAutoSize.LEFT; tt.selectable = false; tt.multiline = true; tt.wordWrap = true; tt.defaultTextFormat = new TextFormat("Courier New",16,0x444444,true,true,null,null,null,null,5,5,null,5); tt.text = ti; this.content.addChild(tt); var desc:TextField = new TextField(); desc.autoSize = TextFieldAutoSize.LEFT; desc.selectable = false; desc.multiline = true; desc.wordWrap = true; desc.defaultTextFormat = new TextFormat("Courier New",12,0x666666,null,null,null,null,null,null,5,5,null,5); desc.htmlText = msg; this.content.addChild(desc); updatePaneContent(); } public function readXml(url:String):void{ this.setPaneContentString("提示信息","正在加载["+url+"]文件..."); try{ this.loader.load(new URLRequest(url)); } catch (error:Error) { this.errorHandler(new ErrorEvent(ErrorEvent.ERROR)); } } private function buttonClickHandler(event:Event):void { switch(event.target.label){ case "开始测试": this.value = 0; this.pointer = 0; this.pointer ++; this.setPaneContentList(this.xmlData.list.item.(@id==this.pointer)); this.button.label = "下一步"; break; case "下一步": var res:String = ""+group.selectedData; switch(this.type){ case "jump": var arr:Array = res.split("_"); if(arr[0] == "p"){ this.setPaneContentList(this.xmlData.list.item.(@id==arr[1])); }else{ this.setPaneContentString("分析结果",this.xmlData.result.item.(@id==arr[1])); this.button.label = "重新测试"; } break; case "sum": this.value += Number(res); this.pointer ++; if(this.pointer <= this.xmlData.list.item.length()){ this.setPaneContentList(this.xmlData.list.item.(@id==this.pointer)); }else{ this.setPaneContentString("分析结果",this.getResultContent(this.value)); this.button.label = "重新测试"; } break; } break; case "重新测试": this.value = 0; this.pointer = 0; this.button.label = "开始测试"; this.setPaneContentString("简述",xmlData.description); break; } } private function getResultContent(num:uint):String{ var opt:XML = null; for each(opt in this.xmlData.result.item){ var min:uint = Number(opt.@min); var max:uint = Number(opt.@max); if(((min == 0)||(num >= min)) && ((max == 0)||(num <= max))){ return opt + ""; } } return ""; } private function groupClickHandler(event:Event):void { this.button.enabled = true; } private function resizeHandler(event:Event):void { preWidth = (stage.stageWidth > 500)?stage.stageWidth:500; preHeight = (stage.stageHeight > 400)?stage.stageHeight:400; updateSkin(); } private function configureURLLoaderListeners(dispatcher:IEventDispatcher):void { dispatcher.addEventListener(Event.COMPLETE, completeHandler); dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler); dispatcher.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); } private function completeHandler(event:Event):void{ xmlData = new XML(this.loader.data); xmlData.ignoreWhitespace = true; xmlData.ignoreComments = true; this.setTitle(xmlData.title); this.setPaneContentString("简述",xmlData.description); this.type = xmlData.type; button.enabled = true; } private function errorHandler(event:Event):void{ this.setPaneContentString("错误信息",event.toString()); } } }
Answer类
view plain copy to clipboard print ?
- package project.topic {
-
- import flash.display.*;
- import flash.text.*;
- import flash.xml.*;
- import flash.events.*;
- import fl.controls.RadioButton;
- import fl.controls.RadioButtonGroup;
-
- public class Answer extends Sprite{
- private var node:XML;
- private var radio:RadioButton;
- private var lbl:TextField;
- private var group:RadioButtonGroup;
- public function Answer(group:RadioButtonGroup,node:XML){
- this.node = node;
- radio = new RadioButton();
- radio.group = group;
- radio.value = node.@value;
- radio.label = "";
- radio.move(0,0);
- radio.width = 20;
- radio.height = 20;
- addChild(this.radio);
-
- lbl = new TextField();
- lbl.autoSize = TextFieldAutoSize.LEFT;
- lbl.selectable = false;
- lbl.multiline = true;
- lbl.wordWrap = true;
- lbl.x = 20;
- lbl.y = 0;
- lbl.defaultTextFormat = new TextFormat("Courier New",12,0x666666,null,null,null,null,null,null,5,5,null,5);
- lbl.text = node;
- lbl.addEventListener(MouseEvent.CLICK, clickHandler);
- addChild(lbl);
- }
- public function getHeight():uint{
- return this.lbl.height;
- }
- public function update(width:uint):void{
- this.lbl.width = width - 20;
- }
- private function clickHandler(event:Event):void {
- this.radio.selected = true;
- }
- }
- }
package project.topic { import flash.display.*; import flash.text.*; import flash.xml.*; import flash.events.*; import fl.controls.RadioButton; import fl.controls.RadioButtonGroup; public class Answer extends Sprite{ private var node:XML; private var radio:RadioButton; private var lbl:TextField; private var group:RadioButtonGroup; public function Answer(group:RadioButtonGroup,node:XML){ this.node = node; radio = new RadioButton(); radio.group = group; radio.value = node.@value; radio.label = ""; radio.move(0,0); radio.width = 20; radio.height = 20; addChild(this.radio); lbl = new TextField(); lbl.autoSize = TextFieldAutoSize.LEFT; lbl.selectable = false; lbl.multiline = true; lbl.wordWrap = true; lbl.x = 20; lbl.y = 0; lbl.defaultTextFormat = new TextFormat("Courier New",12,0x666666,null,null,null,null,null,null,5,5,null,5); lbl.text = node; lbl.addEventListener(MouseEvent.CLICK, clickHandler); addChild(lbl); } public function getHeight():uint{ return this.lbl.height; } public function update(width:uint):void{ this.lbl.width = width - 20; } private function clickHandler(event:Event):void { this.radio.selected = true; } } }
代码使用时,只要把"RadioButton","Button","ScrollPane"放到fla文件库就可以了,然后只在fla文件属性里设置document class值为"project.topic.Main"就可以了
所使用到的xml文件路径:“ t1.xml” ,当然你也可以继续完善代码!