Actionscript Game - 随机问题匹配答案
Posted
技术标签:
【中文标题】Actionscript Game - 随机问题匹配答案【英文标题】:Actionscript Game - Matching answers with Random Questions 【发布时间】:2015-07-23 06:15:02 【问题描述】:我是 ActionScript 的初学者,所以可能对此有一个简单的解释,我错过了。对于这项任务,我将不胜感激。
我正在创建一个儿童游戏,用户在其中计算对象并选择一个数字来回答所提出的问题。我的问题随机产生。我的问题是我的答案与我随机生成的问题不匹配。
这是我的 ActionScript,其中“选择”是答案按钮。
import flash.events.MouseEvent;
stop();
//------------------variables-----------------
var score = 0;
var questionnum = 1;
scoretxt.text = "0";
var randomnum:Number;
var questions:Array = ["How many orange flags are there?",
"How many Blue flags are there? ",
"How many balls is the clown juggling?",
"How many clouds are in the sky?",
"How many horses are on the carousel?",
"How many stripes are on the roof of the tent?"];
var answers:Array = [choice10, choice6, choice10, choice4, choice2, choice5];
//------------------Display-----------------
choice1.visible = false;
choice2.visible = false;
choice3.visible = false;
choice4.visible = false;
choice5.visible = false;
choice6.visible = false;
choice7.visible = false;
choice8.visible = false;
choice9.visible = false;
choice10.visible = false;
//------------------choice buttons-----------------
this.choice1.addEventListener(MouseEvent.CLICK, this.nextq);
this.choice2.addEventListener(MouseEvent.CLICK, this.nextq);
this.choice3.addEventListener(MouseEvent.CLICK, this.nextq);
this.choice4.addEventListener(MouseEvent.CLICK, this.nextq);
this.choice5.addEventListener(MouseEvent.CLICK, this.nextq);
this.choice6.addEventListener(MouseEvent.CLICK, this.nextq);
this.choice7.addEventListener(MouseEvent.CLICK, this.nextq);
this.choice8.addEventListener(MouseEvent.CLICK, this.nextq);
this.choice9.addEventListener(MouseEvent.CLICK, this.nextq);
this.choice10.addEventListener(MouseEvent.CLICK, this.nextq);
//------------------generate question-----------------
function generateq()
this.randomnum = Math.floor((Math.random() * this.questions.length));
questiontxt.text = questions[randomnum];
this.questions.splice(this.randomnum, 1);
//------------------Start Game-----------------
startgame_mc.addEventListener(MouseEvent.CLICK, startgame);
function startgame(event:MouseEvent)
generateq();
choice1.visible = true;
choice2.visible = true;
choice3.visible = true;
choice4.visible = true;
choice5.visible = true;
choice6.visible = true;
choice7.visible = true;
choice8.visible = true;
choice9.visible = true;
choice10.visible = true;
startgame_mc.visible=false;
startscreen.visible=false;
//------------------Next Question-----------------
function nextq(event:MouseEvent)
this.questionnum++;
this.generateq();
trace(("questionnum" + this.questionnum));
if (this.questionnum == 6)
trace("questions finished....");
questiontxt.text = "All Questions Complete";
;
if(MovieClip(event.target) == answers[randomnum])
trace("right answer");
score = (score + 5);
scoretxt.text = String(("Score: " + String(score)));
;
【问题讨论】:
您可能决定在程序计算正确答案后手动将正确答案添加到问题中(或者,如果是静态图片,则从图片的元数据中获取),然后随机排列数组。跨度> 正如我所说,我是初学者。这个答案并没有太大帮助,你能解释一下吗? 【参考方案1】:是时候学习多维数组或类了。
看来问题就在这里。如果你正在拼接 questions 数组,那么你应该拼接 answers 数组以使两者匹配。
function generateq()
randomnum = Math.floor((Math.random() * this.questions.length));
text = questions[randomnum];
questions.splice(this.randomnum, 1);
// add this
this.questions.splice(this.randomnum, 1);
【讨论】:
【参考方案2】:使用一个对象来创建你的问题:
var questions:Array = [
title:"How many orange flags are there?",
correct:5,
incorrectA:8,
incorrectB:10,
questionImageUrl:"https://***.com/imageexample.jpg",
title:"How many Blue flags are there? ",
correct:5,
incorrectA:8,
incorrectB:10,
questionImageUrl:"https://***.com/imageexample2.jpg"
]
【讨论】:
以上是关于Actionscript Game - 随机问题匹配答案的主要内容,如果未能解决你的问题,请参考以下文章