Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4342290
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:35:54+00:00 2026-05-21T11:35:54+00:00

I’ve got an as3 function that controls a movie clip with the keyboard: package

  • 0

I’ve got an as3 function that controls a movie clip with the keyboard:

package
{

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;

public class Main_Character_Two extends MovieClip
{
    var vx:int;
    var vy:int;

    public function Main_Character_Two()
    {
        init();
    }
    function init():void
    {
        //initialize variables
        vx = 0;
        vy = 0;

        //Add event listeners
        stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    function onKeyDown(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.LEFT)
        {
            vx = -5;
        }
        else if (event.keyCode == Keyboard.RIGHT)
        {
            vx = 5;
        }
        else if (event.keyCode == Keyboard.UP)
        {
            vy = -5;
        }
        else if (event.keyCode == Keyboard.DOWN)
        {
            vy = 5;
        }
    }
    function onKeyUp(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
        {
            vx = 0;
        }
        else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
        {
            vy = 0;
        }
    }
    function onEnterFrame(event:Event):void
    {
        //Move the player
        player.x += vx;
        player.y += vy;
    }
}

}

This works ok but the main problem is that when you press the right key (and hold it down) then press the left key, the character will move to the left but when you release the left key (with the right key still held down) the character just stops. How can I make it so the character starts moving to the right again in this situation (if Im still holding the right key after Ive released the left key)

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:35:55+00:00Added an answer on May 21, 2026 at 11:35 am

    You could put your ‘player’ positioning code into an Event.ENTER_FRAME loop and use KeyboardEvent.KEY_DOWN and KeyboardEvent.KEY_UP to set booleans and position the player in the loop like:

    var leftIsPressed:Boolean = false;
    var rightIsPressed:Boolean = false;
    var upIsPressed:Boolean = false;
    var downIsPressed:Boolean = false;
    var speed:Number = 5;
    var vx:Number = 0;
    var vy:Number = 0;
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
    stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    
    function keyDownHandler(e:KeyboardEvent):void {
        switch(e.keyCode) {
            case Keyboard.LEFT : leftIsPressed = true; break;
            case Keyboard.RIGHT : rightIsPressed = true; break;
            case Keyboard.UP : upIsPressed = true; break;
            case Keyboard.DOWN : downIsPressed = true; break;
        }
    }
    
    function keyUpHandler(e:KeyboardEvent):void {
        switch(e.keyCode) {
            case Keyboard.LEFT : leftIsPressed = false; break;
            case Keyboard.RIGHT : rightIsPressed = false; break;
            case Keyboard.UP : upIsPressed = false; break;
            case Keyboard.DOWN : downIsPressed = false; break;
        }
    }
    
    function enterFrameHandler(e:Event):void {
        vx = -int(leftIsPressed)*speed + int(rightIsPressed)*speed;
        vy = -int(upIsPressed)*speed + int(downIsPressed)*speed;
        player.x += vx;
        player.y += vy;
    }
    

    [EDIT] Added a more detailed example.

    Of course in this case, if both left and right keys are pressed, this would result in a 0 offset.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.