Alright, here's my situation. I have an array that consists of 5 words inside of a movie clip. Everytime the user enters the correct current letter, the movie clip will go to frame 2, and the array will update to the next character until the end of the word, then it will move on to the next word. I'm more familiar with java than flash, so I'm trying to learn how flash arrays work and if any of my syntax is incorrect, I'd appreciate some advice. Here is my code, I commented where I know my error is. I want to know how to get the code of the current letter and compare it to the key being pressed. Thanks
onClipEvent(load)
{
myArray = new Array("Jimmy","is","very","cool!");
current = 0;
currentIndex = 0;
}
onClipEvent(enterFrame)
{
//word is the current word
word = (myArray[current]);
//letter is the current letter
letter = (myArray[current]).charAt(currentIndex);
//length is length of word
lengthw = word.length;
onKeyDown = function()
{
//**I'm not sure what to put for the second part of this condition statement
if(Key.getCode() == letter.getAscii().getCode())
{
this.gotoAndPlay(2);
if(currentIndex < (lengthw - 1))
{
currentIndex++;
}
else
{
current++;
currentIndex = 0;
}
}
}
}
Key codes and Keyboard funcitons...need help
Started by dontlookback93, Sep 25 2006 08:23 PM
5 replies to this topic
#1
Posted 25 September 2006 - 08:23 PM
#2
Posted 25 September 2006 - 10:35 PM
i've done a similar thing it should be similar to what your after i'll post the code and you can have a look and get back to me if it is...
hope it somewhat helps ya i did the word not using an array...i guess you could repeat this method 5 times and it would do a similar thing turn my traces back on to have a look at how it works in the output box
_root.login_screen._alpha = 0;
this.wordToType = "word";
this.lettersToType = this.wordToType.split("");
this.letterIndex = 0;
Key.addListener(this);
this.onKeyDown = function():Void {
if(this.lettersToType[this.letterIndex] == String.fromCharCode(Key.getAscii())) {
this.letterIndex++;
_root.login_screen._alpha = 0;
_root.login_screen._visible = false;
//trace(this.wordToType.substr(0, this.letterIndex));
} else {
this.letterIndex = 0
_root.login_screen._alpha = 0;
_root.login_screen._visible = false;
//trace("restart");
}
if(this.letterIndex == this.lettersToType.length) {
Key.removeListener(this);
_root.login_screen._alpha = 100;
_root.login_screen._visible = true;
//trace("word completed");
}
}
hope it somewhat helps ya i did the word not using an array...i guess you could repeat this method 5 times and it would do a similar thing turn my traces back on to have a look at how it works in the output box
#3
Posted 26 September 2006 - 09:48 AM
i just need a simple function...
say i have a given character, is there a function in flash that can translate that into a key code so i can put if(Key.isDown(Key.(\\key code for character\\))). I'm sure your way would work, but i'm really close and don't feel like changing around the context of my program. If there's no way to do what I just asked, then i'll do it your way, thanks.
say i have a given character, is there a function in flash that can translate that into a key code so i can put if(Key.isDown(Key.(\\key code for character\\))). I'm sure your way would work, but i'm really close and don't feel like changing around the context of my program. If there's no way to do what I just asked, then i'll do it your way, thanks.
#4
Posted 26 September 2006 - 05:53 PM
For some things, there arent easy ways. And his is pretty simple, just take the time to read through it, so you understand it.
#5
Posted 26 September 2006 - 08:30 PM
you seem to already be doing it the way i would have suggested other than my way...you can change the ascii code to index but not as keys....unless u could try having your word and your index number relating to each letter of the array may be a lil longer but....might work for ya
#6
Posted 01 October 2006 - 05:41 PM
Hello,
I took your code and rewrote it a bit, tried to keep it in your style of coding as much as possible but couldn't help myself in certain places. Inserted comments and it traces what is happening as you type keys. Remember it is case sensitive so hold shift on the first letter.
Also added in a check for when the whole sentence is complete. Figured that would be your next step.
It no longer uses onEnterFrame, switched it to use a more efficient Key Listener.
If you have any questions feel free to ask.
Max (PHD)
I took your code and rewrote it a bit, tried to keep it in your style of coding as much as possible but couldn't help myself in certain places. Inserted comments and it traces what is happening as you type keys. Remember it is case sensitive so hold shift on the first letter.
Also added in a check for when the whole sentence is complete. Figured that would be your next step.
It no longer uses onEnterFrame, switched it to use a more efficient Key Listener.
If you have any questions feel free to ask.
Max (PHD)
onClipEvent(load) {
myArray = new Array("Jimmy","is","very","cool!");
current = 0;
currentIndex = 0;
//word is the current word
word = (myArray[current]);
//letter is the current letter
letterCode = word.charCodeAt(currentIndex);
// ---------------------------------
// Key Capturing.
// ---------------------------------
// Object to listen for key actions.
captureKeys = new Object();
// Listen for key down action.
captureKeys.onKeyDown = function(){
trace('--------------- KEY PRESS -----------------');
trace("Key pressed: (" + Key.getAscii() + ") compare to next letter: (" + letterCode + ")");
if(Key.getAscii() == letterCode){
trace('Key pressed matched the letter!');
// { PUT CODE TO DO SOMETHING } you had gotoAndPlay(2);
if(currentIndex != (word.length-1)){
// There are more letters in word to match. get next letter.
currentIndex++;
letterCode = word.charCodeAt(currentIndex);
} else {
// Finished word. Move on to next word and it's first letter.
trace('finished word!');
current++;
currentIndex = 0;
word = myArray[current];
letterCode = word.charCodeAt(currentIndex);
if(current == myArray.length){
trace("Finished whole sentence!");
// do something.
}
}
}
}
// Add your object as a Key listener.
Key.addListener(captureKeys);
}
Edited by PixelHiveDesign, 01 October 2006 - 05:41 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
