How can I capture if the user clicked a wrong button? - actionscript-3

I made a game remember for the first fishes. I found a code like this. I also have wrong buttons.
Wrong buttons list:
pinkButton.addEventListener(MouseEvent.CLICK, pinkClick);
whiteButton.addEventListener(MouseEvent.CLICK, whiteClick);
greyButton.addEventListener(MouseEvent.CLICK, greyClick);
And I use this code
import flash.events.MouseEvent;
var checkString:String = "";
 
//Create event listeners and their functions.
YellowButton.addEventListener(MouseEvent.CLICK, yellowClick);
RedButton.addEventListener(MouseEvent.CLICK, redClick);
BlueButton.addEventListener(MouseEvent.CLICK, blueClick);
/*True choices*/
function yellowClick(evt:Event):void
{
//In each event listener function, add a letter or
//string to the checkString variable.
checkString += "y";
//Then, see if the string matches or not.
check();
}
function redClick(evt:Event):void
{
checkString += "r";
check();
}
function blueClick(evt:Event):void
{
checkString += "b";
check();
}
/*True choices*/
 
//If the proper sequence is red, yellow, blue, the string would read "ryb".
function check():void
{
if(checkString == "ryb")
{
//Clear the checkString for convenience before going on.
clearString();
//CODE TO GO TO NEW FRAME
gotoAndStop(3);
}
else
{
//Make sure the string is at least 3 characters long.
if(checkString.length >= 3)
{
clearString();
gotoAndStop(1);
}
}
}
function clearString():void
{
//You will want to have a function for clearing the string.
//This is especially useful if you have a button for "start over."
checkString = "";
}
 
if I click yellow, red, blue it works. How can I make wrong choices? Do I have to write a code for all possibilities? Player has 1 chance. For example if player clicked 2 false and 1 true button,or 2 true and 1 false, this results in a loss for the player.

Use an array of values. Like
var correctSequence:Array = ["r", "y", "b"];
then have a incrementing variable to give you control over traversing the array
var arrayPosition:int = 0;
and you need a variable to hold the Boolean value of whether or not there have been any wrong guesses:
var noneWrong:Boolean = true;
Then you could do something like
private function playerNextGuess(e:MouseEvent):void{
if (e.target._color == correctSequence[arrayPosition] && noneWrong == true){
arrayPosition++;
if (arrayPosition == 3){
playerWin();
arrayPosition = 0;
}
} else {
// put whatever logic you want for when the guess is wrong
noneWrong = false;
arrayPosition++;
if (arrayPosition == 3){
playerLose();
arrayPosition = 0;
}
}
This will make it so that 3 guesses are made before the results (right or wrong) are given to the player. But it won't tell the player which were right and which were wrong. If none are wrong, the win function is called. If any are wrong the lose function is called. Is that what you wanted?
Hopefully that gets you going in the right direction. Let me know if anything I wrote isn't abundantly clear.

Related

How to create a sequence of clicks in ActionScript?

I am creating a puzzle in which the player have to click on the buttons on the right order or sequence to go to the next level (Scene 2 for example). I don't know how to do it. If any one have any idea to how to achieve this in Action Script.
Thank you
Scene 1:
enter image description here
each number is a button. Now the player has to click in the right order or sequence (1 2 3 4 5 6 7 8) to open the next level (Scene 2)
var checkString:String = "";
//Create event listeners and their functions.
btn1.addEventListener(Mouse.CLICK, oneClick);
btn2.addEventListener(Mouse.CLICK, twoClick);
btn3.addEventListener(Mouse.CLICK, threeClick);
btn4.addEventListener(Mouse.CLICK, fourClick);
btn5.addEventListener(Mouse.CLICK, fiveClick);
btn6.addEventListener(Mouse.CLICK, sixClick);
btn7.addEventListener(Mouse.CLICK, sevenClick);
btn8.addEventListener(Mouse.CLICK, eightClick);
function oneClick(evt:Event):void
{
//In each event listener function, add a letter or
//string to the checkString variable.
checkString += "on";
//Then, see if the string matches or not.
check();
}
function twoClick(evt:Event):void
{
checkString += "tw";
check();
}
function threeClick(evt:Event):void
{
checkString += "th";
check();
}
function fourClick(evt:Event):void
{
checkString += "fo";
check();
}
function fiveClick(evt:Event):void
{
checkString += "fi";
check();
}
function sixClick(evt:Event):void
{
checkString += "si";
check();
}
function sevenClick(evt:Event):void
{
checkString += "se";
check();
}
function eightClick(evt:Event):void
{
checkString += "ei";
check();
}
//If the proper sequence is one, two, three, four, five, six, seven, eight the string would read "ontwthfofisiseei".
function check():void
{
if(checkString == "ontwthfofisiseei")
{
//Clear the checkString for convenience before going on.
clearString();
//CODE TO GO TO NEW FRAME
gotoAndPlay(1, "Scene 3");
}
}
function clearString():void
{
//You will want to have a function for clearing the string.
//This is especially useful if you have a button for "start over."
checkString = "";
}
this the code i used before but it show error in the listener and it doesn't. work
To answer your updated question:
Your error is likely that Mouse.CLICK should be MouseEvent.CLICK.
Your other error is telling you that there is no scene called "Scene 3"
Let's assume you have 8 MovieClips (or buttons) that are on a timeline in Flash/Animate.
One (of many) ways to accomplish this would be the following:
Give each of those buttons an instance name. To make for less code, lets give them the name btn + their respective correct order number - so btn1, btn2, btn3 etc.
You'll need to add a click listener to each button, so they can have something happen when they are clicked.
You could do this 8 times (one for each button): btn1.addEventListener(MouseEvent.CLICK, buttonClick); but to make things simpler, you can just iterate through all the objects on the timeline and add the listener to each object whose name starts with "btn":
var totalBtns:int = 0; //create a var to store how many buttons there are
//loop through each child of the current timeline
var i:int = numChildren;
while(i--){
//if the child's name starts with 'btn'
if(getChildAt(i).name.indexOf("btn") == 0){
//add the click listener
getChildAt(i).addEventListener(MouseEvent.CLICK, buttonClick,false,0,true);
totalBtns++; //increase the total buttons variable by 1
}
}
This also means less work later if you add/remove buttons
You need a way to track when a button was clicked. To accomplish this, we'll use an array.
var clickArray:Array = []; //this creates a new array
//When a button is clicked, you add it to this array
lets create the function that is called when a button is clicked:
function buttonClick(e:Event):void {
//add the item that was just clicked (represented by the event's currentTarget property) to the array
//so if btn1 was just clicked, btn1 would be e.currentTarget
clickArray.push(e.currentTarget);
//now disable the button so it can't be clicked anymore
SimpleButton(e.currentTarget).enabled = false;
//check if all button have been clicked
if(clickArray.length == totalBtns){
//lets go through every item in the array, and see if it's in the right order
var ctr:int = 0; //a counter to keep track of the expected next number var i:int = 0; //iterator for the for loops
for(i=0;i<clickArray.length;i++){
//lets convert everything after the 3rd character of the name to a number - so for btn1, that would be 1
if(parseInt(clickArray[i].name.substring(3)) == ctr + 1){
ctr++; //increment the counter to the next expected number
}else{
break; //leave the for loop early since a click was out of place
}
}
//if the correct order was achieved
if(ctr == totalBtns){
nextScene(); //or however you continue
}else{
//the correct order was NOT acheived
//make all the buttons clickable again
for(i=0;i<clickArray.length;i++){
SimpleButton(clickArray[i]).enabled = true;
}
//reset the array
clickArray = [];
//probably want to tell the user to try again
}
}
}

AS3 Check if movieclips inside array are all the same color

I've created a simple puzzle game and have uploaded it to kongregate, now I want to upload highscores (the least amount of moves = better) to it using their API. To make sure no one can cheat the system (submitting the score before puzzle is finished) I need to make sure that none of the pieces in the puzzle are black. All the pieces of the puzzle are movieclips and are inside a array called buttons.
I've currently got this:
public function SumbitScore(e:MouseEvent)
{
for (var v:int = 0; v < buttons.length; v++)
{
if (buttons[v].transform.colorTransform.color != 0x000000)
{
_root.kongregateScores.submit(1000);
}
}
}
but I think that will submit the score as soon as it checks a movieclip that is not black and it'll ignore the rest.
I think the way to go is to keep track of whether or not an 'empty button' is found in your for-loop. After the loop you could submit the score if no empty tiles were found or let the player know the puzzle has to be completed before submitting.
I've added some comments in the code below:
// (I changed the function name 'SumbitScore' to 'SubmitScore')
public function SubmitScore(e:MouseEvent)
{
// use a boolean variable to store whether or not an empty button was found.
var foundEmptyButton : Boolean = false;
for (var v:int = 0; v < buttons.length; v++)
{
// check whether the current button is black
if (buttons[v].transform.colorTransform.color == 0x000000)
{
// if the button is empty, the 'foundEmptyButton' variable is updated to true.
foundEmptyButton = true;
// break out of the for-loop as you probably don't need to check if there are any other buttons that are still empty.
break;
}
}
if(foundEmptyButton == false)
{
// send the score to the Kongregate API
_root.kongregateScores.submit(1000);
}
else
{
// I'd suggest to let the player know they should first complete the puzzle
}
}
Alternatively you could let the player know how many buttons he still has to finish:
public function SubmitScore(e:MouseEvent)
{
// use an int variable to keep track of how many empty buttons were found
var emptyButtons : uint = 0;
for (var v:int = 0; v < buttons.length; v++)
{
// check whether the current button is black
if (buttons[v].transform.colorTransform.color == 0x000000)
{
// if the button is empty increment the emptyButtons variable
emptyButtons++;
// and don't break out of your loop here as you'd want to keep counting
}
}
if(emptyButtons == 0)
{
// send the score to the Kongregate API
_root.kongregateScores.submit(1000);
}
else
{
// let the player know there are still 'emptyButtons' buttons to finish before he or she can submit the highscore
}
}
Hope it's all clear.
Good luck!

Verify if buttons are clicked actionscript3

I have 9 movieclips, all with their function and i want to show a message after the user clicked on all the movieclips.
EX:
button1.addEventListener(MouseEvent.CLICK, showText1);
button2.addEventListener(MouseEvent.CLICK, showText2);
button3.addEventListener(MouseEvent.CLICK, showText3);
function showText1(e.Event)
{
text1.visible=true;
}
...
How do i check if all the buttons are clicked and after that, show a message?
Thank you.
Call a function like test on each click. In that function do something similar to:
function test() {
for (var i:uint = 0; i < 9; i++) {
if (this["text" + i].visible != false) { // do proper check; maybe visible, or maybe some variables - whatever fits your needs
return false;
}
}
return true; // do whatever you want here - all are 'clicked';
}

else statement always run even the answer is true

tnx! it works. but when i tried to add another object named, p1_2 and add the "trace" thing to the code,
it goes back to the same problem.
p1_1.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
question.text = "shape?";
}
submit.addEventListener(MouseEvent.CLICK, onClickss);
function onClickss(e:MouseEvent):void
{
trace("ans.text = "+ans.text);
if (ans.text == "circle")
{
p1_1.visible = false;
}
else
{
gotoAndStop(6);
}
}
p1_2.addEventListener(MouseEvent.CLICK, onClick2);
function onClick2(e:MouseEvent):void
{
question.text = "Color?";
}
submit.addEventListener(MouseEvent.CLICK, onClickss2);
function onClickss2(e:MouseEvent):void
{
trace("ans.text = "+ans.text);
if (ans.text == "red")
{
p1_2.visible = false;
}
else
{
gotoAndStop(6);
}
}
what should i do.? do i nid to seperate p1_2 in another frame
and make another inputtextfield for it?
im planning to add 5 objects on the stage. until p1_5. -_-
Before the line...
if(ans.text == "circle") {
...add:
trace("ans.text = "+ans.text);
Clicking the submit button will trace the actual value of ans.text to the output panel when you test your movie in Flash. If you get a null reference error then ans has not been instantiated. Otherwise, knowing the actual value of ans.text should point you in the right direction to solve the problem.

How to make 3 buttons be clicked in a certain order go to certain frame? Flash AS3

I am struggling making 3 buttons be able to go to a frame when the buttons are clicked in a certain order. For example the buttons are labeled
button 1
button 2
button 3
and the order that they are needed to be clicked in is button 3, button 1, button 2 for them to go to the next frame, How can I achieve this?
This is what I have so far.
var clicked = MouseEvent.CLICK
var buttons = new Array(YellowButton, BlueButton, RedButton);
for (var a=0; a<buttons.lenght; a++)
{
buttons[a].buttonMode=true
buttons[a].addEventListener(clicked,RedYellowBlue);
}
function RedYellowBlue(event:MouseEvent):void { gotoAndStop(20); }
Thanks for your help in advance
What I recommend is to create a string object that will be assembled by clicking the buttons, and then compared to a particular sequence that you set. The beauty of this is that you can make it work for sequences of any length.
The biggest thing here is, you don't want the same event listener on all three buttons. You want their actions to be unique, otherwise, clicking one is the same as clicking all the others (which is your current code's problem.)
var checkString:String = "";
//Create event listeners and their functions.
YellowButton.addEventListener(Mouse.CLICK, yellowClick);
RedButton.addEventListener(Mouse.CLICK, redClick);
BlueButton.addEventListener(Mouse.CLICK, blueClick);
function yellowClick(evt:Event):void
{
//In each event listener function, add a letter or
//string to the checkString variable.
checkString += "y";
//Then, see if the string matches or not.
check();
}
function redClick(evt:Event):void
{
checkString += "r";
check();
}
function blueClick(evt:Event):void
{
checkString += "b";
check();
}
//If the proper sequence is red, yellow, blue, the string would read "ryb".
function check():void
{
if(checkString == "ryb")
{
//Clear the checkString for convenience before going on.
clearString();
//CODE TO GO TO NEW FRAME
gotoAndStop(20);
}
else
{
//Make sure the string is at least 3 characters long.
if(checkString.length >= 3)
{
clearString();
gotoAndStop(foo);
}
}
}
function clearString():void
{
//You will want to have a function for clearing the string.
//This is especially useful if you have a button for "start over."
checkString = "";
}
That should be sufficient to get you started. If you need additional help, feel free to comment on my answer.
To consolidate #JasonMc92's answer, you could use the name of the target as the checkString letter so you only need to call one function for all buttons.
yellowButton.addEventListener(MouseEvent.CLICK, redYellowBlue);
redButton.addEventListener(MouseEvent.CLICK, redYellowBlue);
blueButton.addEventListener(MouseEvent.CLICK, redYellowBlue);
function redYellowBlue(e:MouseEvent){
checkString += e.currentTarget.name.substr(0,1);
check();
}
//... continue on with Jason's functions
Or, since you've set them up in an array, you could also just use the buttons' indices as your check sequence. Something like:
function redYellowBlue(e:MouseEvent){
checkString += String(buttons.indexOf(e.currentTarget));
check();
}
function check(){
if (checkString == '201') {
// ... handle correct sequence
}
}
Also, just a best practice tip:
only use a leading capital for names of classes. Instance names and function names should start with a lowercase. (leading cap won't break anything, but it's standard practice)
//Here is code
var buttons:Array = new Array(YellowButton, BlueButton, RedButton);
for(var i:int=0;i< buttons.length;i++)
{
buttons[a].buttonMode=true
buttons[a].addEventListener(MouseEvent.CLICK,buttonsClickHandler);
}
function buttonsClickHandler(event:MouseEvent):void {
var button:MovieClip = MovieClip(event.target);
switch(button.name)
{
case "YellowButton":
case "BlueButton":
gotoAndStop(2);
break;
case "RedButton":
gotoAndStop(3);
break;
}
}