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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:23:26+00:00 2026-05-18T22:23:26+00:00

I am relatively new to Action Script, and I am trying to make a

  • 0

I am relatively new to Action Script, and I am trying to make a game of Snake. Obviously I need to implement a global key listener, but I am having odd problems. I tried adding the listener to the application tag but it didn’t seem to have any effect (the movie was still able to compile). Whenever I call

this.stage.addEventListener(KeyboardEvent.KEY_DOWN, key, true);

my program crashes. Below is the content of my main.mxml file:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
 xmlns:mx="http://www.adobe.com/2006/mxml" 
 layout="absolute"
 width="800" 
 height="600"
 frameRate="15"
 creationComplete="creationComplete();"
 enterFrame="enterFrame(event);"
 currentState="MainMenu">

 <mx:states>
  <mx:State 
   name="Game"   
   enterState="enterGame(event)"
         exitState="exitGame(event)">
  </mx:State>
  <mx:State 
   name="LevelEnd">
   <mx:AddChild relativeTo="{myCanvas}" position="lastChild">
    <mx:Button x="380" y="344" label="Continue" id="btnContinue" click="btnContinueClicked(event)" width="90" height="30"/>
   </mx:AddChild>
   <mx:AddChild relativeTo="{myCanvas}" position="lastChild">
    <mx:Label x="10" y="10" text="Congratulations, you finished the level."/>
   </mx:AddChild>
  </mx:State>
  <mx:State name="MainMenu">
   <mx:AddChild relativeTo="{myCanvas}" position="lastChild">
    <mx:Button x="381" y="344" label="Start" id="btnStart" click="startGameClicked(event)" width="90" height="30"/>
   </mx:AddChild>
   <mx:AddChild relativeTo="{myCanvas}" position="lastChild">
    <mx:Image x="10" y="49" source="@Embed('../media/mainmenu.png')"/>
   </mx:AddChild>
   <mx:AddChild relativeTo="{myCanvas}" position="lastChild">
    <mx:Label x="10" y="10" text="Snake Pro" fontSize="20" fontWeight="bold"/>
   </mx:AddChild>
  </mx:State>
 </mx:states>

 <mx:Canvas x="0" y="0" width="100%" height="100%" id="myCanvas"/>

 <mx:Script>
 <![CDATA[   

  protected var inGame:Boolean = false;
  protected var currentLevel:int = 1;
  import flash.events.KeyboardEvent; 

  public function creationComplete():void
     {
   LevelDefinitions.Instance.startup();
   addKeyEvent();
   //stage.focus = stage;
     }

  private function addKeyEvent():void
  {
   this.stage.addEventListener(KeyboardEvent.KEY_DOWN, key, true);
  }

     public function enterFrame(event:Event):void
     {
      if (inGame)
      {
       GameObjectManager.Instance.enterFrame();

       myCanvas.graphics.clear();
       myCanvas.graphics.beginBitmapFill(GameObjectManager.Instance.backBuffer, null, false, false);
       myCanvas.graphics.drawRect(0, 0, this.width, this.height);
       myCanvas.graphics.endFill();
      } 
     }

        private function key(event:KeyboardEvent):void {
            //t1.text = event.keyCode + "/" + event.charCode;

   GameObjectManager.Instance.setDirection(0, 1);
   currentState = "MainMenu";
   inGame = false;
        }

     protected function startGameClicked(event:Event):void
  {
   currentState = "Game"
  }     

     protected function enterGame(event:Event):void
  {
   Mouse.hide();
   GameObjectManager.Instance.startup();
   Level.Instance.startup(currentLevel);
   inGame = true;
     }

     protected function exitGame(event:Event):void
  {
   Mouse.show();
   Level.Instance.shutdown();
   GameObjectManager.Instance.shutdown();
   inGame = false;
  }  

  protected function btnContinueClicked(event:Event):void
  {
   currentLevel = LevelDefinitions.Instance.getNextLevelID(currentLevel);
   if (currentLevel == 0)
   {
    currentLevel = 1;
    currentState = "MainMenu";
   }
   else
   {
    currentState = "Game"
   }
     }
 ]]>
    </mx:Script>

</mx:Application>

Also, it seems I am getting this stack trace:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at main/addKeyEvent()[C:\Users\Me\Desktop\Flash\Snake\src\main.mxml:58]
    at main/creationComplete()[C:\Users\Me\Desktop\Flash\Snake\src\main.mxml:52]
    at main/___main_Application1_creationComplete()[C:\Users\Me\Desktop\Flash\Snake\src\main.mxml:10]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:12528]
    at mx.core::UIComponent/set initialized()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:1627]
    at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:759]
    at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.x\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1072]

I am at my wit’s end here, and I appreciate your time and efforts. 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-18T22:23:26+00:00Added an answer on May 18, 2026 at 10:23 pm

    You are getting a runtime error because you attach an event listener to the “stage” property, which is null at the time you try it. Instead of doing this on the “creationComplete” event, try to do this on the “applicationComplete” event. The stage object will be available then.

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

Sidebar

Related Questions

I'm relatively new javascript, but I am comfortable in other languages. I'm trying to
Relatively new to WinForms but I'm working on a small business app. Anyway, where
Im relatively new to the Android world but I have a quick question. I
I'm relatively new to Java and I'm still trying to understand the fundamentals. I
I am relatively new to Android, but I am looking to use an interactive
I am relatively new to this, but I'm working through things. I want to
I'm relatively new with MVC3, but I'm using it, C# and EF4 to create
I am relatively new to flash. I am trying to create a square grid
I'm relatively new to unit testing, and trying it on my personal projects. I
I am relatively new to game development so I decided I wanted to create

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.