A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [RESOLVED] Key event assigned in class not working

  1. #1
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171

    resolved [RESOLVED] Key event assigned in class not working

    My script has a structure like this (shorten down version):

    MenuDisplay.as (creates the menu)
    PHP Code:
    package
    {
        
    import flash.display.Graphics;
        
    import flash.display.BitmapData;
        
    import Array;
        
    //
        
    import flash.display.Sprite;
        
    import flash.text.TextField;
        
    import flash.text.TextFormat;
        
    import flash.display.Graphics;
        
    import flash.display.Sprite;
        
    import flash.events.MouseEvent;
        
    import flash.events.KeyboardEvent;
        
        public class 
    MenuDisplay extends Sprite
        
    {
            public function 
    makeMenu () : void
            
    {
                
    addChild (menuAct);
                
    // make the menu here
            
    }
            
    // functions for creating the menu items here...
        
    }

    For making it easier to navigate the code I moved the functions for the buttons into a new class MenuActions.as:

    PHP Code:
    package
    {
        
    import flash.display.Graphics;
        
    import flash.display.BitmapData;
        
    import Array;
        
    //
        
    import flash.display.Sprite;
        
    import flash.text.TextField;
        
    import flash.text.TextFormat;
        
    import flash.display.Graphics;
        
    import flash.events.MouseEvent;
        
    import flash.events.KeyboardEvent;
        
    import flash.display.Sprite;
        
    //import flash.display.Stage;
        
    import flash.display.*;
        
    import flash.events.*;

        
        public class 
    MenuActions extends Sprite
        
    {
            var 
    backgroundNoise = new StaticNoise ();
            
    // Functions for menu items
            
    public function startProgram (MouseEvent) : void
            
    {
                
    backgroundNoise.CreateStaticNoise ();
                
    addChild (backgroundNoise);
                
    // Set focus to stage
                
    backgroundNoise.addEventListener (KeyboardEvent.KEY_DOWNbackgroundNoise.checkKey);
            }
        }

    The problem here is that the keys are not picked up. Earlier when I had the code in the MenuDisplay class, everything worked fine by doing:

    PHP Code:
                addChild (backgroundNoise);
                
    stage.addEventListener (KeyboardEvent.KEY_DOWNbackgroundNoise.checkKey);
                
    // Set focus to stage
                
    stage.focus stage
    but the stage is null, and if I try Stage.addEventListener... or Stage.focus, I receive an error.

    as an weekend coder, I'm not sure if I'm structuring my code right

  2. #2
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171
    Solved it by using:

    PHP Code:
                this.stage.addEventListener (KeyboardEvent.KEY_DOWNbackgroundNoise.checkKey);
                
    this.stage.focus this.stage
    Thanks to http://azerdark.wordpress.com/2011/0...ed-movie-clip/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center