Arrays

ok i’ll have an input box in which the user can enter a room number

then an array of all the room numbers
an array of the x coordinate of the corresponding room
an array of the y coordinate of the corresponding room
and an array of the floor number of the corresponding room

i need to create a function that will check the value of the input box onPress of a button against the first array and if it finds a match then it will go and get the corresponding values from the other arrays and send them to variables

i’ll work on a sample file to post since the main file is too large

any help is appreciated

here’s the file

Hi Shuga,
First question is does the room number refer to the floor number, - if it does you don’t really need the floors array.
Writing a multi array was not too much of a problem, however I could only get the for…loop to extract the xPos, so I had a liitle help to with the final syntax from CyanBlue - so thanks to him.
Also, I changed the x & y co -ordinates in the arrays so that I could see the ball move, and I commented out the animation code on the ball mc as I couldn’t get it to work with the arrays,
I didin’t have the time to play around with that side, but I guess you know what you are doing there OK.
Basically, when a room number is typed in and the button pressed, the ball will “jump” to the right place and the playHead will goto the right frame relative to the floor array.

Hope that helps

SteveD

sweet … you da man … i’ve edited it and i got the ball’s movment to work. just one thing that’s going to be a problem i think.

when the value of the array is checked it checks every value in the array … ofcourse

so if i add an }else{ statement following the initial if statement and tell it to trace an error message … it traces that error message 5 times for each of the values in the array that is not equal to the value in the input box … d/l the file and check out the code. this isn’t a problem with 6 rooms but i will have 500+ and i worry about that lagging the computers

any ideas on how to get around that

b/c i think if they type in a room number that does not exist i should put up an error message. but i can’t put it up 499 times.

thanks for all your help … i really appreciate it

Will your room numbers be in order from smallest to largest? If they are then you could do a binary search to find the room number which will take less processor power.

i could make them in order in the array yes … i don’t understand how to do a binary search though … please elaborate =)

thanks very much

i’ll make a quick example

thanks =)

/me waits excitedly

I could not get it to work, which is really annoying me because I can’t figure out what is wrong with my code, flash seems to just be doing whatever it feels like.

I am including my fla incase anyone else wants to see if they can fix whatever is wrong with my coding.

About binary searches it basically takes the lenght of the array and divides it in half. Then it looks at that index in the array and determines if the value contained there is higher or lower than the one entered. Then it cuts it in half again and looks at the middle point and continues until it finds it or there is nothing left to search.

I can’t figure out what is wrong with my code but for some reason it isn’t evaluating the if statements correctly and so the search won’t work.

Hi,
Just a bit of preamble, not being a coding guru or anything like that, I have used arrays but never understood them properly, this little exercise has improved my knowledge of arrays and has done me a power of good.

Now onwards

when the value of the array is checked it checks every value in the array … ofcourse

OK, with a bit of patience and the help of Moocks ASDG I have written a function( if you have the book, check p.234) which searches the array and returns the correct room number, I have put a small message box up to demonstrate how it works. Also room.text has been changed to a var room and the code has been removed from the button and placed in frames 1 & 2. Anything you don’t understand ( tho’ I guess you are getting the nack of arrays as well by now) just ask.

Q. for you - are the floor numbers related to the room numbers ? I mean does roomNum 4141 mean the room is on the 4th floor ,
if it does you can dump the floors array and use indexOf or toString to get the floor number which will save you some typing.
Anyway, hope the attached is OK - little by little we will get there.

Sorcerer - I know little about binary searches, but apart from a few typos I can see nothing wrong with your code, I think I will have a little session with Moock again to see if I can sort this out.

Cheers for now

SteveD

It works … wonderful … thank you so much

the floor numbers do correspond to the first number of each room’s number

how would take the first number only. some rooms have a letter after them so i couldn’t just strip off the last 3 … i need to just take the first number … if that’s possible … that would be wonderful

use

var = string.charAt(0)

where var will be assigned the first letter or number in the string and string is the variable which holds the room number entered.

the 0 indicates you want the first character in the string 1 would be the 2nd and so on.

i think i understand … but i don’t understand how to implement that into the example file :frowning:

this is what i put in:


_root.floor = roomNums*.charAt(0);

but it doesn’t work. i’ve tried several different combinations but i can’t make it work :frowning:

change your _root.floor variable to something else like _root.floorNum

floor is a keyword in flash and could cause problems if you use this, you will also want to get the charAt of the input text not what is in the array.

solved it =)


_root.floorNumber = String(roomNums*).charAt(0);

thanks =)