[AS3]心理测试题的开发
很久以前在《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类
- 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());
- }
- }
- }
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类
- 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” ,当然你也可以继续完善代码!
[AS3]心理测试题的开发相关推荐
- 计算机心理学测试题目及答案解析,大学生趣味心理测试题及答案
当你对一个人感到趣味的时候,就会发现看对方会越看越顺眼,当你对一个人产生趣味的时候,可能你就已经陷入了爱情的漩涡中,下面学习啦小编收集整理了一些相关资料,供大家学习借鉴! 大学生趣味心理测试题篇一 你 ...
- 基于python 心理测试题
# coding=utf-8 """ @author:maker @date:2019-07-05 """""" ...
- 职业选择心理测试软件,职业选择的心理测试题
打从学校全部毕业之后,接下来大半生的打拼都即将是在职场中.在这个竞争激烈的"战场"上,我们总是为着升职加薪的目标去前进.以下是学习啦小编为您准备的职业选择的心理测试题,希望对您有所 ...
- c语言程序 心理测试,简单心理测试题及答案
简单心理测试题及答案 1.心理现象分为(A) A.心理过程与个性心理 B.认知过程与个性心理 C.情感过程与个性心理 D.意志过程与个性心理. 2.心理过程包括(D) A.认识过程.情感过程.行为过程 ...
- 微信小程序学习(一)心理测试题库改造
文章目录 前言 一.分析 二.行动 1.修改题库 2.修改函数 三.收获 四.扩展 五.引用 前言 学微信小程序课程学到题库开发,正好看到个考研模型,相似度比较高,就起了魔改一番的心思 一.分析 ...
- 答题类微信小程序(心理测试题)
最近课上的作业要求我们写一个微信小程序关于心理测试的,我简略的完成了.. 附上GitHub地址:https://github.com/Silverados/We-AnswerPage 目前小程序实现的 ...
- 平安性格测试题及答案_性格趣味小测试题 有趣的心理测试题大全及答案
每个人的性格都不一定如我们所看到的一般,很多人对自己的性格认知也不深,下面有10道趣味小测试题,并附有测试答案,接下来就一起来做个性格测试题,来测测你的性格. 性格趣味小测试题.注意:每题只能选择一个 ...
- 计算机心理测试题,计算机也能当“心理医生” “知心情感计算”带你领略脑科学前沿技术...
中新网上海7月10日电 (张践)人随时随地都会有喜怒哀乐等情感的起伏变化,那么在人与人工智能交互过程中,机器是否能够体会人的喜怒哀乐呢? 7月9日,在上海举行的2021世界人工智能大会现场,中新网记者 ...
- 逻辑心理测试题:三囚分汤
一间囚房里关押着两个犯人.每天监狱都会为这间囚房提供一罐汤,让这两个犯人自己来分.起初,这两个 人经常会发生争执,因为他们总是有人认为对方的汤比自己的多.后来他们找到了一个两全其美的办法:一个人分汤, ...
最新文章
- pythoninterpolate用法_Pytorch上下采样函数--interpolate用法
- 远程连接Kali Linux使用PuTTY实现SSH远程连接
- 【PAT甲级 大数运算】1065 A+B and C (64bit) (20 分) Python 全部AC
- 程序员必备的10大健康装备!——我们要工作更要健康!
- 基于vue-cli的vuex配置
- this.$modal.confirm 自定义按钮关闭_【勤哲资料】7.6 自定义打印
- Vue父组件监听子组件调用删除模块(个性化页面设置会使用到)
- 陪跑 Android 十年,这家操作系统创业公司终于实现盈利!
- 创建一个简单的数据库
- Android布局详解(二)
- 扇贝有道180916每日一句
- 光谱共焦位移传感器原理
- 【数据库基础】数据库的视图操作
- 如何查看电脑上是否安装了MySQL
- 随笔(2015.11)
- 用python创建widows窗口
- 推荐系统(一)推荐系统整体概览
- JSP自定义标签(一)
- 基于STM32和ATH20实现OLED显示温湿度
- go语言处理html文件,golang解析html网页的方法