-
[F8] Checking for a value in an array?
Ok, this is what I want to do:
1. Create a new array with basically nothing in it on the root.
2. When a button is clicked the array is updated or changed with whatever that button says to put in the array. There would be several buttons and each one would change the set of options in that array.
3. There is an input box and whenever someone enters the text in it and hits a button lets say something happens if the text matches one of the values listed in the current array.
Can anyone help here?
-Kirk II
-
Maybe something like this...
Code:
array = [];
matchFound = false;
Button1.onRelease = function() {
array.push("whatever");
}
Button2.onRelease = function() {
array.push("somethingElse");
}
searchButton.onRelease = function() {
for(n=0; n<array.length; n++) {
if(inputBox.text == array[n]) {
trace("match");
matchFound = true;
}
else if(n == array.length && !matchFound) trace("no matches found");
}
}
-
Ok, so understanding the code...
First off, I appreciate you. Thank you. Ok, so I'm trying to understand...
Let's say this part is on the root:
array = [];
matchFound = false;
This is for a button on the stage:
Button1.onRelease = function() {
array.push("whatever");
}
This is for another button on the stage:
Button2.onRelease = function() {
array.push("somethingElse");
}
This would be for the button to check the answer and should happen on the button release?
searchButton.onRelease = function() {
for(n=0; n<array.length; n++) {
if(inputBox.text == array[n]) {
trace("match");
matchFound = true;
}
else if(n == array.length && !matchFound) trace("no matches found");
}
}
-
Right. You might want to put matchFound = false; at the very beginning of the search button function (before the for loop), too. That way the user won't get erroneous results if they search more than once.
-
This is what I've got, but the button does nothing when clicked. Also, keep in mind that I will have multiple things in the array, not just one word. I appreciate your help greatly.
on (release){
matchFound = false;
searchButton.onRelease = function() {
for(n=0; n<array.length; n++) {
if(_parent.answer.text == array[n]) {
trace("match");
matchFound = true;
}
else if(n == array.length && !matchFound) trace("no matches found");
}
}
}
-
There are 2 kinds of button code. One kind is code that goes on a frame, refers to the button with a target path, and assigns a function to a certain button event. Example:
Code:
searchButton.onRelease = function() {
//code
}
The other kind is code that goes on the button itself, and uses an on() handler. Example:
Code:
on(release) {
//code
}
You want to use only one or the other. Do either...
Code:
//frame actions
array = [];
matchFound = false;
Button1.onRelease = function() {
array.push("whatever");
}
Button2.onRelease = function() {
array.push("somethingElse");
}
searchButton.onRelease = function() {
matchFound = false;
for(n=0; n<array.length; n++) {
if(inputBox.text == array[n]) {
trace("match");
matchFound = true;
}
else if(n == array.length && !matchFound) trace("no matches found");
}
}
or...
Code:
//frame actions
array = [];
matchFound = false;
//button 1 actions
on(release) {
array.push("whatever");
}
//button 2 actions
on(release) {
array.push("somethingElse");
}
//search button actions
on(release) {
matchFound = false;
for(n=0; n<array.length; n++) {
if(inputBox.text == array[n]) {
trace("match");
matchFound = true;
}
else if(n == array.length && !matchFound) trace("no matches found");
}
}
Also make sure you're using actual button symbols (not MCs with button code), because that makes a difference in your coding style (MC buttons are of a different scope, whereas true buttons are not).
-
Thank you for all your help. I am going to take all of these notes and continue working on this. Great help from you. I appreciate you much!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|