onKeyDown question

Hello, this is my first time on a forum, I hope I do this right :puzzled:…

I am developing a game in Flash MX 2004 and I am using the onKeyDown() function with a Listener to know what key the user has pressed. I then compare that key with a letter on a block and if it is the same, the block goes up and the ball bounces on it. The problem is the last key pressed is stored somewhere and I can’t reinitialize it. For example, if the first block is an “a” block and the user pressed “a” and the second block is also an “a” block, he doesn’t have to press the key again for the second block to go up.

I would like to be able to reinitialize the key (if there were a Key.SetKey() that would be great, but that doesn’t seem to exist) or to simulate an onKeyDown() or anything else that would work…

Thanks!

you could put an IF statement say

if(x!=2){
x=2
...rest of your code...
}

on the onKeyDown() function, then have an onKeyUp() function that resets the variable to 1. that work?

Prophet.

yeah thats what I was gonna suggest…start off before the Key.onKeyDown() enter a variable = 1 or something…then when you do the Key.onKeyDown() add in variable = 2, then add in a Key.onKeyUp() and do variable = 1…that’ll reset it each time a key is pressed and should fix the problem.

Hello!
I tried what you suggested and that doesn’t work. I use : myListener.onKeyDown = function (), not Key.onKeyDown(). I don’t know if that changes anything. I remove myListener afterwards, too.
I hope you can help me :slight_smile:

Ignore how it says PHP and not actionscript, PHP is color coded so I used to just for you example so it’d be easier to understand.


      onClipEvent (enterFrame) {
	(your variable) = new Object();
	Key.addListener(your variable);
	with (_root.your variable) {

//this "with" statement is not needed, it's only so you don't have to specify the target everytime//

		mario.onKeyDown = function() {
			(new variable) = "0";
			if (Key.isDown(Key.RIGHT)) {
				(new variable) = "1"
			}
			if (Key.isDown(Key.LEFT)) {
				(new variable) = "2";
		};
		mario.onKeyUp = function() {
		        (new variable) = "0"
	}
}


this is just a for instance, you can use any buttons or variables.

Hello!
Hum, I was wondering if there were any way to simply empty the buffer after each keypress? That would solve my problem instantly.
Thanks! :slight_smile:

no.
but scarces method looks a bit daunting i gotta say :wink:

on your code, before you perform any actions, ie. after all your onKey stuff and listeners and what not… before you actually DO what you want to do… put this before it

if(x!=2){

and the following just after your actions

x=2}

then close off all your key down stuff… then put an identical to your onKeyDown function but trading in Down with Upwith the following action:

{x=1}

if you give us the code you have at the moment we will be better able to demonstrate the simplicity of it

Prophet.

Hello!
Ok, I did what you said and it still doesn’t work. :frowning: I will write the code for my onKeyDown function here but I don’t think it’s easy to understand without a lot of details (and my comments are in french…)
Anyway here it is with the stuff you told me to add :

var myListener = new Object();
myListener.onKeyDown = function () {
if(x!=2){
if(_root[“m”+cote]._visible == false){
//Si la lettre tapée par l’usager est égale à celle du bloc
if (String.fromCharCode(Key.getAscii()) == _root[“b”+cote+bloc].lettre.text) {
//Augmente la vitesse de la balle
Intervalle = 30;
for(i=1;i<=4;i++){
for(j=1;j<=_global.nbBloc[i-1];j++){
if(String.fromCharCode(Key.getAscii()) == _root[“b”+i+j].lettre.text) {
//Soulève tous les blocs contenant la lettre tapée
_root[“b”+i+j].action.gotoAndPlay(“c”+i+j+“s”);
}
else
{
_root[“b”+cote+bloc].action.gotoAndPlay(“v”+cote+bloc+“b”);
_root[“b”+i+j].action.gotoAndPlay(“c”+i+j+“b”);
}
}
}
//Soulève le bloc vert contenant la lettre tapée
_root[“b”+cote+bloc].action.gotoAndPlay(“v”+cote+bloc+“s”);
}
else{
//Si la lettre tapée par l’usager n’est pas égale à celle du bloc
for(i=1;i<=4;i++){
for(j=1;j<=_global.nbBloc[i-1];j++){
if (String.fromCharCode(Key.getAscii()) == _root[“b”+i+j].lettre.text){
//Soulève tous les blocs contenant la lettre tapée
_root[“b”+i+j].action.gotoAndPlay(“c”+i+j+“s”);
}
else
{
//Mets les bloc à “bas”
_root[“b”+i+j].action.gotoAndPlay(“c”+i+j+“b”);
}
//Si on tape la barre d’espacement
if (Key.getAscii() == 32){
//Met tous les blocs à “bas”
_root[“b”+i+j].action.gotoAndPlay(“c”+i+j+“b”);
}
//Met le bloc vert à “bas”
_root[“b”+cote+bloc].action.gotoAndPlay(“v”+cote+bloc+“b”);
}
}
}
}
x = 2;
}
}
myListener.onKeyUp = function () {
x=1;
}
Key.addListener(myListener);

Hope it’s useful and that you could understand it.
Thanks!

ok im starving hungry while im posting this so ive actually no idea if this will even make a difference but i moved the x line:

var myListener = new Object();
myListener.onKeyDown = function () {
if(_root["m"+cote]._visible == false){
//Si la lettre tapée par l'usager est égale à celle du bloc
if (String.fromCharCode(Key.getAscii()) == _root["b"+cote+bloc].lettre.text) {
//Augmente la vitesse de la balle
Intervalle = 30;
for(i=1;i<=4;i++){
for(j=1;j<=_global.nbBloc[i-1];j++){
if(String.fromCharCode(Key.getAscii()) == _root["b"+i+j].lettre.text) {
//Soulève tous les blocs contenant la lettre tapée
if(x!=2){
_root["b"+i+j].action.gotoAndPlay("c"+i+j+"s");
x=2
}
else
{
//commented the following x ilnes coz im not sure what is what here so i could have put it in the wrong place ;)
//if(x!=2){
_root["b"+cote+bloc].action.gotoAndPlay("v"+cote+bloc+"b");
_root["b"+i+j].action.gotoAndPlay("c"+i+j+"b");
//x=2
}
}
}
//Soulève le bloc vert contenant la lettre tapée
_root["b"+cote+bloc].action.gotoAndPlay("v"+cote+bloc+"s");
}
else{
//Si la lettre tapée par l'usager n'est pas égale à celle du bloc
for(i=1;i<=4;i++){
for(j=1;j<=_global.nbBloc[i-1];j++){
if (String.fromCharCode(Key.getAscii()) == _root["b"+i+j].lettre.text){
//Soulève tous les blocs contenant la lettre tapée
_root["b"+i+j].action.gotoAndPlay("c"+i+j+"s");
}
else
{
//Mets les bloc à "bas"
_root["b"+i+j].action.gotoAndPlay("c"+i+j+"b");
}
//Si on tape la barre d'espacement
if (Key.getAscii() == 32){
//Met tous les blocs à "bas"
_root["b"+i+j].action.gotoAndPlay("c"+i+j+"b");
}
//Met le bloc vert à "bas"
_root["b"+cote+bloc].action.gotoAndPlay("v"+cote+bloc+"b");
}
}
}
}
}
}
myListener.onKeyUp = function () {
trace(x)
//added a trace as well to check it comes up ok ;)
x=1;
}
Key.addListener(myListener);

that work? btw i had no idea ur code was so big… but that may just b my tummy makin it look bigger than it actually is :wink:

Prophet.

Thanks very much, Prophet, but that doesn’t work either. I don’t know what I’m gonna do but I will try to find something. :wink:
Thanks again with your help.

sorry i couldnt be of more help… i must admit im not too familiar with key functions and listeners :blush: sorry!

Pophet.

PS if it makes any difference, that should work in theory!! :S lol