Skip to main content
Participating Frequently
March 28, 2011
Answered

Holding Down a Key: Execute ONCE

  • March 28, 2011
  • 2 replies
  • 2030 views

I'm very lost on my scripting for holding a button down and having the action take place once.

So thing is, I have code for pressing a button (which is the up arrow button)

This was suppose to show the animation for someone shooting constantly until released:

    if (Key.isDown(Key.UP)){
        fox.gotoAndPlay(16);
        deploy.gotoAndPlay(2);
        trace("Pressed.");
    }

Unfortunetly the results are:

Holding down the up key places out "Pressed." constantly instead of once.

fox.gotoAndPlay(16) stays on 16 rather progressing the animation (to which should come back to 16 at a repeated process)

deploy.gotoAndPlay(2) plays a sound but before: the sound played constanlty rather once) recently: sound never plays

I also need code that prevents the player from moving when holding down the up arrow key.

So in total, I want the player to be able to hold the up key, and everything executed is played once until pressed agan after release.


I heard about adding listeners:

     var myListener:Object = new Object();
myListener.onKeyDown = function() {
    KEY = Key.getCode();
    if (!this[KEY]) {
        trace("You pressed "+KEY);
    }
    this[KEY] = true;
};
myListener.onKeyUp = function() {
    KEY = Key.getCode();
    trace("You released a key."+KEY);
    this[KEY] = false;
};
Key.addListener(myListener);

but I can't understand this technobabble and everytime I try to work it, it only puts error outputs.

Please help. This needs to be finshed in under 4 days.

This topic has been closed for replies.
Correct answer kglad

i don't see that error.  are you publishing for fp 6 or earlier??   in any case, KEY was from your code and is unneeded:

var upBool:Boolean;
var myListener:Object = new Object();
myListener.onKeyDown = function() {

    if (Key.getCode()== Key.UP && !upBool) {
        trace("You pressed "+KEY);
        upBool=true;
    }
};
myListener.onKeyUp = function() {

    if (Key.getCode() == Key.UP) {
        upBool=false
    }
};
Key.addListener(myListener)

2 replies

kglad
Community Expert
Community Expert
March 28, 2011

use:

var upBool:Boolean;
var myListener:Object = new Object();
myListener.onKeyDown = function() {
    KEY = Key.getCode();
    if (KEY == Key.UP && !upBool) {
        trace("You pressed "+KEY);
        upBool=true;
    }
};
myListener.onKeyUp = function() {
    KEY = Key.getCode();
    if (KEY == Key.UP) {
        trace("You released a key."+KEY);
        upBool=false
    }
};
Key.addListener(myListener);

Participating Frequently
March 29, 2011

The code given doesn't work.

The up arrow key has no function in testing and checking syntax in the actionscript code crashes flash altogether.

An error also occurs stating "Case-insensitive identifier 'KEY' will obscure built-in object 'Key'.

I don't know whether or not I need to modify the code or if I just replace the current code with the new set of code.

Either way, it didn't work.

Thanks though.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
March 29, 2011

i don't see that error.  are you publishing for fp 6 or earlier??   in any case, KEY was from your code and is unneeded:

var upBool:Boolean;
var myListener:Object = new Object();
myListener.onKeyDown = function() {

    if (Key.getCode()== Key.UP && !upBool) {
        trace("You pressed "+KEY);
        upBool=true;
    }
};
myListener.onKeyUp = function() {

    if (Key.getCode() == Key.UP) {
        upBool=false
    }
};
Key.addListener(myListener)

Inspiring
March 28, 2011

Simple solution, set a Boolean. If pressed once, it won't run twice.

var keyPressed:Boolean = false; if (Key.isDown(Key.UP)) {      if (!keyPressed)      {           fox.gotoAndPlay(16);           deploy.gotoAndPlay(2);           keyPressed = true;           race("Pressed.");      } }

Participating Frequently
March 29, 2011

Good news: it worked.

Bad news: can only press the up key once. I'd like for it to be pressed and held and to be done multiple occasions.

Thanks for the input though.