Monday, May 20, 2013

Getting Flash to Talk to UDK - part 3


Capturing Keys -


This is a multi-part set up. You'll be setting up the code in Actionscript, as well as in Unreal Script later. Planning ahead here will save a ton of heartache later. Now any key you capture, can easily call a function back in unreal, if you need it for the purposes exec style functions (where the key press activates the function).

First set up the ability to capture keys. Near the top of your code, outside of any function, put in the following:


var keyboardInput:Object = new Object(); //holds keyboard input
Key.addListener(keyboardInput); //used to listen for key input.


For right now, we will focus on what needs to be done in actionscript. Here is an example from my code:


// when a key is pressed, execute this function.
keyboardInput.onKeyDown = function() 
{

    // store the keyboard input ASCII code inside 'keyPressed'.
    var keyPressed:Number = Key.getCode();


    // if TAB was pressed...
    if (keyPressed == 9 && menuLoc == "GameActive") 
    { 
        
        if (invshown == false && bPause == false)
{
    ShowInventory();
    trace("Inventory Shown");
}
else if (invshown == true && bPause == false)
{
         HideInventory();
    trace("Inventory Hidden");
        }
    }


}

This simply checks what state the game is in, and calls the correct function. But focus on the set up, and not the execution.

keyboardInput.onKeyDown = function() 
{
    // store the keyboard input ASCII code inside 'keyPressed'.
    var keyPressed:Number = Key.getCode();


All the key press events will occur after this and before the closing curly brace. As in the example, the Tab key is in there, so that right after:


HideInventory();
trace("Inventory Hidden");
}
    }

you would have

HideInventory();
trace("Inventory Hidden");
}
    }
    // if Escape was pressed...
    
    if (keyPressed == 27 && bPause == false && menuLoc == "GameActive") 
    {         

Now you'll notice that the keyPressed variable is compared to a number. These numbers can be found online, by doing a search for javascript keycodes.


So you would at the same time be keeping track of what keys you need, so that later on in unrealscript, you can capture those keys for flash.

That's pretty much it for capturing keys in actionscript. The other step in UDK will be done later on when we are working in UDK.

No comments:

Post a Comment