{"id":2810,"date":"2013-06-28T16:37:55","date_gmt":"2013-06-28T23:37:55","guid":{"rendered":"http:\/\/www.nathalielawhead.com\/candybox\/?p=2810"},"modified":"2013-06-28T16:43:21","modified_gmt":"2013-06-28T23:43:21","slug":"source-code-snippet-making-a-custom-customize-keyboard-shortcuts-feature-for-a-game-actionscript","status":"publish","type":"post","link":"http:\/\/www.nathalielawhead.com\/candybox\/source-code-snippet-making-a-custom-customize-keyboard-shortcuts-feature-for-a-game-actionscript","title":{"rendered":"Source Code Snippet: Making A Custom Customize Keyboard Shortcuts Feature &#8211; For A Game (ActionScript)\u2026"},"content":{"rendered":"<p>This is a simple example that I&#8217;m throwing up here because it may help some googler looking for a quick solution (just strip out the <code>trace<\/code> and <code>txt_output<\/code>).<br \/>\nThis lets you set an alphanumeric (letters and numbers) character as a desired &#8220;keyboard shortcut&#8221; which is saved for use throughout your game (or application).<\/p>\n<p>I&#8217;m currently working on the &#8220;settings&#8221; area for (if you haven&#8217;t heard yet, I love saying the title) my upcoming game, Offender 2 (World War B: War on Rabbits).<br \/>\n<img decoding=\"async\" src=\"http:\/\/nathalielawhead.com\/sourcefiles\/Offender2\/code_examples\/offender_2_customize.jpg\" alt=\"\" \/><br \/>\nFor those that are interested I have a gallery of the game&#8217;s artwork here: <a href=\"http:\/\/offender2.alienmelon.com\/artwork\/\" target=\"_blank\">http:\/\/offender2.alienmelon.com\/artwork\/<\/a> which I&#8217;m continuously updating as I make progress. Visit it for a sneak peek of the game&#8217;s artwork, style, and more\u2026 I&#8217;m excited.<br \/>\nOk. The snippet:<\/p>\n<p><strong>Preview it here: <\/strong><br \/>\n[swfobj src=&#8221;http:\/\/nathalielawhead.com\/sourcefiles\/Offender2\/code_examples\/keyboard_shortcut_example.swf&#8221; height=&#8221;85&#8243; width=&#8221;393&#8243;]<\/p>\n<p><strong>Download the .fla here:<\/strong> <a href=\"http:\/\/nathalielawhead.com\/sourcefiles\/Offender2\/code_examples\/\" target=\"_blank\">http:\/\/nathalielawhead.com\/sourcefiles\/Offender2\/code_examples\/<\/a><\/p>\n<p>Where <code>txt_key_menu<\/code> is an input text field with a 1 character limit.<br \/>\nThe input is saved to a shared object which is checked against in the KeyboardEvent.<br \/>\nIf the keyboard character pressed matches the SO then the desired event is triggered.<\/p>\n<p><code>var so_keys:SharedObject = SharedObject.getLocal(\"keyexample\/data\");<\/p>\n<p>if(so_keys.data.key_menu == undefined){<br \/>\n\tso_keys.data.key_menu = \"m\";<br \/>\n\tso_keys.flush();<br \/>\n};<\/p>\n<p>\/\/keyboard input restrictions<br \/>\ntxt_key_menu.restrict = \"a-z 0-9\";<br \/>\ntxt_key_menu.text = so_keys.data.key_menu;<br \/>\n\/\/<br \/>\ntxt_key_menu.addEventListener(FocusEvent.FOCUS_IN, evnt_key_menu);<br \/>\ntxt_key_menu.addEventListener(FocusEvent.FOCUS_OUT, evnt_key_menu);<br \/>\n\/\/<br \/>\nfunction evnt_key_menu(event:FocusEvent):void {<br \/>\n\ttrace(event.type);<br \/>\n\tif(event.type==\"focusIn\"){<br \/>\n\t\ttxt_key_menu.text = \"\";<br \/>\n\t}<br \/>\n\tif(event.type==\"focusOut\" && txt_key_menu.text == \"\"){<br \/>\n\t\t\/\/<br \/>\n\t\ttrace(\"no input selected revert to m\");<br \/>\n\t\ttxt_output.text = \"no input selected revert to m\";<br \/>\n\t\t\/\/<br \/>\n\t\ttxt_key_menu.text = so_keys.data.key_menu;<br \/>\n\t};<br \/>\n\tif(event.type==\"focusOut\"){<br \/>\n\t\tso_keys.data.key_menu = txt_key_menu.text;<br \/>\n\t\tso_keys.flush();<br \/>\n\t\t\/\/<br \/>\n\t\ttrace(\"shortcut is now set to: \"+so_keys.data.key_menu);<br \/>\n\t\ttxt_output.text =\"shortcut is now set to: \"+so_keys.data.key_menu;<br \/>\n\t\t\/\/<br \/>\n\t};<br \/>\n}<br \/>\nfunction evnt_key_shortcut(event:KeyboardEvent){<br \/>\n\tif(String.fromCharCode(event.charCode) == so_keys.data.key_menu){<br \/>\n\t\ttrace(\"the shortcut key has been pressed\");<br \/>\n\t\ttxt_output.text = \"the shortcut key has been pressed\";<br \/>\n\t};<br \/>\n}<br \/>\nstage.addEventListener(KeyboardEvent.KEY_DOWN, evnt_key_shortcut);<br \/>\n<\/code><\/p>\n<p>Well&#8230; <a href=\"http:\/\/offender2.alienmelon.com\/artwork\/\" target=\"_blank\">Drop by the gallery<\/a> to check out the artwork, and animations (Yeah. Misspellings and all. I haven&#8217;t gotten around to that part yet). It&#8217;s come a long, and exhausting way!<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/offender2.alienmelon.com\/artwork\/offender_2_newspaper_surrender.png\" alt=\"\" \/><\/p>\n<p>So&#8230; back to work!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple example that I&#8217;m throwing up here because it may help some googler looking for a quick solution (just strip out the trace and txt_output). This lets you set an alphanumeric (letters and numbers) character as a desired &#8220;keyboard shortcut&#8221; which is saved for use throughout your game (or application). I&#8217;m currently working on the &#8220;settings&#8221; area for (if you haven&#8217;t heard yet, I love saying the title) my upcoming game, Offender 2 (World War B: War on Rabbits). For those that are interested I have a gallery of the game&#8217;s artwork here: http:\/\/offender2.alienmelon.com\/artwork\/ which I&#8217;m continuously&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":4522,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[12],"tags":[],"class_list":["post-2810","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-resources"],"_links":{"self":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/comments?post=2810"}],"version-history":[{"count":15,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2810\/revisions"}],"predecessor-version":[{"id":2828,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/posts\/2810\/revisions\/2828"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media\/4522"}],"wp:attachment":[{"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/media?parent=2810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/categories?post=2810"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nathalielawhead.com\/candybox\/wp-json\/wp\/v2\/tags?post=2810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}