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

  • Home
  • SEARCH
  • 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 4567178
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:53:37+00:00 2026-05-21T18:53:37+00:00

So I’m a little new to Actionscript 3 and if I’m missing something crucial

  • 0

So I’m a little new to Actionscript 3 and if I’m missing something crucial to solving just tell me and I will post it, but anyways…

So I have 2 layers. The top layer is content and the bottom layer is as3 (for actionscript). In my content I have a little blue ball that is approx. in the center of the stage. And in my as3 layer I have the following code:

//Add the event listeners...
stage.addEventListener(Event.ENTER_FRAME, moveBall);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keypress);
stage.addEventListener(KeyboardEvent.KEY_UP, keyrelease);

//Movement variables...
var velY;
var velX;
var power = 5;
var friction = 0.95;

//Key variables...
var right;
var left;
var up;
var down;

function keyrelease(event:KeyboardEvent) {
    right = false;
    left = false;
    down = false;
    up = false;
}

function keypress(event:KeyboardEvent) {
    if (event.keyCode == 39) {
        right = true;
    }
    if (event.keyCode == 37) {
        left = true;
    }
    if (event.keyCode == 38) {
        up = true;
    }
    if (event.keyCode == 40) {
        down = true;
    }
}

function moveBall(event:Event) {
    if (right == true) {
        velX += power;
    }
    if (left == true) {
        velX -= power;
    }
    if (up == true) {
        velY += power;
    }
    if (down == true) {
        velY -= power;
    }

    character.x += velX;
    character.y += velY;


    velY *= friction;
    velX *= friction;
}

Where basically what I am doing is check if a key is pressed and if so I make velY or velX equal powe that will increment the little blue ball a certain way and velY and velX will keep decreasing until (because of rounding errors) it becomes zero and the blue ball stops. But, nothing is working with the keys, and for some reason my little blue circle is in the upper left most corner of the screen.

  • 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-21T18:53:38+00:00Added an answer on May 21, 2026 at 6:53 pm

    That is because you don’t initialize the velocity variables. The are untyped, thus their initial value is undefined.

    In these lines they are automatically promoted to Number

    character.x += velX;
    character.y += velY;
    

    When coercing undefined to Number it becomes NaN (not a number). Thus the resulting coordinates are NaN. FlashPlayer responds to this by putting the DisplayObject to 0.

    Also, I’d suggest using flash.geom.Point (good to manipulate coordinates), flash.ui.Keyboard (is far more readable) and flash.utils.Dictionary (rather than having many flags and ifs, build lookup maps).

    //Add the event listeners...
    stage.addEventListener(Event.ENTER_FRAME, moveBall);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keypress);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyrelease);
    
    //Movement variables...
    const power:Number = 5;
    const friction:Number = 0.95;
    var velocity:Point = new Point();
    
    var map:Dictionary = new Dictionary();
    map[Keyboard.LEFT] = new Point(-power, 0);
    map[Keyboard.UP] = new Point(0, -power);
    map[Keyboard.RIGHT] = new Point(power, 0);
    map[Keyboard.DOWN] = new Point(0, -power);
    
    var acceleration:Dictionary = new Dictionary();
    
    function keyrelease(event:KeyboardEvent):void {
        delete acceleration[map[event.keyCode]];
    }
    
    function keypress(event:KeyboardEvent):void {
        var p:Point = map[event.keyCode];
        if (p) acceleration[p] = p;
    }
    
    function moveBall(event:Event):void {
        for each (var p:Point in acceleration) velocity = velocity.add(p);
        character.x += velocity.x;
        character.y += velosity.y;
        velocity.normalize(velocity.length * friction);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.