Search code examples
actionscript-3flashkeyboard-eventsadobe-animate

Actionscript 3 equivalent of KeyboardEvent.Repeat


I'm very much a beginner when it comes to programming; I'm doing a flash game for my 2D Game class on Adobe Animator, and I had a doubt about the code that my instructor couldn't answer himself. Long story short, I want the character speed to increase based on how long the key has been pressed, so I came up with this:

spdX = spdX + aclMov*(tempo) - aclFrc*(tempo);

Where the variable tempo would increase as long as the key is being held down, and I would check if it is with KeyboardEvent.repeat, as in:

if(heldDown){while(heldDown){tempo += 1}}
else{tempo = 0}

spdX = spdX + aclMov*(tempo) - aclFrc*(tempo);

However when I try to do that, the output responds with "Property repeat not found on flash.events.KeyboardEvent and there is no default value". I assume that this is because KeyboardEvent.repeat is not defined in the medium I'm using. Is there anyway I can reproduce the same effect of KeyboardEvent.repeat, perhaps by creating a function that mimics what it would have done?

Thanks in advance.

(Edit 1)

I begin apologizing for my shortness of clarification, as well as my ignorance in the topic as I am barely a beginner when it comes to as3, and am not properly presented yet to many of the terms I've read.

So, thanks to the meaningful contributions of comments, I already have been given a glimpse of what kind of workaround I would need to do to substitute KeyboardEvent.repeat. There are other parts of the code of relevance to the problem, as well:

stage.addEventListener(KeyboardEvent.KEY_DOWN,pressKey)

function pressKey (Event){
(...)
if(Event.keyCode == (Keyboard.A)) {left = true;}
(...)
}

stage.addEventListener(KeyboardEvent.KEY_UP,releaseKey)

function releaseKey (Event){
(...)
if(Event.keyCode == (Keyboard.A)) {left = false;}
(...)
}

This is how the code was intended to go. It was suggested that I use the getTimer() method to record the moment the event KEY_DOWN happens, stopping when the KEY_UP comes into effect. Problem is, how can I increment the code to make it differentiate between those two events, and more specifically, how can I adapt the ENTER_FRAME event so that differentiating between them still works with it? Here's the most relevant parts of it, by the way:

addEventListener(Event.ENTER_FRAME,walk);

function walk(Event) {
    if(left) { 
            (...)
            char.x -= spdX;
            (...)
            }

I assume that the code worked up till now because, as the state of "left" constantly switched between "true" and "false", the if conditional was met repeatedly, leading the character to move. However, if I try to make it so that the conditional depends on "left" staying "true" for a certain time, the code becomes incompatible with itself.

In short, it brings the question of how to adapt the "KEY_[]" event listeners and the "walk" function to work, in using the getTimer() method, to work together.

Again, thanks in advance.


Solution

  • I believe that would be somehow self-explanatory .

    stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
    addEventListener(Event.ENTER_FRAME, onFrame);
    
    var pressedKeyA:int;
    
    // Never name variables and arguments with the same name
    // as existing classes, however convenient that might seem.
    function onDown(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.A)
        {
            // Let's record the moment it was pressed.
            pressedKeyA = getTimer();
        }
    }
    
    function onUp(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.A)
        {
            // Mark the key as not pressed.
            pressedKeyA = 0;
        }
    }
    
    function onFrame(e:Event):void
    {
        // It is true if it is not 0.
        if (pressedKeyA)
        {
            // That's how long it has been pressed. In milliseconds.
            var pressedFor:int = getTimer() - pressedKeyA;
    
            // Change the position with regard to it.
            // For example 1 base + 1 per second.
            char.x -= 1 + pressedFor / 1000;
        }
    }