Question

I'm making a top-down RPG style game, very much a throwback to zelda. This is my code for the movement controls based on keyboard controls. The movement itself is pretty solid, no stutter-key syndrome, all animations are triggered correctly.

However, when the attack button (SPACE in this case) is pressed it triggers the appropriate frame but when the key is released the corresponding frame is still visible, until another direction key is pressed. So, when you attack it looks like he keeps his sword extended until another movement is made, and so on. This should only happen when the key is held down not when simply pressed. This is not the expected result. The result I'm looking for is an attack that triggers every time the key is pressed, creating the normal buttondown=attacking, buttonup = not attacking.

The attacks and movement now are based on a variable moving:int = 4; That way I can switch the attack position to the corresponding movement direction.

Any ideas on how to correct this issue?

import flash.events.Event;
import flash.events.KeyboardEvent; 

character.stop(); 

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);// when key is pressed
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);//when key is released
stage.addEventListener(Event.ENTER_FRAME, MainLoop);

var moving:int = 4;
var movingDown:Boolean = false;
var movingUp:Boolean = false;
var movingRight:Boolean = false;
var movingLeft:Boolean = false;


function onKeyPress(e:KeyboardEvent):void
{ 
   switch (e.keyCode) 
     { 
        case Keyboard.DOWN : movingDown = true; moving = 1; break;
        case Keyboard.UP : movingUp = true; moving = 2; break; 
        case Keyboard.RIGHT : movingRight = true; moving = 3; break; 
        case Keyboard.LEFT : movingLeft = true; moving = 4; break;
        case Keyboard.SPACE : handleAttack();  break;
      }           
 } 

 function onKeyRelease(e:KeyboardEvent):void
 {
    switch (e.keyCode)  
  { 
     case Keyboard.DOWN: character.gotoAndStop(1); movingDown=false;  moving = 1; break;
     case Keyboard.UP: character.gotoAndStop(3);movingUp=false; moving = 2; break;
     case Keyboard.LEFT: character.gotoAndStop(5);movingLeft=false;  moving = 3; break;
     case Keyboard.RIGHT: character.gotoAndStop(7);movingRight=false;  moving = 4; break;   
     case Keyboard.SPACE: handleAttack(); break;      
   }       
} 

 function handleAttack():void
 {          
    switch (moving)
      {
        case 1: character.gotoAndStop(9); movingDown = false;   break;      //down
        case 2: character.gotoAndStop(10); movingUp = false;  break;        //up
        case 3: character.gotoAndStop(11); movingLeft = false;  break;      //left  attacks
        case 4: character.gotoAndStop(12); movingRight = false;  break;     //right
      }
}    

function MainLoop(e:Event):void
{

 switch (true) 
   {
     case movingDown: character.gotoAndStop(2); character.y+=4.5; break; 
     case movingUp: character.gotoAndStop(4); character.y-=4.5; break;
     case movingLeft: character.gotoAndStop(6); character.x-=4.5; break;
     case movingRight: character.gotoAndStop(8); character.x+=4.5; break; 
   } 

}
Was it helpful?

Solution

I am kinda new myself but i think it's because both onKeyPress() and onKeyRelease() point to the same function handleAttack() and as such point to the same frame even when you release. Try making two seperate attack functions for each one.

e.g attackPressed() for onKeyPress()

function attackPressed():void
{          
    switch (moving)
    {
        case 1: character.gotoAndStop(9); movingDown = false;   break;      
        case 2: character.gotoAndStop(10); movingUp = false;  break;       
        case 3: character.gotoAndStop(11); movingLeft = false;  break;     
        case 4: character.gotoAndStop(12); movingRight = false;  break;     
    }
}

and attackRelease() for onKeyRelease()

function attackRelease():void
{
    switch (moving)
    {
        case 1:character.gotoAndStop(1); movingDown = false;  break;
        case 2:character.gotoAndStop(3); movingUp = false; break; 
        case 3: character.gotoAndStop(5); movingLeft = false; break; 
        case 4: character.gotoAndStop(7); movingRight = false; break;
    }
 }

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top