make flash detect a key press [SOLVED]

13 posts

Flag Post

Hi, i´m making a platform AS2 game and I´m trying to make a mario 3 type map like this

The problem is that file is written in AS3 and I´m translating it to AS2, and I need to make that flash listen that I press a Key without I press that Key.

I need that if I press DOWN, flash listen "press and release DOWN key, and then press and release LEFT key…It´s possible to make that?

 
Flag Post

Why not go all the way back in time and use punch cards?
So you only managed to find an As3 file that you cannot copy&paste, now you actually have to code it yourself.

register a listener here:
http://docs.brajeshwar.com/as2/Key.html

 
Flag Post

XD really funny…I really translated all code to AS2, but it doesn´t work perfect…the original code is in spanish, but I can to translate it to english for publish it here. I turned all night investigate the listeners but I haven´t any positive results…

 
Flag Post
Originally posted by chachon2:

XD really funny…I really translated all code to AS2, but it doesn´t work perfect…the original code is in spanish, but I can to translate it to english for publish it here. I turned all night investigate the listeners but I haven´t any positive results…

How is the code in “Spanish”? Spanish is not a programming language.

And there are some differences between the AS2 and AS3 event system, that’s why your code may not work. And if you already know AS3, why are you doing your game in AS2?

 
Flag Post

the problem is that I don´t know AS3. This is the code:


package 
{
   import flash.display.*;
   import flash.events.*;
   import flash.ui.Keyboard;
   import flash.utils.Timer;

   public class Mario extends MovieClip
   {

      private var enMovimiento:Boolean = false;
      private var temporizador:Timer = new Timer(10,0);
      private var teclaPulsada:uint;
      private var contendorPuntos:Sprite = new Sprite();

      private var contactoConCamino_Arriba:Boolean;
      private var contactoConCamino_Abajo:Boolean;
      private var contactoConCamino_Izquierda:Boolean;
      private var contactoConCamino_Derecha:Boolean;

      public function Mario()
      {
         lineas.visible = false;
         puntos.visible = false;
         stage.addEventListener(KeyboardEvent.KEY_DOWN,tecla);
         temporizador.addEventListener(TimerEvent.TIMER,mover);
      }
      private function tecla(e:KeyboardEvent):void
      {
         if (enMovimiento == false) {
            this.contactoConCamino_Arriba= false;
            this.contactoConCamino_Abajo= false;
            this.contactoConCamino_Izquierda= false;
            this.contactoConCamino_Derecha= false;

            teclaPulsada = e.keyCode;
            palparCaminos();
            temporizador.start();
            enMovimiento = true;
         }

      }
      private function mover(e:TimerEvent):void
      {

         var velocidad:uint = 2;

         if (teclaPulsada == 39 && this.contactoConCamino_Derecha == true) // 39 es RIGHT
         {
            mario.x += velocidad;
         }
         if (teclaPulsada == 37 && this.contactoConCamino_Izquierda== true) // 37 es LEFT
         {
            mario.x -= velocidad;
         }
         if (teclaPulsada == 38 && this.contactoConCamino_Arriba == true) // 39 es UP
         {
            mario.y -= velocidad;
         }
         if (teclaPulsada == 40 && this.contactoConCamino_Abajo== true) // 40 es DOWN
         {
            mario.y += velocidad;
         }

         for (var i:uint = 0; i <= puntos.numChildren-1; i++) {
            // En el mc llamado mario hay un MC llamado centro que sirve como detector..
            // ... de coliciones
            // Si centro coliciona con cualquier punto se detiene
            if (mario.centro.hitTestObject(puntos.getChildAt(i)) == true) {
               temporizador.stop();
               enMovimiento = false;
            }
         }
      }
      // Funcion que le indica a Mario, que caminos puede cojer
      // Mas tecnico. Dependiendo de que lineas tenga arriba, abajo, derecha o izquierda...
      // ...se podra mover en una direccion
      // En el MC llamado mario hay 4 MC que sirven como vigotes de un gato, para saber..
      // que lineas tiene cerca

      private function palparCaminos():void
      {
         for (var i:uint = 0; i <= lineas.numChildren-1; i++) {
            if (mario.arriba.hitTestObject(lineas.getChildAt(i)) == true) {
               this.contactoConCamino_Arriba= true;
            }
            if (mario.abajo.hitTestObject(lineas.getChildAt(i)) == true) {
               this.contactoConCamino_Abajo= true;
            }
            if (mario.izquierda.hitTestObject(lineas.getChildAt(i)) == true) {
               this.contactoConCamino_Izquierda= true;
            }
            if (mario.derecha.hitTestObject(lineas.getChildAt(i)) == true) {
               this.contactoConCamino_Derecha= true;
            }
         }
      }
   }
}
<pre/>
 
Flag Post
Originally posted by chachon2:

the problem is that I don´t know AS3. This is the code:

If you don’t know how to listen to key presses, you don’t know As2 either.
So why not start with As3 right away?

@code: a Timer will probably not run at 10ms interval.

 
Flag Post

Ok then. I don´t know AS2. I´m know a little bit of AS2 only. Can you through your shield very far and help me? I don´t now to solve the problem yet…and the last thing I do is to make a post to question. I´m googling every my doubts

 
Flag Post

I´m made this code, but it don´t works:


function the_key()
      {
	Key.isDown(37);
        trace("Left-> Code: " + Key.getCode());  
	  }
		  

var keylistener:Object = new Object();
		keylistener.onKeyDown = function()
		{
	   if (Key.isDown(Key.DOWN)) {
        the_key();
		trace("down, left");
	   }
   }

	Key.addListener(keylistener);

<pre/>
 
Flag Post
Originally posted by chachon2:

I´m made this code, but it don´t works:


function the_key()
      {
	Key.isDown(37);
        trace("Left-&gt; Code: " + Key.getCode());  
	  }
		  

var keylistener:Object = new Object();
		keylistener.onKeyDown = function()
		{
	   if (Key.isDown(Key.DOWN)) {
        the_key();
		trace("down, left");
	   }
   }

	Key.addListener(keylistener);

&lt;pre/&gt;

What are you trying to do? Because the code you posted has nothing to do with the AS3 code that you posted above.
If I’m not wrong (I’ve never coded in AS2), it’s supposed to run the “the_key” function when the user presses the DOWN Key. And that function traces the code of the Key pressed (Key.getCode()), that will be always 40 since the keycode of DOWN it’s 40.
And BTW, there’s a call to Key.isDown(37) that I don’t know what’s doing there, since it’s not assigned to anything.

 
Flag Post

With the code I made, the test file works, except for a thing.

If the character is in a vertical way, on top of that way, and you press down, the character go from the point 1 to the point 3. He don´t stops on point 2. But if (after press down) you press the left key or the right key, the character stop in the point 2.

I want to simulate that I too press the LEFT key after press DOWN key.
I hope I’ve made myself clear

 
Flag Post
Originally posted by chachon2:

With the code I made, the test file works, except for a thing.

If the character is in a vertical way, on top of that way, and you press down, the character go from the point 1 to the point 3. He don´t stops on point 2. But if (after press down) you press the left key or the right key, the character stop in the point 2.

I want to simulate that I too press the LEFT key after press DOWN key.
I hope I’ve made myself clear

Mmmmhhh… You’ve not made yourself very clear (you can try to explain me that in Spanish by private message, if you feel more comfortable), but I think that it doesn’t work beacuse you are only registering when a key is pressed, and not when it is lift. Then your character doesn’t stop because it is still registering that the down key is pressed unless you press another key.

And BTW, Key.isDown() doesn’t “simulate” that you are pressing a key, it only checks if the key with the keycode passed between the parenthesis is currently being pressed.

 
Flag Post

I solve more or less the problem.

File map type mario 3 in AS2

 
Flag Post
Originally posted by chachon2:

the problem is that I don´t know AS3. This is the code:

Where did you get this exactly? Was this all in one file or were there others that go with it?

Its not in spanish. Its code that’s got variables and comments that are IN spanish. That does not mean that the code is crap. Use babelFish or google translations if you need to figure this out.