-
Array help!
Hi there, i have made a game, and have developed a high scores system to use, which all works fine, the only thing i am having trouble with is the way to input the players names, basically what i want is an old school name input system where on the screen u can see one letter in a textbox at a time with a button either side of it (kinda like this: <| A |> ) now when you click on the right button i want the letter in the textbox to change from A to B, then if i click again, C and so on, same with the left button ( if letter is "B" and left is pressed letter turns to "A")
so what i have so far is an array containing all of the letters, and the functions to detect when the button is pressed,
my code:
var array:Array = new Array("a","b","c","d","e","f","g","h","i","j","k", "l","m","n","o","p","q","r","s","t","u","v","w","x ","y","z");
now in my function what i need is a way of detecting which element of the array is selected, sending the letter to the textbox, and when the button is pressed for the letter to change, i didnt really have any idea how to do this but im guessing its something like this:
for(var i:int = 0; i < array.length -1; i+1){
textbox.text = array[i];
i++;
}
but this doesnt exactly work as i just guessed it and guessed it wrong lol, could someone please help me out??
Thanks in advance
Dan
-
Actionscript Code:
for(var i:int = 0; i < array.length -1; i+1){ textbox.text = array[i]; i++; }
I just don't understand what you wanted to get from this loop, and why you used loop at all.
You're incrementing "i" by 1 here: i+1 and then you do it again in your loop's body "i++"
So after each iteration, you're obviously incrementing your "i" variable by 2.
Read about as3 loops in adobe's official docs.
As to your question, if I was to do something like this, I'd rather create some variable to keep the current letter's index, and incremented / decremented it by one every time someone pushes the button:
Actionscript Code:
var minLetterIndex:uint = 0; var maxLetterIndex:uint = array.length -1; var currentLetterIndex:uint = 0; function arrowClick(e:MouseEvent):void { if (e.target == leftArrow) { if (currentLetterIndex > minLetterIndex) textBox.text = array[currentLetterIndex], currentLetterIndex --; else if (currentLetterIndex == minLetterIndex) textBox.text = array[maxLetterIndex], currentLetterIndex = maxLetterIndex; } else if (e.target == rightArrow){ if (currentLetterIndex < maxLetterIndex) textBox.text = array[currentLetterIndex], currentLetterIndex ++; else if (currentLetterIndex == maxLetterIndex) textBox.text = array[minLetterIndex], currentLetterIndex = minLetterIndex; } }
Something like that.
-
A good program flow for this would be to make the string substring itsself to append the first element with the back element everytime you clicked the button.
Something like this
Actionscript Code:
tf.text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()"; tf.x = tf.y = 0; // where you want it placed on the screen. addChild(tf); function arrowClick(e:MouseEvent):void{ if (e.Target == rightArrow){ tf.text = tf.text.substr(1) + tf.text.charAt(0); } if(e.target == leftArrow){ tf.text = tf.text.charAt(tf.text.length - 1 + tf.text.substr(0,tf.text.length-2); } if (e.target == pressButtonToAddLetter){ NAMESTRING = NAMESTRING + tf.text.charAt(0); } }
I probably made a lot of mistakes in that. But you should get the idea from there.
A brief short example of this is the following string:
1234
You would have the other letters covered by another layer, with a transparency cut out of it to only see the first letter
In this case you would only see 1, the rest would be hidden by the upper layer.
Then, if you hit the right arrow (or right button), the goal that we want to achieve is to get the first letter of the string to be 2, right?
So we substring from number 2-4 inclusive, then append the first character
thus giving
2341
If we hit right again, we would get substring from 3-1 inclusive, then append first character
giving 3412.
But if we decided that we wanted to have 2 afterall and hit left, we want the string to be "2341" again, right?
No problem.
take the last character and put it on the front, then substring from 3-1 inclusive.
Giving 2341 again.
See how this works?
Then if you want to add it to the name, you just have to add the first character in the string.
Also, I can't remember if it's tf.length() or tf.length. I've been switching between python, java, as2, and as3 too much, so if I mixed the languages a bit in my code example I'm sorry. Hah.
Last edited by Xanathalas; 03-11-2011 at 12:06 AM.
-
thanks all, caseyryan, your right i didnt need it in a loop, i was just not to sure how to use arrays up until now lol, i figured out a very simple way of doing it thanks to both of your code, thanks a lot peoples, problem solved!
Tags for this Thread
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
|