我正在做一个简单的食物游戏,其中有9张成分图片,每道菜中只有两种成分。我可以指定或硬编码哪些图片是正确的。我也不需要洗牌。
用户成功点击2个正确的成分后,将显示一个输出,告诉他们他们已经正确。如果是一个或没有,游戏将重置。
到目前为止,我已经完成了游戏布局但是缺少游戏动作部分的shell,我已经对记忆游戏进行了一些研究,但我觉得编码有很多不同。
下图:
1)用户将通过单击左侧的任意2个方格开始(例如:sp1和sp3)
2)如果正确,用户将被带到另一个框架以开始第二道菜
3)如果组合错误,蓝框将显示"错误"和游戏重置
已编辑:已添加指向fla的链接 - 感谢用户蝙蝠侠 - https://www.dropbox.com/s/9oz9uikfbyp3rhi/thespicegame.fla?dl=0
编辑:我被建议使用开关,将尝试使用
Edited:
function checkPicture(e:MouseEvent):void
{
// Start your custom code
// This example code displays the words "Mouse clicked" in the Output panel.
if(e.currentTarget == sp1){
if(e.currentTarget == sp2){
trace("working");
}else{
trace("notworking");
}
}
}
答案 0 :(得分:1)
有很多不同的方法可以完成这项任务。总结一下你需要做什么(无论实现如何):
您需要将每道菜的两种正确香料联系起来
您需要在内存中存储两个被点击的项目
单击第二个项目后,您需要将内存中的两个项目与用于将正确的香料与当前菜肴相关联的任何机制进行比较。
由于看起来你是一名学生而且只是学习,我会根据你当前的基于框架和图书馆的游戏定制我的解决方案 - 请记住,最好的解决方案将涉及使用类文件进行更多编程而没有时间轴代码
将正确的香料与菜肴联系起来。最简单的方法是使用菜单按钮作为键创建字典。将此(以及所有后续代码除非另有说明)放在第一个游戏框架(当前第4帧)上。
//create a dictionary to store all the correct spices
var correctSpices:Dictionary = new Dictionary();
correctSpices[meeBtn2] = [sp1, sp3]; //assign an array containing the correct spices
correctSpices[chickBtn2] = [sp1, sp3];
correctSpices[satayBtn2] = [sp1, sp3];
correctSpices[laskaBtn2] = [sp1, sp3];
correctSpices[rojakBtn2] = [sp1, sp3];
创建一个变量来保存单击的第一个成分。还要创建一个存储当前菜的变量:
var firstSpice; //to hold the first spice clicked
var currentDish; //to hold the current dish
将当前菜放在该菜的任何框架上:
currentDish = chickBtn2; //put this on the frame where chicken is the dish
单击spice时,需要填充第一个单击spice变量。我没有在你的香料按钮上看到任何点击听众,所以让我们假设你有以下内容:
sp1.addEventListener(MouseEvent.CLICK, spiceClick, false,0,true);
sp2.addEventListener(MouseEvent.CLICK, spiceClick, false, 0, true);
//...etc with all spices
或者通过简单的草率方式来完成3行中的所有9行:
for(var i:int=1; i<= 9;i++){
this["sp" + i].addEventListener(MouseEvent.CLICK, spiceClick, false, 0, true);
}
然后在点击处理程序中:
function spiceClick(e:Event):void {
//If the firstSpice variable is null (empty), assign it the click events current target (which is the item you attached the listener to)
if(firstSpice == null){
firstSpice = e.currentTarget;
firstSpice.mouseEnabled = false; //make it so you can't click this one anymore
return; //exit this function since we want to wait for the second item to be clicked
}
//the second item was clicked if we made it this far, check the results
if(correctSpices[currentDish].indexOf(firstSpice) > -1){
//first one was correct, now check the one just clicked
if(correctSpices[currentDish].indexOf(e.currentTarget) > -1){
//second one was correct
nextLevel();
}else{
//second one was wrong
}
}else{
//first one was wrong
}
}
function nextLevel(){
//reset all the mouse enabled properties so you can click all the buttons again
for(var i:int=1; i<= 9;i++){
this["sp" + i].mouseEnabled = true;
}
//clear the firstSpice var
firstSpice = null;
nextFrame(); //goto the next frame
}
请注意,此代码将保留所有未来的帧。只要你的按钮上没有关键帧或空帧,鼠标监听器和所有后续帧都将保持相同
为此,我建议只为当前的菜肴文本添加另一个图层,然后在需要时隐藏或禁用菜单按钮(这样您只需连接鼠标监听器一次)。
所以在每个游戏框架中,你都有这个代码:
if(currentDish) currentDish.visible = true; //if there was previously a hidden dish button, make it visible again
currentDish = chickBtn2; //assign whatever the current dish btn is for each frame
currentDish.visible = false; //hide the current dish button so it can't be clicked
修改强>
以下是我用于测试的代码:(全部在第4帧)
import flash.utils.setTimeout;
import flash.utils.Dictionary;
meeBtn2.addEventListener(MouseEvent.CLICK, dishBtnClick);
chickBtn2.addEventListener(MouseEvent.CLICK, dishBtnClick);
satayBtn2.addEventListener(MouseEvent.CLICK, dishBtnClick);
laskaBtn2.addEventListener(MouseEvent.CLICK, dishBtnClick);
rojakBtn2.addEventListener(MouseEvent.CLICK, dishBtnClick);
function dishBtnClick(event:MouseEvent):void
{
switch(event.currentTarget){
case meeBtn2:
gotoAndStop(4);
break;
case chickBtn2:
gotoAndStop(5);
break;
case satayBtn2:
gotoAndStop(6);
break;
case laskaBtn2:
gotoAndStop(7);
break;
case rojakBtn2:
gotoAndStop(8);
break;
}
}
//create a dictionary to store all the correct spices
var correctSpices:Dictionary = new Dictionary();
correctSpices[meeBtn2] = [sp1, sp3];
correctSpices[chickBtn2] = [sp1, sp3];
correctSpices[satayBtn2] = [sp1, sp3];
correctSpices[laskaBtn2] = [sp1, sp3];
correctSpices[rojakBtn2] = [sp1, sp3];
var firstSpice; //to hold the first spice clicked
var currentDish; //to hold the current dish
currentDish = meeBtn2; //put this on the frame where chicken is the dish
for(var i:int=1; i<= 9;i++){
this["sp" + i].addEventListener(MouseEvent.CLICK, spiceClick, false, 0, true);
}
function spiceClick(e:Event):void {
//If the firstSpice variable is null (empty), assign it the click events current target (which is the item you attached the listener to)
if(firstSpice == null){
firstSpice = e.currentTarget;
firstSpice.mouseEnabled = false; //make it so you can't click this one anymore
return; //exit this function since we want to wait for the second item to be clicked
}
//the second item was clicked if we made it this far, check the results
var ctr:int = 0; //a counter to count how many are correct
if(correctSpices[currentDish].indexOf(firstSpice) > -1){
//the first one was correct, add 1 to the counter
ctr++;
}
if(correctSpices[currentDish].indexOf(e.currentTarget) > -1){
//second one was correct
ctr++;
}
switch(ctr){
case 2:
//both items were correct
output1.text = "Great Job!";
//go to the next level in 2 seconds
flash.utils.setTimeout(nextLevel, 2000);
break; //don't look at any more case statements
case 1:
output1.text = "Almost There";
reset();
break;
case 0:
output1.text = "Try harder...";
reset();
}
}
function reset(){
//reset all the mouse enabled properties so you can click all the buttons again
for(var i:int=1; i<= 9;i++){
this["sp" + i].mouseEnabled = true;
}
//clear the firstSpice var
firstSpice = null;
}
function nextLevel(){
reset();
nextFrame(); //goto the next frame
}