4
\$\begingroup\$

I finally managed to get the code to work properly. It makes the player object move faster if the user double taps the arrow key with a few 'ticks' (new frames) after the 1st stroke. - User presses and has a limited time to press again to sprint the object. If the time runs out the user has to try again.

However, It'd be better I believe to be working with time, miliseconds and not 'ticks', as what I intend involves lots of key combinations that have x seconds to be pressed.

I would like to know the redundancies in the code, what can be made into a class and something better then screen refresh to time things up (delays?).

package 
{

import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;

public class Main extends Sprite {
var keys:Array = [];
var player:Sprite = new Sprite();   
var sprint, condition1, condition2, timerStart, keyUp, keyDown:Boolean;
var timer:int;

public function Main():void {

player.graphics.beginFill(0x000000);
player.graphics.drawCircle(0, 0, 25);
player.graphics.endFill;

addChild(player);
player.x = 100;
player.y = 250;

player.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}

function update(e:Event):void {

  if ((timerStart)&&(timer>0)) {
    timer --;
    condition1 = true;
  }

  if (keyUp) {
    if ((condition1) && (timer > 0)) {
       condition2 = true;
    }   

    if (sprint) {
      timerStart = false;
      condition1 = false;
      condition2 = false;
      sprint = false;
      timer = 7;
    }

    if (timer <= 0) {
      timerStart = false;
      condition1 = false;
      condition2 = false;
      sprint = false;
      timer = 7;
    }
}


if (keys[Keyboard.RIGHT]) {
timerStart = true;

if ((condition1) && (condition2)) {
sprint = true;
}

if (sprint) {
player.x += 7;
} else player.x += 2;
            }

function onKeyDown(e:KeyboardEvent):void {
keys[e.keyCode] = true;
keyUp = false;
keyDown = true;
}

function onKeyUp(e:KeyboardEvent):void {
keys[e.keyCode] = false;
keyUp = true;
keyDown = false;
}

} // class
} // package
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

From a once over

  • Indentation is terrible, consider using a tool like jsbeautifier.org, it works for ActionScript as well
  • Keep working with Event.ENTER_FRAME <- It is the better approach in my mind
  • Always be on the look out for copy pasted code, this:

    if (sprint) {
      timerStart = false;
      condition1 = false;
      condition2 = false;
      sprint = false;
      timer = 7;
    }
    
    if (timer <= 0) {
      timerStart = false;
      condition1 = false;
      condition2 = false;
      sprint = false;
      timer = 7;
    }
    

could be

    if (sprint || timer <= 0) {
      timerStart = false;
      condition1 = false;
      condition2 = false;
      sprint = false;
      timer = 7;
    }
  • Do not track keyDown, you do not use it, and it's value is always the opposite of keyUp
  • condition1 and condition2 are very unfortunate names, you should try to be more descriptive
  • This code could use some well named constants and indenting:

    if (sprint) {
    player.x += 7;
    } else player.x += 2;
    

    I would rather see

    var FAST_SPEED = 7,
        NORMAL_SPEED = 2;
    if (isSprinting) {
        player.x += FAST_SPEED;
    } else {
        player.x += NORMAL_SPEED;
    }
    

    or if I wanted to be fancy

    player.x += isSprinting ? FAST_SPEED : NORMAL_SPEED;
    
\$\endgroup\$
2
  • \$\begingroup\$ Ok, how do I time things if needed? 0,5 secs instead of the value 7 held by var timer for an example :P \$\endgroup\$
    – RAO
    Apr 21, 2014 at 20:36
  • 2
    \$\begingroup\$ That would be an SO question :\ \$\endgroup\$
    – konijn
    Apr 21, 2014 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.