关于对象比较的简单 Adobe Flash 游戏
Simple Adobe Flash game about objects comparison
- 游戏布局
我正在开发一个简单的美食游戏,其中有 9 张配料图片,每道菜中只有其中两种配料。我可以指定或硬编码哪些图片是正确的。我也不需要洗牌。
用户成功点击 2 种正确的成分后,下方会显示一个输出,告诉他们他们已经正确了。如果是一个或 none,游戏将重置。
- 问题
我已经完成了 shell 到目前为止,游戏的布局已经完成,但缺少游戏的动作脚本部分,我对记忆游戏做了一些研究,但我觉得有很大的不同在编码中。
下图:
1) 用户将通过单击左侧的任意 2 个方块(例如:sp1 和 sp3)开始
2) 如果正确,用户将被带到另一个框架开始第二道菜
3) 如果组合错误,蓝框会显示"incorrect",游戏重置
已编辑:将 link 添加到 fla - 感谢用户 batman
- https://www.dropbox.com/s/9oz9uikfbyp3rhi/thespicegame.fla?dl=0
已编辑:有人建议我使用 switch,我会尝试解决这个问题
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");
}
}
}
有很多不同的方法可以完成这个任务。总结一下你需要做什么(不管实现如何):
您需要关联每道菜的两种正确香料
您需要在内存中存储被点击的两个项目
单击第二项后,您需要使用任何机制将内存中的两项进行比较,以将正确的香料与当前菜肴相关联。
因为看起来你是一名学生并且只是在学习,我会根据你当前的框架和基于库的游戏定制我的解决方案 - 请记住,最好的解决方案将涉及使用 class 文件进行更多编程并且没有时间线代码。
将正确的香料与一道菜联系起来。最简单的方法是使用菜单按钮作为键创建一个字典。将此代码(以及所有后续代码,除非另有说明)放在您的 first 游戏框架(当前框架 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 按钮上没有看到任何点击监听器,所以我们假设你有以下内容:
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
}
Keep in mind, this code will persist for all future frames. As long you have no keyframes or empty frames on your buttons, the mouse listeners and everything will stay the same for all subsequent frames
为此,我建议只为当前的菜品文本添加另一层,然后在需要时隐藏或禁用菜品按钮(这样你只需要附加一次鼠标监听器)。
所以在每个游戏帧上,你都会有这个代码:
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
}
- 游戏布局
我正在开发一个简单的美食游戏,其中有 9 张配料图片,每道菜中只有其中两种配料。我可以指定或硬编码哪些图片是正确的。我也不需要洗牌。
用户成功点击 2 种正确的成分后,下方会显示一个输出,告诉他们他们已经正确了。如果是一个或 none,游戏将重置。
- 问题
我已经完成了 shell 到目前为止,游戏的布局已经完成,但缺少游戏的动作脚本部分,我对记忆游戏做了一些研究,但我觉得有很大的不同在编码中。
下图:
1) 用户将通过单击左侧的任意 2 个方块(例如:sp1 和 sp3)开始
2) 如果正确,用户将被带到另一个框架开始第二道菜
3) 如果组合错误,蓝框会显示"incorrect",游戏重置
已编辑:将 link 添加到 fla - 感谢用户 batman - https://www.dropbox.com/s/9oz9uikfbyp3rhi/thespicegame.fla?dl=0
已编辑:有人建议我使用 switch,我会尝试解决这个问题
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");
}
}
}
有很多不同的方法可以完成这个任务。总结一下你需要做什么(不管实现如何):
您需要关联每道菜的两种正确香料
您需要在内存中存储被点击的两个项目
单击第二项后,您需要使用任何机制将内存中的两项进行比较,以将正确的香料与当前菜肴相关联。
因为看起来你是一名学生并且只是在学习,我会根据你当前的框架和基于库的游戏定制我的解决方案 - 请记住,最好的解决方案将涉及使用 class 文件进行更多编程并且没有时间线代码。
将正确的香料与一道菜联系起来。最简单的方法是使用菜单按钮作为键创建一个字典。将此代码(以及所有后续代码,除非另有说明)放在您的 first 游戏框架(当前框架 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 按钮上没有看到任何点击监听器,所以我们假设你有以下内容:
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 }
Keep in mind, this code will persist for all future frames. As long you have no keyframes or empty frames on your buttons, the mouse listeners and everything will stay the same for all subsequent frames
为此,我建议只为当前的菜品文本添加另一层,然后在需要时隐藏或禁用菜品按钮(这样你只需要附加一次鼠标监听器)。
所以在每个游戏帧上,你都会有这个代码:
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
}