Search code examples
actionscript-3flashdevelop

Totally non-responsive to keyboard presses


This doesn't respond to pressing Enter -traces give no results.

Is there any way to do this that works?

This is just extra ridiculous writing to get past the ludicrous posting restrictions.

Yaddah.

boards.as

  package 
  {
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.ui.Keyboard;
    import mx.core.BitmapAsset;
    //import board;
    import flash.accessibility.AccessibilityImplementation;
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.display.Sprite;
    import flash.utils.ByteArray;
    import flash.events.MouseEvent;
    import flash.text.AntiAliasType; 
    import flash.utils.describeType;
    import flash.net.*;
    import Set;
    import StatusBox;
    import Statusx;
    import flash.display.InteractiveObject;
    import flash.text.TextFieldType;
    import flash.events.FocusEvent;
    import fl.managers.FocusManager;
    import flash.display.*;
    import flash.display.Stage;
    import flash.events.KeyboardEvent;

    public class boxsprite extends Sprite  
    {     
        [Embed(source = "C:/Windows/Fonts/Verdana.ttf", fontName = "Verdana", fontWeight = "bold", advancedAntiAliasing = "true", mimeType = "application/x-font")] 
        public static const VERD:Class;
        [Embed(source="../lib/box.gif")]
         private var boxspriteClass:Class
         [Embed(source = "../lib/m2.gif")]
         private var m2:Class

         [Embed(source = "../lib/m3.gif")]
        private var m3:Class
         [Embed(source="../lib/m4.gif")]
        private var m4:Class
        [Embed(source = "../lib/m5.gif")]
        private var m5:Class
        [Embed(source = "../lib/m6.gif")]
        private var m6:Class

        [Embed(source = "../lib/m7.gif")]
        private var m7: Class
        [Embed(source="../lib/m8.gif")]
        private var m8: Class
        [Embed(source = "../lib/m9.gif")]
        private var m9: Class

        private var m22:Bitmap;
        private var m33:Bitmap;
        private var m44:Bitmap;
        private var m55:Bitmap;
        private var m66:Bitmap;
        private var m77:Bitmap;
        private var m88:Bitmap;
        private var m99:Bitmap;
        private var boxsprite2:Bitmap;

         internal var mode:uint=2;
            internal var displaytext:String;
            internal var setBox:Boolean = false;
            internal var onBoard:Array = [0];
            internal var playerRound:uint = 1;
            internal var round:uint = 1;
            internal var playernumber:uint; 
            internal var myTextBox:TextField = new TextField();
            public function boxsprite():void
            {
                trace (mode);

            boxsprite2=new boxspriteClass() as Bitmap;
            this.addChild(boxsprite2); 
            m22 = new m2 as Bitmap;
            this.addChild(m22);

            m77= new m7 as Bitmap;

            this.addChild(m77);

            m66= new m6 as Bitmap;
            this.addChild(m66);

            m55= new m5 as Bitmap;
            this.addChild(m55);
            m44= new m4 as Bitmap;
            this.addChild(m44);
            m33= new m3 as Bitmap;
            this.addChild(m33);
            this.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown2);
            }

            private function myKeyDown2(e2:KeyboardEvent):void
                {
                if (e2.keyCode == Keyboard.ENTER) 
                {
                trace("Enter pressed");
                    modeswitch();

                }


            else if (e2.keyCode ==Keyboard.H)
            {

            navigateToURL(new URLRequest("http://wp.me/P3FUQl-n"));
            }
            //else if (e.Keycode == Keyboard.Q)
            function modeswitch():void
            {  trace(mode);
                switch(mode)
                {
                case 8: 

                {mode = 9;
                choosetext(); }

                case 9: 
                    {mode = 2;
                choosetext();
                }
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                {mode = 3; 
                choosetext();}

startBox.addEventListener(KeyboardEvent.KEY_DOWN, kill);

This code part in

Board.as- appears to work.

function kill (e2:KeyboardEvent):void

                {if (e2.keyCode == Keyboard.NUMBER_1)
                   {removeChild(this.startBox);
                   startBox = null;
                   }
                 else if (e2.keyCode == Keyboard.NUMBER_2)
                  {removeChild(this.startBox);
                   startBox = null;
                  }

                  else if (e2.keyCode == Keyboard.NUMBER_3)
                  {removeChild(this.startBox);
                   startBox = null;
                  }
                  else if (e2.keyCode == Keyboard.NUMBER_4)
                  {removeChild(this.startBox);
                   startBox = null;
                  }
                  else if (e2.keyCode == Keyboard.NUMBER_5)
                  {removeChild(startBox);
                   startBox = null; }
                  else if (e2.keyCode == Keyboard.NUMBER_6)

                { removeChild(this.board.startBox);
                   startBox = null;}
                 else if (e2.keyCode == Keyboard.ENTER)
                {removeChild(startBox);
                 startBox = null;}

Maybe I've give up on keyboard for this part and use mouse events instead- in a previous structure I couldn't have done that as mouse was already occupied doing something but that should be freed up now.

By the way this isn't need on stage initialisation.


Solution

  • Your sprite will never receive any KeyboardEvent. You should add your event listeners directly to the stage. Of course, you have to obtain a link to the stage.

    In your constructor delete

     this.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown2);
    

    And add the following:

        //Your constructor
    ...
    if(stage)
        initKeyboardEvents();
    else
        addEventListener(Event.ADDED_TO_STAGE, initKeyboardEvents)
    //End of constructor
    ...
    //New function
    private function initKeyboardEvents(e:Event = null):void
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown2);
    }