저는 9 개의 재료 사진이 있고 모든 요리에이 재료 중 2 개만있는 간단한 음식 게임을 작업하고 있습니다. 정확한 그림을 지정하거나 하드 코딩 할 수 있습니다. 나는 그들을 섞을 필요도 없습니다.
사용자가 2 개의 올바른 재료를 성공적으로 클릭하면 다음과 같은 출력이 표시되어 올바른 재료를 얻었음을 알려줍니다. 하나 또는없는 경우 게임이 재설정됩니다.
지금까지 게임의 레이아웃은했지만 게임의 액션 스크립트 부분이 부족한 쉘을 해봤고, 메모리 게임에 대한 조사를 좀 해봤지만 코딩에 많은 차이가 있다고 느낍니다.
아래 그림 :
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");
}
}
}
이 작업을 수행하는 방법에는 여러 가지가 있습니다. 구현에 관계없이 수행해야하는 작업을 요약하려면 다음을 수행하십시오.
각 요리에 맞는 두 가지 향신료를 연결해야합니다.
클릭 한 두 항목을 메모리에 저장해야합니다.
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
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다