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 8082641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:10:59+00:00 2026-06-05T17:10:59+00:00

I have three classes that I am using and have shortened for ease of

  • 0

I have three classes that I am using and have shortened for ease of reading, leaving out the package and imports. MainClass is my document class, Player is a class linked to a movieclip, and KeyHandler is a class i’m using to contain the functions for my key presses. First, the code..

public class  TestMain extends Sprite
{
    public var keyHandler:KeyHandler = new KeyHandler();
    public var timer:Timer = new Timer(30);
    public var player:Player = new Player();

    public function TestMain()
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler.KeyPress);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler.KeyRelease);
        timer.addEventListener(TimerEvent.TIMER, onTick);
        timer.start();

        stage.addChild(player);
    }

    public function onTick(timerEvent:TimerEvent)
    {
        player.Move();
    }
}

Player Class..

public class Player extends Sprite
{
    public var keyHandler:KeyHandler = new KeyHandler();

    public function Player()
    {
    }

    public function Move():void
    {
        trace("this works");
        if (keyHandler.upKeyIsPressed)
        {
            trace("this doesnt work")
        }
    }

}

KeyHandler class..

public class KeyHandler extends Sprite
{
    public var upKeyIsPressed:Boolean = false;

    public function KeyHandler() 
    {
    }

    public function KeyPress(keyboardEvent:KeyboardEvent)
    {
        if (keyboardEvent.keyCode == Keyboard.UP)
        {
            upKeyIsPressed = true;
        }
    }

    public function KeyRelease(keyboardEvent:KeyboardEvent)
    {
        if (keyboardEvent.keyCode == Keyboard.UP)
        {
            upKeyIsPressed = false;
        }
    }

}

Holding down the up key doesn’t trace anything, but the Move function does. Should I make KeyHandler static, or is there an easy fix? Is this bad practice to do?

  • 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-06-05T17:11:01+00:00Added an answer on June 5, 2026 at 5:11 pm

    This is option 1 from my comment

    TestMain

    public class  TestMain extends Sprite
    {
        public var keyHandler:KeyHandler = new KeyHandler();
        public var timer:Timer = new Timer(30);
        public var player:Player = new Player();
    
        public function TestMain()
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler.KeyPress);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler.KeyRelease);
            timer.addEventListener(TimerEvent.TIMER, onTick);
            timer.start();
            player.keyHandler = keyHandler;
            stage.addChild(player);
        }
    
        public function onTick(timerEvent:TimerEvent)
        {
            player.Move();
        }
    }
    

    Player

    public class Player extends Sprite
    {
        public var keyHandler:KeyHandler;
    
        public function Player()
        {
        }
    
        public function Move():void
        {
            trace("this works");
            if (keyHandler.upKeyIsPressed)
            {
                trace("this should be working now")
            }
        }
    
    }
    

    KeyHandler

    public class KeyHandler extends Sprite
    {
        public var upKeyIsPressed:Boolean = false;
    
        public function KeyHandler() 
        {
        }
    
        public function KeyPress(keyboardEvent:KeyboardEvent)
        {
            if (keyboardEvent.keyCode == Keyboard.UP)
            {
                upKeyIsPressed = true;
            }
        }
    
        public function KeyRelease(keyboardEvent:KeyboardEvent)
        {
            if (keyboardEvent.keyCode == Keyboard.UP)
            {
                upKeyIsPressed = false;
            }
        }
    
    }
    

    Option 2 is making a “Singleton” type of setup where you get an instance of the class through a static method. Only one instance of the class is used throughout in this case and doesn’t require passing the instance through objects that don’t actually need a handle on it themselves. In the link in my comment on the OP I there’s discussion on options for enforcing the pattern so no new instances are accidentally made, I’m just using the simple option here that causes a runtime error:

    public class KeyHandler extends Sprite
    {
        public var upKeyIsPressed:Boolean = false;
        public static var SINGLETON:KeyHandler = new KeyHandler();
        public static function getInstance():KeyHandler
        {
            return SINGLETON;
        }
    
        public function KeyHandler() 
        {
            if(SINGLETON)
                throw new Error("There can be only one!!!");
        }
    
        public function KeyPress(keyboardEvent:KeyboardEvent)
        {
            if (keyboardEvent.keyCode == Keyboard.UP)
            {
                upKeyIsPressed = true;
            }
        }
    
        public function KeyRelease(keyboardEvent:KeyboardEvent)
        {
            if (keyboardEvent.keyCode == Keyboard.UP)
            {
                upKeyIsPressed = false;
            }
        }
    
    }
    

    Player

    public class Player extends Sprite
    {
        public var keyHandler:KeyHandler = KeyHandler.getInstance();
    
        public function Player()
        {
        }
    
        public function Move():void
        {
            trace("this works");
            if (keyHandler.upKeyIsPressed)
            {
                trace("this should be working now")
            }
        }
    
    }
    

    TestMain

    public class  TestMain extends Sprite
    {
        public var keyHandler:KeyHandler = KeyHandler.getInstance();
        public var timer:Timer = new Timer(30);
        public var player:Player = new Player();
    
        public function TestMain()
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler.KeyPress);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler.KeyRelease);
            timer.addEventListener(TimerEvent.TIMER, onTick);
            timer.start();
            player.keyHandler = keyHandler;
            stage.addChild(player);
        }
    
        public function onTick(timerEvent:TimerEvent)
        {
            player.Move();
        }
    }
    

    Here’s another way to enforce the Singleton that will cause compile time errors so you see the problem earlier on:

    http://blog.pixelbreaker.com/actionscript-3-0/as30-better-singletons

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

Sidebar

Related Questions

Using threads, I have a principal class ( SlaveCrawler ) that instantiates three classes(
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have Three different classes that all need to use instances of one class.
I have three classes mapped using the table-per-subclass class mapping strategy. The tables are:
I have three classes that all have a static function called 'create'. I would
I have three classes; Classes A and B both reference class C . How
I have three classes called Broker , Instrument and BrokerInstrument . using Iesi.Collections.Generic; public
I have a custom Image class that I am using to store individual image
I have been writing several class templates that contain nested iterator classes, for which
I have three classes that pose a problem when trying to add a new

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.