玩家读到一个问题,然后从多个选项中选择答案,答对了会得到相应的分数或者一些奖励。接着进入下一个问题~~

package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class TriviaGame extends MovieClip {
//问题数据
private var dataXML:XML;
// 文本格式
private var questionFormat:TextFormat;
private var answerFormat:TextFormat;
private var scoreFormat:TextFormat;
// 文本字段
private var messageField:TextField;
private var questionField:TextField;
private var scoreField:TextField;
// sprites 和对象
private var gameSprite:Sprite;
private var questionSprite:Sprite;
private var answerSprites:Sprite;
private var gameButton:GameButton;
// 游戏状态变量
private var questionNum:int;//问题数量
private var correctAnswer:String;//正确答案
private var numQuestionsAsked:int;//提问的数量
private var numCorrect:int;//追问的问题数量
private var answers:Array;
public function startTriviaGame() {
// 创建游戏 sprite
gameSprite = new Sprite();
addChild(gameSprite);
// 设置文本格式
questionFormat = new TextFormat("Arial",24,0x330000,true,false,false,null,null,"center");
answerFormat = new TextFormat("Arial",18,0x330000,true,false,false,null,null,"left");
scoreFormat = new TextFormat("Arial",18,0x330000,true,false,false,null,null,"center");
// 创建一个得分文本和开始信息文本字段
scoreField = createText("",questionFormat,gameSprite,0,360,550);
messageField = createText("Loading Questions...",questionFormat,gameSprite,0,50,550);
// 设置游戏状态,导入问题
questionNum = 0;
numQuestionsAsked = 0;
numCorrect = 0;
showGameScore();
xmlImport();
}
// 开始加载问题
public function xmlImport() {
var xmlURL:URLRequest = new URLRequest("trivia1.xml");
var xmlLoader:URLLoader = new URLLoader(xmlURL);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
// 导入问题
public function xmlLoaded(event:Event) {
dataXML  = XML(event.target.data);
gameSprite.removeChild(messageField);
messageField = createText("Get ready for the first question!",questionFormat,gameSprite,0,60,550);
showGameButton("GO!");
}
// 创建文本字段
public function createText(text:String, tf:TextFormat, s:Sprite, x,y: Number, width:Number): TextField {
var tField:TextField = new TextField();
tField.x = x;
tField.y = y;
tField.width = width;
tField.defaultTextFormat = tf;
tField.selectable = false;
tField.multiline = true;//多行
tField.wordWrap = true;//自动换行
if (tf.align == "left") {
tField.autoSize = TextFieldAutoSize.LEFT;
} else {
tField.autoSize = TextFieldAutoSize.CENTER;
}
tField.text = text;
s.addChild(tField);
return tField;
}
// 更新得分
public function showGameScore() {
scoreField.text = "Number of Questions: "+numQuestionsAsked+"   Number Correct: "+numCorrect;
}
// 询问玩家是否准备好开始下一个问题
public function showGameButton(buttonLabel:String) {
gameButton = new GameButton();
gameButton.label.text = buttonLabel;
gameButton.x = 220;
gameButton.y = 300;
gameSprite.addChild(gameButton);
gameButton.addEventListener(MouseEvent.CLICK,pressedGameButton);
}
//玩家准备就绪
public function pressedGameButton(event:MouseEvent) {
// 移除问题
if (questionSprite != null) {
gameSprite.removeChild(questionSprite);
}
// 移除按钮和信息
gameSprite.removeChild(gameButton);
gameSprite.removeChild(messageField);
//询问下一个问题
if (questionNum >= dataXML.child("*").length()) {
gotoAndStop("gameover");
} else {
askQuestion();
}
}
// 设置问题
public function askQuestion() {
//准备新问题的sprite
questionSprite = new Sprite();
gameSprite.addChild(questionSprite);
// 创建问题的文本字段
var question:String = dataXML.item[questionNum].question;
questionField = createText(question,questionFormat,questionSprite,0,60,550);
// 创建答案的sprite,得到正确的答案,然后随机排列所有的答案
correctAnswer = dataXML.item[questionNum].answers.answer[0];
answers = shuffleAnswers(dataXML.item[questionNum].answers);
// 将每个答案放进新的 sprite中并添加circle图标
answerSprites = new Sprite();
for(var i:int=0;i<answers.length;i++) {
var answer:String = answers[i];
var answerSprite:Sprite = new Sprite();
var letter:String = String.fromCharCode(65+i); // A-D String.fromCharCode,它会将65转成A,66转成B,后面依次类推
var answerField:TextField = createText(answer,answerFormat,answerSprite,0,0,450);
var circle:Circle = new Circle(); //从库中得到
circle.letter.text = letter;
answerSprite.x = 100;
answerSprite.y = 150+i*50;
answerSprite.addChild(circle);
answerSprite.addEventListener(MouseEvent.CLICK,clickAnswer); //
answerSprite.buttonMode = true;
answerSprites.addChild(answerSprite);
}
questionSprite.addChild(answerSprites);
}
// 获取所有的答案,并将随机放置在数组中
public function shuffleAnswers(answers:XMLList) {
var shuffledAnswers:Array = new Array();
while (answers.child("*").length() > 0) {
var r:int = Math.floor(Math.random()*answers.child("*").length());
shuffledAnswers.push(answers.answer[r]);
delete answers.answer[r];
}
return shuffledAnswers;
}
// 玩家选取一个答案
public function clickAnswer(event:MouseEvent) {
// 得到选择的文本并比较
var selectedAnswer = event.currentTarget.getChildAt(0).text;
if (selectedAnswer == correctAnswer) {
numCorrect++;
messageField = createText("You got it!",questionFormat,gameSprite,0,140,550);
} else {
messageField = createText("Incorrect! The correct answer was:",questionFormat,gameSprite,0,140,550);
}
finishQuestion();
}
public function finishQuestion() {
//移除所有的不正确答案
for(var i:int=0;i<4;i++) {
answerSprites.getChildAt(i).removeEventListener(MouseEvent.CLICK,clickAnswer);
if (answers[i] != correctAnswer) {
answerSprites.getChildAt(i).visible = false;
} else {
answerSprites.getChildAt(i).y = 200;
}
}
//下一问题
questionNum++;
numQuestionsAsked++;
showGameScore();
showGameButton("Continue");
}
// 清除 sprites
public function cleanUp() {
removeChild(gameSprite);
gameSprite = null;
questionSprite = null;
answerSprites = null;
dataXML = null;
}
}
}

XML文件

<trivia>
<item category="Entertainment">
<question>Who is known as the original drummer of the Beatles?</question>
<answers>
<answer>Pete Best</answer>
<answer>Ringo Starr</answer>
<answer>Stu Sutcliffe</answer>
<answer>George Harrison</answer>
</answers>
<hint>Was fired before the Beatles hit it big.</hint>
<fact>Pete stayed until shortly after their first audition for EMI in 1962, but was fired on August 16th of that year, to be replaced by Ringo Starr.</fact>
</item>
<item category="History">
<question>What ship rests in the bottom of Pearl Harbor?</question>
<answers>
<answer>USS Arizona</answer>
<answer>USS Iowa</answer>
<answer>USS Wisconsin</answer>
<answer>USS Kentucky</answer>
</answers>
<hint>Was also the 48th state admitted into the U.S. and the last of the contiguous states admitted.</hint>
<fact>The USS Arizona Memorial, dedicated in 1962, spans the sunken hull of the battleship without touching it. The 19,585 pound anchor of the Arizona is displayed at the entrance of the visitor center.</fact>
</item>
<item category="Entertainment">
<question>In what seaside town did Alfred Hitchcock’s 1963 movie “The Birds” take place?</question>
<answers>
<answer>Bodega Bay</answer>
<answer>Half moon Bay</answer>
<answer>Carmel</answer>
<answer>Monterey Bay</answer>
</answers>
<hint>The spanish word for a small grocery store.</hint>
<fact>Located in Sonoma County, California just north of San Francisco it was discovered in 1775 by the Spanish explorer Juan Francisco de la Bodega y Quadra.</fact>
</item>
<item category="History">
<question>Who was the principal author of the Declaration of Independence?</question>
<answers>
<answer>Thomas Jefferson</answer>
<answer>Abraham Lincoln</answer>
<answer>Benjamin Franklin</answer>
<answer>John Adams</answer>
</answers>
<hint>Was also our third president    </hint>
<fact>The committee decided that Jefferson would write the draft, which he showed to Franklin and Adams. Franklin himself made at least 48 corrections. Jefferson then produced another copy incorporating these changes, and the committee presented this copy to the Continental Congress on June 28, 1776.</fact>
</item>
<item category="Sports">
<question>Who was one of the first 5 members elected to the Baseball Hall of Fame?</question>
<answers>
<answer>Babe Ruth</answer>
<answer>Hank Aaron</answer>
<answer>Jackie Robinson</answer>
<answer>Pete Rose</answer>
</answers>
<hint>One of his nicknames was “The Bambino”</hint>
<fact>In 1936 the first five members to elected were Ty Cobb, Babe Ruth, Honus Wagner, Christy Mathewson and Walter Johnson.</fact>
</item>
<item category="Geography">
<question>What is the capitol of Costa Rica?</question>
<answers>
<answer>San Jose</answer>
<answer>Liberia</answer>
<answer>Puntarenas</answer>
<answer>Cartago</answer>
</answers>
<hint>Also the name of a California city</hint>
<fact>San Jose was a very small village until 1824. In that year, Costa Rica's first elected head of state, Juan Mora Fernández, decided to move the government of Costa Rica from the old Spanish colonial capital of Cartago and make a fresh start with a new city.</fact>
</item>
<item category="Grab Bag">
<question>What system is used in libraries to catalog their books?</question>
<answers>
<answer>Dewey Decimal System</answer>
<answer>Library of Congress Classification</answer>
<answer>Universal Decimal Classification</answer>
<answer>International Standard Book Number</answer>
</answers>
<hint>One of Donald Duck’s Nephews</hint>
<fact>Developed by Melvil Dewey in 1876, and since greatly modified and expanded in the course of the twenty-two major revisions, the most recent in 2004.</fact>
</item>
<item category="Science">
<question>Who won the 1921 Nobel Prize for Physics?</question>
<answers>
<answer>Albert Einstein</answer>
<answer>Robert A. Millikan</answer>
<answer>Niels Bohr</answer>
<answer>Charles Edouard Guillaume</answer>
</answers>
<hint>E=mc2</hint>
<fact>The Federal Bureau of Investigation had a file containing 1,427 pages about Albert Einstein.</fact>
</item>
<item category="History">
<question>From 1948-1964 Benjamin Franklin's face was on this U.S. coin. How much was it worth?</question>
<answers>
<answer>Half dollar or 50 cents</answer>
<answer>Dollar</answer>
<answer>Quarter</answer>
<answer>Dime</answer>
</answers>
<hint>There's a rapper by the same name.</hint>
<fact>Benjamin Franklin is credited with inventing the Franklin stove, lightning rod, and bifocals. The city of Philadelphia owns 5000 likenesses of him.</fact>
</item>
<item category="Geography">
<question>Vancouver is located in what Canadian Province north of Washington state?</question>
<answers>
<answer>British Columbia</answer>
<answer>Saskatchewan</answer>
<answer>Alberta</answer>
<answer>Manitoba</answer>
</answers>
<hint>Not to be mistaken with England</hint>
<fact>The 2010 Winter Olympics will be held in Vancouver and nearby Whistler.</fact>
</item>
</trivia>

flash 3.0问答游戏相关推荐

  1. [转]Flash ActionScript2.0面向对象游戏开发-推箱子

    本文转自:http://www.alixixi.com/Dev/W3C/Flash/2007/2007070868666.html 概述: Flash ActionScript2.0是一种面向对向的编 ...

  2. flash 3.0拼图游戏

    package { import flash.display.*; import flash.events.*; import flash.net.URLRequest; import flash.g ...

  3. Flash As3.0 游戏开发小结

    转自: http://blog.csdn.net/chongtianfeiyu/article/details/8096446 ActionScript3.0(以下简称AS3.0)开发flash游戏目 ...

  4. 夏敏捷第28本著作《Flash ActionScript3.0动画基础与游戏设计》(Flash CC版)

            二十 年来,多媒体技术飞速发展,已成为计算机和网络的必备功能.Flash是2D动画制作不可或缺的工具,它以色彩.形态.音效和音乐展现媒体技术的魅力.经过多年的发展,Adobe Flas ...

  5. Python编程基础:第三十八节 问答游戏Quiz Game

    第三十八节 问答游戏Quiz Game 前言 实践 前言 我们这一节还是对之前学习内容的一个综合运用,主要涉及到函数编程.字典以及列表的使用.条件语句.循环结构等等.通过本节的学习读者可以检验之前内容 ...

  6. python智力问答游戏代码,python实现智力问答测试小程序

    智力问答测试功能介绍 .程序设计的思路: 程序使用了一个SQLlite试题库test2.db,其中每个智力问答由题目,4个选项和正确答案组成(question,Answer_A,Answer_B,An ...

  7. python智力问答游戏_Python语言编写智力问答小游戏功能

    本篇博文将使用Python代码语言简单编写一个轻松益智的小游戏,效果如下所示: 1.设计思路 本项目使用SQLite建立问答题库,每道题包括4个选项答案(3个正确答案,1个错误答案).每道题都有一定分 ...

  8. Flash as2.0与3.0功能性能详细对比

    一.flash as2.0 与as3.0的定义 ActionScript 2.0:实际上是as1.0的升级版,首次将OOP(Object Oriented Programming,面向对象的程序设计) ...

  9. Flash AS3.0实战

    如今网页游戏在游戏产业中占有半壁江山.在网页游戏中,百分之九十使用的是flash as3来做前端交互的开发.flash以其体积小等特性,吸引了无数的玩家.比如<傲剑>,<神仙道> ...

最新文章

  1. ubunu16.04 TensorFlow object detection API 应用配置
  2. Arcgis API for JavaScript在地图上实现手机定位信息的追踪显示
  3. 《博士五年总结》及我其它过去的博客文章
  4. xshell连不上虚拟机linux的解决办法(用的默认NAT模式)
  5. java 数据库 事务 只读_不使用事务和使用只读事务的区别
  6. 拿起电话就开始给(飞鸽传书3.0)
  7. python的简单GUI(多线程时钟)
  8. 数据结构之搜索算法二:二叉搜索树
  9. python 在Excel中新增一列
  10. 使用struct与typedef定义结构体
  11. matlab 求解发动机换算转速,简单一个公式,教你用发动机转速计算车速!
  12. C语言学习 单精度、双精度各有几位小数?
  13. 智慧语录(人生哲学)
  14. Linux开发 | 电脑WiFi上网,开发板和电脑网线直连,文件拷贝
  15. Numpy 怎么把arange ()产生的列表 变成一个行向量或者列向量
  16. CISP学习笔记2:风险管理1
  17. 网易招财猫(内测版)
  18. 校园点餐系统:点餐、食堂管理、商户管理和菜品管理(Java和MySQL)
  19. Everything.exe命令行激发窗口查询
  20. 基石为勤能补拙的迷宫之旅——第一天(计算机硬件和操作系统)

热门文章

  1. 底部导航图片与文字上下显示
  2. 常见智力题汇总(建议收藏)
  3. 2019011工作日志-关于代币空投合约的编写和js基于koa框架的整合
  4. Imported target “dart“ includes non-existent path 笔记
  5. c语言宏定义多个常量,C语言几个常见的宏定义
  6. 周鸿祎谈柔道战略(转)
  7. 计算机专业在哪里盖章好,毕业生就业证明去哪盖章
  8. 为什么亚马逊被认为是最有战略头脑的科技公司?
  9. Nginx 配置Godaddy下载的没有.key 文件的SSL证书
  10. 关于dojo的build系统