Reputation: 27
I'm working on a simple food game where there are 9 pictures of ingredients and every dish has only two of these ingredients found in them. I can specify or hard code which pictures are correct. I dont need to shuffle them too.
The user when successfully clicked on the 2 correct ingredients, an output will be shown below to tell them they have got it correct. If its one or none, the game will reset.
I have done the shell so far where the layout of the game has been done but lacking the actionscript portion of the game, i have done some research on memory games but i feel that there is much difference in the coding.
Picture below:
1) The user will start by clicking any 2 square on the left (eg: sp1 and sp3)
2) If it is correct, the user will be brought to another frame to start on the second dish
3) If the combination is wrong, the blue box will show "incorrect" and game resets
Edited: Added link to fla - Thanks user batman - https://www.dropbox.com/s/9oz9uikfbyp3rhi/thespicegame.fla?dl=0
Edited: I have been suggested using switch, will try to work on that
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");
}
}
}
Upvotes: 0
Views: 55
Reputation: 14406
There are a lot of different ways to accomplish this task. To summarize what you need to do (regardless of the implementation):
You need to associate the two correct spices that go with each dish
You need to store in memory the two items that get clicked
After the second item is clicked, you need to compare to the two items in memory with whatever mechanism you use to associate the correct spices with current dish.
Since it would appear you are a student and just learning, I'll tailor my solution to your current frame and library based game - keeping in mind the best solution would involve much more programming using class files and no timeline code.
Associate your correct spices with a dish. The easiest way to do this, is create a dictionary using the dish buttons as the key. Put this (and all subsequent code unless noted) on your first game frame (frame 4 currently).
//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];
Create a variable to hold the first ingredient that was clicked. Also create a variable that stores the current dish:
var firstSpice; //to hold the first spice clicked
var currentDish; //to hold the current dish
Populate the current dish on whatever frame is for that dish:
currentDish = chickBtn2; //put this on the frame where chicken is the dish
You need to populate the first click spice variable when a spice is clicked. I don't see any click listeners on your spice buttons, so let's assume you have the following:
sp1.addEventListener(MouseEvent.CLICK, spiceClick, false,0,true);
sp2.addEventListener(MouseEvent.CLICK, spiceClick, false, 0, true);
//...etc with all spices
Or the shorthanded sloppy way to do all 9 in 3 lines:
for(var i:int=1; i<= 9;i++){
this["sp" + i].addEventListener(MouseEvent.CLICK, spiceClick, false, 0, true);
}
Then in the click handler:
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
To that end, I would recommend just adding another layer for the current dish text, and then just hide or disable the dish button when you need to (that way you only attach the mouse listener once).
So on every game frame, you'd have this code:
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
EDIT
Here is the code I used for testing: (all on frame 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
}
Upvotes: 1