Your Ad Here

Posted By

ctrloptcmd on 06/10/09


Tagged

konamicheatcodes


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

themill
liamr
volvis
jslice390
TrentSkunk


Konami.as


 / Published in: ActionScript 3
 

URL: http://ctrloptcmd.com

Listen for cheatcodes in ActionScript 3

  1. package com.ctrloptcmd.string {
  2.  
  3.  
  4. /**
  5. * Konami.as
  6. * Up Up Down Down Left Right Left Right B A
  7. *
  8. * @langversion: ActionScript 3.0
  9. * @playerversion: Flash 9.0
  10. *
  11. * @author: Martin Jacobsen
  12. * @since: 27-05-2009
  13. * @description : Simple Class to implement a 'cheat code' in your Flash app.
  14. * Useful for easter eggs and similar shenaningans.
  15. *
  16. * Usage should be pretty straightforward. Needs a reference to stage for
  17. * listening purposes. If you don't pass an Array with keyCodes in
  18. * the constructor the Konami Code is used by default (using
  19. * the KONAMI Constant of Keys.as).
  20. *
  21. * To use another default (or indeed to pass an Array from the calling class)
  22. * the easiest way is to use Keys.getKeyCodes("mystring");
  23. *
  24. * If you don't want to use Keys.as, delete line 41 and use any Array
  25. * of keyCodes you please as the default.
  26. *
  27. * @copyright :
  28. * No restrictions. Leave my name and link and do what thou wilt shall be the whole of the law
  29. *
  30. */
  31.  
  32.  
  33. import flash.display.Stage;
  34. import flash.events.Event;
  35. import flash.events.KeyboardEvent;
  36. import flash.events.EventDispatcher;
  37. import com.ctrloptcmd.string.Keys;
  38.  
  39.  
  40. public class Konami extends EventDispatcher {
  41.  
  42. private var sequence : Array;
  43. private var count : int;
  44.  
  45. public static const CODE_COMPLETE : String = "code_complete";
  46.  
  47. public function Konami (stage : Stage, _sequence : Array = null){
  48.  
  49. if(_sequence == null)
  50. sequence = Keys.KONAMI;
  51. else
  52. sequence = _sequence;
  53.  
  54. stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
  55. count = 0;
  56.  
  57. }
  58.  
  59. private function onKeyUp( ke : KeyboardEvent) : void {
  60. for (var i:int = 0; i < sequence.length; i++) {
  61. if (count == i) {
  62. if (ke.keyCode != sequence[i]) {
  63. count = 0;
  64. return;
  65. } else {
  66. count++;
  67. if (count != sequence.length) {
  68. return;
  69. }
  70. }
  71. }
  72. if (count == sequence.length) {
  73. count = 0;
  74. dispatchEvent(new Event(CODE_COMPLETE,true,false));
  75. }
  76.  
  77. }
  78.  
  79. }
  80. }
  81.  
  82. }

Report this snippet  

You need to login to post a comment.