flash 3.0问答游戏

来源:互联网 发布:苏州企业名录数据库 编辑:程序博客网 时间:2024/06/11 11:02

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

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() {// 创建游戏 spritegameSprite = 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() {//准备新问题的spritequestionSprite = 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");}// 清除 spritespublic 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>