PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/Main.hx

http://boxyboxumber.googlecode.com/
Haxe | 1052 lines | 840 code | 125 blank | 87 comment | 56 complexity | eed32177d4d9d9f341fd1c68512c8c56 MD5 | raw file
  1. import flash.display.BitmapData;
  2. import flash.display.Bitmap;
  3. import flash.geom.Rectangle;
  4. import flash.geom.Point;
  5. import flash.events.Event;
  6. import flash.events.KeyboardEvent;
  7. import flash.display.Sprite;
  8. import flash.media.Sound;
  9. import flash.ui.Keyboard;
  10. import flash.text.TextField;
  11. import flash.text.TextFieldAutoSize;
  12. import flash.text.TextFormat;
  13. import flash.text.TextFormatAlign;
  14. import flash.filters.GlowFilter;
  15. import haxe.FastList;
  16. import flash.events.MouseEvent;
  17. import flash.net.SharedObject;
  18. import flash.Lib;
  19. import haxe.Log;
  20. import flash.utils.Timer;
  21. import haxe.Timer;
  22. import flash.events.Event;
  23. import flash.display.MovieClip;
  24. import flash.media.SoundChannel;
  25. //imports
  26. import Images;
  27. import Sounds;
  28. //Pages
  29. import InstructionPage;
  30. import CreditsPage;
  31. import OptionsPage;
  32. import PauseMenu;
  33. //Levels
  34. import BossLevel;
  35. import Level1;
  36. import Level2;
  37. import CustomSprite;
  38. import Kongregate;
  39. import Logo;
  40. import SoundPlayer;
  41. class Main extends Sprite{
  42. private var ground : Int;
  43. private var currentLevel : Int;
  44. private var damageTaken : Int;
  45. private var difficultly : Int;
  46. private var BaseLevels : Array<BaseLevel>;
  47. private var CutScences : Array<Intro>;
  48. private var State : String;
  49. private var savedData:SharedObject;
  50. private var ContinueGameButton:Sprite;
  51. private var StartGameButton:Sprite;
  52. private var MainMenuButton:Sprite;
  53. private var InstructionsButton:Sprite;
  54. private var CreditsButton:Sprite;
  55. private var OptionsButton:Sprite;
  56. private var BoxyPic:CustomSprite;
  57. private var Title:TextField;
  58. private var myLogo : Logo;
  59. private var label1:TextField;
  60. private var TryAgainPic:Sprite;
  61. private var TryAgainMenu:Sprite;
  62. var myTimer:Timer;
  63. var music:Sound;
  64. var track1:Track1;
  65. var track2:Track2;
  66. var track3:Track3;
  67. private var player:SoundPlayer;
  68. private var BackgroundImage : Sprite;
  69. private var StartGameDifficulty:Sprite;
  70. private var StartGameDifficultyAdded:Bool;
  71. public var Instruction : InstructionPage;
  72. public var Credit: CreditsPage;
  73. public var OptionsMenu : OptionsPage;
  74. public var MouseImage : Sprite;
  75. public var KeysImage : Sprite;
  76. public var textBubble : Sprite;
  77. public var textBubbleText : TextField;
  78. var mKeyDown:Array<Bool>;
  79. var pausePosition:Float;
  80. // music.play();
  81. var volume:Bool;
  82. var sound:Bool;
  83. var framerate:Int;
  84. var kongVar : CKongregate;
  85. public function new(){
  86. haxe.remoting.AMFConnection.registerClassAlias("FastListAlias",FastList);
  87. super();
  88. BaseLevels = [];
  89. CutScences = [];
  90. getLevelData();
  91. //inPostion,inMoveSpeed,inJumpSpeed,inAttackSpeed,inHealth:Int
  92. initLevels();
  93. var txtFormat:TextFormat = new TextFormat();
  94. txtFormat.align = TextFormatAlign.CENTER;
  95. //Add Main to Current Flash
  96. flash.Lib.current.addChild(this);
  97. //Create Menu (own Class?)
  98. Title = new TextField();
  99. Title.x = 50; Title.y = 20;
  100. Title.width = Constants.flashWidth-100; Title.height = Constants.flashHeight;
  101. Title.multiline = true;
  102. var myFormat:TextFormat = new TextFormat();
  103. myFormat.size = 25;
  104. myFormat.align = TextFormatAlign.CENTER;
  105. Title.defaultTextFormat = myFormat;
  106. Title.htmlText = "<font color='#FFFFFF'>Boxy Boxumber</font>";
  107. Title.selectable = false;
  108. //It seems like there should be a better way to add a bitmap to a Sprite than filling the background with the bitmap. But this works.
  109. StartGameButton = new Sprite();
  110. StartGameButton.graphics.beginFill(0x000000);
  111. StartGameButton.graphics.lineStyle(1,0xFFFFFF);
  112. StartGameButton.graphics.drawRect(0, 0, 250 ,48);
  113. var StartGameButtonText = new TextField();
  114. StartGameButtonText.htmlText = "<font size='26' color='#FFFFFF'>"+"Start Game"+"</font>";
  115. StartGameButtonText.width = 250;
  116. StartGameButtonText.x = 0;
  117. StartGameButtonText.y = 5;
  118. StartGameButtonText.setTextFormat(txtFormat);
  119. StartGameButtonText.selectable = false;
  120. StartGameButtonText.mouseEnabled = false;
  121. StartGameButton.addChild(StartGameButtonText);
  122. StartGameButton.x = 20;
  123. StartGameButton.y = 270;
  124. StartGameButton.buttonMode = true;
  125. StartGameButton.addEventListener(MouseEvent.ROLL_OVER,onStartGameHover);
  126. StartGameButton.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
  127. StartGameButton.addEventListener(MouseEvent.CLICK, onStartGameButtonClick);
  128. ContinueGameButton = new Sprite();
  129. ContinueGameButton.graphics.beginFill(0x000000);
  130. ContinueGameButton.graphics.lineStyle(1,0xFFFFFF);
  131. ContinueGameButton.graphics.drawRect(0, 0, 250 ,48);
  132. var ContinueGameButtonText = new TextField();
  133. ContinueGameButtonText.htmlText = "<font size='26' color='#FFFFFF'>"+"Continue Game"+"</font>";
  134. ContinueGameButtonText.width = 250;
  135. ContinueGameButtonText.x = 0;
  136. ContinueGameButtonText.setTextFormat(txtFormat);
  137. ContinueGameButtonText.y = 5;
  138. ContinueGameButtonText.selectable = false;
  139. ContinueGameButtonText.mouseEnabled = false;
  140. ContinueGameButton.addChild(ContinueGameButtonText);
  141. ContinueGameButton.x = 20;
  142. ContinueGameButton.y = 330;
  143. ContinueGameButton.buttonMode = true;
  144. ContinueGameButton.addEventListener(MouseEvent.ROLL_OVER,onContinueGameHover);
  145. ContinueGameButton.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
  146. ContinueGameButton.addEventListener(MouseEvent.CLICK, onContinueGameButtonClick);
  147. InstructionsButton = new Sprite();
  148. InstructionsButton.graphics.beginFill(0x000000);
  149. InstructionsButton.graphics.lineStyle(1,0xFFFFFF);
  150. InstructionsButton.graphics.drawRect(0, 0, 250 ,48);
  151. var InstructionsButtonText = new TextField();
  152. InstructionsButtonText.htmlText = "<font size='26' color='#FFFFFF'>"+"Instructions"+"</font>";
  153. InstructionsButtonText.width = 250;
  154. InstructionsButtonText.x = 0;
  155. InstructionsButtonText.setTextFormat(txtFormat);
  156. InstructionsButtonText.y = 5;
  157. InstructionsButtonText.selectable = false;
  158. InstructionsButtonText.mouseEnabled = false;
  159. InstructionsButton.addChild(InstructionsButtonText);
  160. InstructionsButton.x = 20;
  161. InstructionsButton.y = 0;
  162. InstructionsButton.buttonMode = true;
  163. InstructionsButton.addEventListener(MouseEvent.CLICK, onInstrutionsButtonClick);
  164. Instruction = new InstructionPage();
  165. Instruction.addEventListener("Back",onInstrutionsBack);
  166. CreditsButton = new Sprite();
  167. CreditsButton.graphics.beginFill(0x000000);
  168. CreditsButton.graphics.lineStyle(1,0xFFFFFF);
  169. CreditsButton.graphics.drawRect(0, 0, 250 ,48);
  170. var CreditsButtonText = new TextField();
  171. CreditsButtonText.htmlText = "<font size='26' color='#FFFFFF'>"+"Credits"+"</font>";
  172. CreditsButtonText.width = 250;
  173. CreditsButtonText.x = 0;
  174. CreditsButtonText.setTextFormat(txtFormat);
  175. CreditsButtonText.y = 5;
  176. CreditsButtonText.selectable = false;
  177. CreditsButtonText.mouseEnabled = false;
  178. CreditsButton.addChild(CreditsButtonText);
  179. CreditsButton.x = 20;
  180. CreditsButton.y = 390;
  181. CreditsButton.buttonMode = true;
  182. CreditsButton.addEventListener(MouseEvent.ROLL_OVER,onCreditsHover);
  183. CreditsButton.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
  184. CreditsButton.addEventListener(MouseEvent.CLICK, onCreditsButtonClick);
  185. Credit = new CreditsPage();
  186. Credit.addEventListener("Back",onCreditsBack);
  187. OptionsButton = new Sprite();
  188. OptionsButton.graphics.beginFill(0x000000);
  189. OptionsButton.graphics.lineStyle(1,0xFFFFFF);
  190. OptionsButton.graphics.drawRect(0, 0, 250 ,48);
  191. var OptionsButtonText = new TextField();
  192. OptionsButtonText.htmlText = "<font size='26' color='#FFFFFF'>"+"Options"+"</font>";
  193. OptionsButtonText.width = 250;
  194. OptionsButtonText.x = 0;
  195. OptionsButtonText.setTextFormat(txtFormat);
  196. OptionsButtonText.y = 5;
  197. OptionsButtonText.selectable = false;
  198. OptionsButtonText.mouseEnabled = false;
  199. OptionsButton.addChild(OptionsButtonText);
  200. OptionsButton.x = 20;
  201. OptionsButton.y = 450;
  202. OptionsButton.buttonMode = true;
  203. OptionsButton.addEventListener(MouseEvent.ROLL_OVER,onOptionsHover);
  204. OptionsButton.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
  205. OptionsButton.addEventListener(MouseEvent.CLICK, onOptionsButtonClick);
  206. OptionsMenu = new OptionsPage();
  207. OptionsMenu.addEventListener("Back",onOptionsMenuBack);
  208. OptionsMenu.addEventListener("ResetSavedData",onOptionsMenuResetSavedData);
  209. OptionsMenu.addEventListener("VolumeToggle",onOptionsMenuVolumeToggle);
  210. OptionsMenu.addEventListener("PrevTrack",onPrevTrack);
  211. OptionsMenu.addEventListener("NextTrack",onNextTrack);
  212. OptionsMenu.addEventListener("SoundEffectsToggle",onOptionsMenuSoundEffectsToggle);
  213. //this.addEventListener(Event.ENTER_FRAME, OnEnter);
  214. myTimer = new Timer(12);
  215. myTimer.addEventListener("timer", OnEnter);
  216. myTimer.start();
  217. label1 = new TextField();
  218. TryAgainMenu = new Sprite();
  219. //TryAgainMenu.width = Constants.flashWidth;
  220. TryAgainMenu.graphics.beginFill(0x000000);
  221. TryAgainMenu.graphics.drawRect(0,0,Constants.flashWidth,Constants.flashHeight);
  222. TryAgainMenu.graphics.endFill();
  223. label1.width = 600;
  224. label1.height = Constants.flashHeight;
  225. label1.x = 25;
  226. label1.y = 25;
  227. label1.wordWrap = true;
  228. label1.selectable = false;
  229. label1.htmlText = "<font size='30' color='#FFFFFF'>" +"Peace has been restored to Box-ville. Purged of the pink and interesting characters, the overrated FPS can now rise again. YAY!" + "</font>";
  230. TryAgainMenu.addChild(label1);
  231. TryAgainPic = new Sprite();
  232. TryAgainPic.graphics.beginBitmapFill(new Menu());
  233. TryAgainPic.graphics.drawRect(0, 0, 46,52);
  234. TryAgainPic.x = 300;
  235. TryAgainPic.y = 75;
  236. MainMenuButton = new Sprite();
  237. MainMenuButton.graphics.beginFill(0x000000);
  238. MainMenuButton.graphics.lineStyle(1,0xFFFFFF);
  239. MainMenuButton.graphics.drawRect(0, 0, 250 ,48);
  240. var MainMenuButtonText = new TextField();
  241. MainMenuButtonText.htmlText = "<font size='30' color='#FFFFFF'>"+"Main Menu"+"</font>";
  242. MainMenuButtonText.width = 250;
  243. MainMenuButtonText.x = 50;
  244. MainMenuButtonText.y = 5;
  245. MainMenuButtonText.selectable = false;
  246. MainMenuButtonText.mouseEnabled = false;
  247. MainMenuButton.addChild(MainMenuButtonText);
  248. MainMenuButton.x = 200;
  249. MainMenuButton.y = 300;
  250. MainMenuButton.buttonMode = true;
  251. MainMenuButton.addEventListener(MouseEvent.CLICK, onMainMenuButtonClick);
  252. MouseImage = new Sprite();
  253. MouseImage.graphics.beginBitmapFill(new RightClick());
  254. MouseImage.graphics.drawRect(0, 0, 200,200);
  255. MouseImage.x = 290;
  256. MouseImage.y = 270;
  257. var MouseImageText = new TextField();
  258. MouseImageText.htmlText = "<font size='20' color='#FFFFFF'>"+"Shoot"+"</font>";
  259. MouseImageText.width = 250;
  260. MouseImageText.x = 70;
  261. MouseImageText.y =180;
  262. MouseImageText.selectable = false;
  263. MouseImage.addChild(MouseImageText);
  264. textBubble = new Sprite();
  265. textBubble.graphics.beginBitmapFill(new Bubble());
  266. textBubble.graphics.drawRect(0, 0, 250,213);
  267. textBubble.x = 390;
  268. textBubble.y = 40;
  269. textBubbleText = new TextField();
  270. textBubbleText.htmlText = "<font size='16' color='#000000'>I'm Boxy!</font>";
  271. textBubbleText.autoSize = TextFieldAutoSize.CENTER;
  272. textBubbleText.width = 180;
  273. textBubbleText.wordWrap = true;
  274. textBubbleText.multiline = true;
  275. textBubbleText.selectable = false;
  276. textBubbleText.mouseEnabled = false;
  277. textBubbleText.x = 25;
  278. textBubble.addChild(textBubbleText);
  279. textBubbleText.y=25;
  280. KeysImage = new Sprite();
  281. KeysImage.graphics.beginBitmapFill(new Keys());
  282. KeysImage.graphics.drawRect(0, 0, 172,116);
  283. KeysImage.x = 450;
  284. KeysImage.y = 320;
  285. var KeysImageText = new TextField();
  286. KeysImageText.htmlText = "<font size='20' color='#FFFFFF'>"+"Jump"+"</font>";
  287. KeysImageText.width = 250;
  288. KeysImageText.x = 62;
  289. KeysImageText.y =-25;
  290. KeysImageText.selectable = false;
  291. KeysImage.addChild(KeysImageText);
  292. var LeftText = new TextField();
  293. LeftText.htmlText = "<font size='20' color='#FFFFFF'>"+"Left"+"</font>";
  294. LeftText.width = 250;
  295. LeftText.x = 10;
  296. LeftText.y =105;
  297. LeftText.selectable = false;
  298. KeysImage.addChild(LeftText);
  299. var RightText = new TextField();
  300. RightText.htmlText = "<font size='20' color='#FFFFFF'>"+"Right"+"</font>";
  301. RightText.width = 250;
  302. RightText.x = 115;
  303. RightText.y =105;
  304. RightText.selectable = false;
  305. KeysImage.addChild(RightText);
  306. StartGameDifficulty = new Sprite();
  307. StartGameDifficulty.graphics.beginFill(0x555555);
  308. StartGameDifficulty.graphics.drawRect(0,0,330,250);
  309. StartGameDifficulty.graphics.endFill();
  310. StartGameDifficulty.x = 290;
  311. StartGameDifficulty.y = 262;
  312. StartGameDifficultyAdded = false;
  313. var easyGame = new Sprite();
  314. easyGame.graphics.lineStyle(1,0xFFFFFF);
  315. easyGame.graphics.beginFill(0x000000);
  316. easyGame.graphics.drawRect(0, 0, 250 ,48);
  317. var easyGameText = new TextField();
  318. easyGameText.htmlText = "<font size='26' color='#FFFFFF'>"+"Easy"+"</font>";
  319. easyGameText.width = 250;
  320. easyGameText.x = 0;
  321. easyGameText.setTextFormat(txtFormat);
  322. easyGameText.y = 5;
  323. easyGameText.selectable = false;
  324. easyGameText.mouseEnabled = false;
  325. easyGame.addChild(easyGameText);
  326. StartGameDifficulty.addChild(easyGame);
  327. easyGame.x = 50;
  328. easyGame.y = 20;
  329. easyGame.buttonMode = true;
  330. easyGame.addEventListener(MouseEvent.ROLL_OVER,onEasyGameHover);
  331. easyGame.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
  332. easyGame.addEventListener(MouseEvent.CLICK, onEasyGameButtonClick);
  333. var mediumGame = new Sprite();
  334. mediumGame.graphics.beginFill(0x000000);
  335. mediumGame.graphics.lineStyle(1,0xFFFFFF);
  336. mediumGame.graphics.drawRect(0, 0, 250 ,48);
  337. var mediumGameText = new TextField();
  338. mediumGameText.htmlText = "<font size='26' color='#FFFFFF'>"+"Medium"+"</font>";
  339. mediumGameText.width = 250;
  340. mediumGameText.x = 0;
  341. mediumGameText.setTextFormat(txtFormat);
  342. mediumGameText.y = 5;
  343. mediumGameText.selectable = false;
  344. mediumGameText.mouseEnabled = false;
  345. mediumGame.addChild(mediumGameText);
  346. StartGameDifficulty.addChild(mediumGame);
  347. mediumGame.x = 50;
  348. mediumGame.y = 100;
  349. mediumGame.buttonMode = true;
  350. mediumGame.addEventListener(MouseEvent.ROLL_OVER,onMediumGameHover);
  351. mediumGame.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
  352. mediumGame.addEventListener(MouseEvent.CLICK, onMediumGameButtonClick);
  353. var hardGame = new Sprite();
  354. hardGame.graphics.lineStyle(1,0xFFFFFF);
  355. hardGame.graphics.beginFill(0x000000);
  356. hardGame.graphics.drawRect(0, 0, 250 ,48);
  357. var hardGameText = new TextField();
  358. hardGameText.htmlText = "<font size='26' color='#FFFFFF'>"+"Hard"+"</font>";
  359. hardGameText.width = 250;
  360. hardGameText.x = 0;
  361. hardGameText.setTextFormat(txtFormat);
  362. hardGameText.y = 5;
  363. hardGameText.selectable = false;
  364. hardGameText.mouseEnabled = false;
  365. hardGame.addChild(hardGameText);
  366. StartGameDifficulty.addChild(hardGame);
  367. hardGame.x = 50;
  368. hardGame.y = 175;
  369. hardGame.buttonMode = true;
  370. hardGame.addEventListener(MouseEvent.ROLL_OVER,onHardGameHover);
  371. hardGame.addEventListener(MouseEvent.ROLL_OUT,onHoverOut);
  372. hardGame.addEventListener(MouseEvent.CLICK, onHardGameButtonClick);
  373. BoxyPic = new CustomSprite();
  374. BoxyPic.graphics.drawRect(0,0,64,64);
  375. BoxyPic.graphics.endFill();
  376. var tileSheetColumn:Int = Std.int(Constants.BOX / 10);
  377. var tileSheetRow:Int = Std.int(Constants.BOX % 10);
  378. var sourceRectangle:Rectangle = new Rectangle
  379. (tileSheetColumn * 64, tileSheetRow * 64, 48,48);
  380. var destinationPoint:Point = new Point(0, 0);
  381. var modelBitmapData = new BitmapData(48, 48, true, 0);
  382. modelBitmapData.copyPixels
  383. (
  384. new TileSheet(),
  385. sourceRectangle,
  386. destinationPoint,
  387. null, null, true
  388. );
  389. var modelBitmap = new Bitmap(modelBitmapData);
  390. BoxyPic.addChild(modelBitmap);
  391. BoxyPic.resize(80,80);
  392. BoxyPic.removeChild(modelBitmap);
  393. BoxyPic.x = 310;
  394. BoxyPic.y = 190;
  395. //The State Will determine whether to load a level, or show Main Menu, or whatever. Used in the OnEnterEvent.
  396. State = "LoadingLogo";
  397. //sState = "LoadMainMenu";
  398. track1 = new Track1();
  399. track2 = new Track2();
  400. track3 = new Track3();
  401. //preloader = new PreloaderHost();
  402. BackgroundImage = new Sprite();
  403. BackgroundImage.graphics.beginFill(0x555555);
  404. BackgroundImage.graphics.drawRect(0,0,Constants.flashWidth,Constants.flashHeight);
  405. BackgroundImage.graphics.endFill();
  406. player = new SoundPlayer(track1,track2,track3);
  407. volume = true;
  408. sound = true;
  409. myLogo = new Logo();
  410. addChild(myLogo);
  411. try
  412. {
  413. kongVar = new CKongregate();
  414. }
  415. catch (msg:Dynamic)
  416. {
  417. }
  418. mKeyDown = [];
  419. stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown );
  420. stage.addEventListener(KeyboardEvent.KEY_UP, OnKeyUp );
  421. //preloader.addEventListener(Event.ENTER_FRAME, OnMovieEnter);
  422. }
  423. public function OnKeyUp (event:KeyboardEvent)
  424. {
  425. mKeyDown[event.keyCode] = false;
  426. //lastUp = event.keyCode;
  427. }
  428. public function OnKeyDown(event:KeyboardEvent)
  429. {
  430. // When a key is held down, multiple KeyDown events are generated.
  431. // This check means we only pick up the first one.
  432. if (!mKeyDown[event.keyCode])
  433. {
  434. // Store for use in game
  435. mKeyDown[event.keyCode] = true;
  436. //I do this here, because if they hold down P,
  437. // it won't pause and unpause constantly.
  438. if(mKeyDown[ 77 ] == true)
  439. {
  440. volume = !volume;
  441. BaseLevels[currentLevel-1].pauseMenu.renderVolume(volume);
  442. }
  443. }
  444. }
  445. function loadMenu()
  446. {
  447. addChild(BackgroundImage);
  448. addChild(Title);
  449. addChild(StartGameButton);
  450. addChild(ContinueGameButton);
  451. //addChild(InstructionsButton);
  452. addChild(CreditsButton);
  453. addChild(OptionsButton);
  454. addChild(BoxyPic);
  455. addChild(textBubble);
  456. addChild(MouseImage);
  457. addChild(KeysImage);
  458. }
  459. function removeMenu()
  460. {
  461. removeChild(Title);
  462. removeChild(StartGameButton);
  463. removeChild(ContinueGameButton);
  464. removeChild(BackgroundImage);
  465. //removeChild(InstructionsButton);
  466. removeChild(CreditsButton);
  467. removeChild(OptionsButton);
  468. removeChild(BoxyPic);
  469. removeChild(textBubble);
  470. removeChild(MouseImage);
  471. removeChild(KeysImage);
  472. if(StartGameDifficultyAdded)
  473. {removeChild(StartGameDifficulty);StartGameDifficultyAdded=false;}
  474. }
  475. //Change to stat Menu
  476. function loadTryAgainMenu(text:String)
  477. {
  478. try
  479. {
  480. if(savedData.data.difficultly == 1)
  481. {
  482. kongVar.SubmitStat("EasyBeaten",1);
  483. kongVar.SubmitStat("DamageTakenEasy",savedData.data.damageTaken);
  484. }
  485. else if (savedData.data.difficultly == 2)
  486. {
  487. kongVar.SubmitStat("MediumBeaten",1);
  488. kongVar.SubmitStat("DamageTakenMedium",savedData.data.damageTaken);
  489. }
  490. else if(savedData.data.difficultly == 3)
  491. {
  492. kongVar.SubmitStat("HardBeaten",1);
  493. kongVar.SubmitStat("DamageTakenHard",savedData.data.damageTaken);
  494. }
  495. }
  496. catch(msg: Dynamic)
  497. {
  498. }
  499. addChild(TryAgainMenu);
  500. addChild(MainMenuButton);
  501. //addChild(TryAgainPic);
  502. player.stop();
  503. }
  504. function removeTryAgainMenu()
  505. {
  506. removeChild(TryAgainMenu);
  507. removeChild(MainMenuButton);
  508. //removeChild(TryAgainPic);
  509. }
  510. function initLevels()
  511. {
  512. CutScences[0] = new PinkIntro(flash.Lib.current.stage);
  513. //
  514. BaseLevels[0] = new Level1(flash.Lib.current.stage);
  515. CutScences[1] = new BlueIntro(flash.Lib.current.stage);
  516. BaseLevels[1] = new Level2(flash.Lib.current.stage);
  517. CutScences[2] = new FairyIntro(flash.Lib.current.stage);
  518. BaseLevels[2] = new Level3(flash.Lib.current.stage);
  519. CutScences[3] = new PinkFairyIntro(flash.Lib.current.stage);
  520. BaseLevels[3] = new Level4(flash.Lib.current.stage);
  521. CutScences[4] = new FlowerIntro(flash.Lib.current.stage);
  522. BaseLevels[4] = new Level5(flash.Lib.current.stage);
  523. CutScences[5] = new HeartIntro(flash.Lib.current.stage);
  524. BaseLevels[5] = new Level6(flash.Lib.current.stage);
  525. CutScences[6] = new KittyKatIntro(flash.Lib.current.stage);
  526. BaseLevels[6] = new Level7(flash.Lib.current.stage);
  527. CutScences[7] = new BossIntro(flash.Lib.current.stage);
  528. BaseLevels[7] = new BossLevel(flash.Lib.current.stage);
  529. }
  530. function OnEnter(e:flash.events.Event)
  531. {
  532. if(player !=null)
  533. {
  534. if(!volume)
  535. {
  536. pausePosition = player.getPosition();
  537. player.stop();
  538. }
  539. else
  540. {
  541. player.play(pausePosition);
  542. }
  543. }
  544. if(State == "LoadMainMenu")
  545. {
  546. loadMenu();
  547. State = "MainMenu";
  548. }
  549. if(State == "MainMenu" )
  550. {
  551. return;
  552. }
  553. if(State == "TryAgain")
  554. {
  555. AnimateTryAgainPic();
  556. return;
  557. }
  558. if(State == "InitLoad")
  559. {
  560. // addChild(preloader);
  561. // preloader.play();
  562. State = "InitLoading";
  563. }
  564. if(State == "InitLoading")
  565. {
  566. //Always Playing. Possible error with swfmill
  567. //Skipping for now.
  568. // if(!preloader.playing)
  569. // {
  570. // preloader.stop();
  571. // removeChild(preloader);
  572. State = "LoadingLogo";
  573. // }
  574. }
  575. if(State == "LoadingLogo")
  576. {
  577. if(!myLogo.play())
  578. {
  579. State = "LoadMainMenu";
  580. }
  581. }
  582. if(State == "StartGame")
  583. {
  584. //player.play(pausePosition);
  585. currentLevel =1;
  586. loadCutScence();
  587. }
  588. if(State == "ContinueGame")
  589. {
  590. //player.play(pausePosition);
  591. loadCutScence();
  592. }
  593. if(State == "LoadLevel")
  594. {
  595. loadCurrentLevel();
  596. }
  597. if(State == "Playing")
  598. {
  599. if(BaseLevels[currentLevel-1] != null)
  600. {
  601. if(!volume)
  602. {
  603. pausePosition = player.getPosition();
  604. player.stop();
  605. }
  606. else
  607. {
  608. player.play(pausePosition);
  609. }
  610. return;
  611. // else
  612. // {
  613. //
  614. // try
  615. // {
  616. // //kongVar.SubmitStat("Won",1);
  617. // }
  618. // catch(msg: Dynamic)
  619. // {
  620. //
  621. // }
  622. //
  623. // loadTryAgainMenu("Try Again?");
  624. // State = "TryAgain";
  625. // return;
  626. // }
  627. }
  628. }
  629. // try
  630. // {
  631. // // var req:flash.net.URLRequest = new flash.net.URLRequest("http://av.adobe.com/podcast/csbu_dev_podcast_epi_2.mp3");
  632. // //
  633. // // Sound.load(req);
  634. // var music = new BackgroundMusic();
  635. // music.play();
  636. // }
  637. // catch (err:flash.Error)
  638. // {
  639. // trace(err.message);
  640. // }
  641. }
  642. public function AnimateTryAgainPic()
  643. {
  644. var randomNumber = Std.random(24);
  645. if(randomNumber == 1)
  646. TryAgainPic.x = TryAgainPic.x + 5;
  647. if(randomNumber == 2)
  648. TryAgainPic.x = TryAgainPic.x - 5;
  649. }
  650. public function loadCutScence() : Bool
  651. {
  652. if(currentLevel > CutScences.length && CutScences[currentLevel-1]==null)
  653. {State="Playing";loadCurrentLevel();}
  654. CutScences[currentLevel-1].Load();
  655. CutScences[currentLevel-1].addEventListener("LevelFinished",onLevelFinished);
  656. State="CutScence";
  657. return true;
  658. }
  659. //Loads Level based on Current Level
  660. public function loadCurrentLevel() : Bool
  661. {
  662. State = "Playing";
  663. if(currentLevel > BaseLevels.length)
  664. {State="TryAgain";loadTryAgainMenu("YOU WON");}
  665. BaseLevels[currentLevel-1].addEventListener("LevelFinished",onLevelFinished);
  666. BaseLevels[currentLevel-1].addEventListener("LevelQuit",onLevelQuit);
  667. BaseLevels[currentLevel-1].addEventListener("VolumeToggle",onBaseLevelVolumeToggle);
  668. BaseLevels[currentLevel-1].addEventListener("PrevTrack",onPrevTrack);
  669. BaseLevels[currentLevel-1].addEventListener("NextTrack",onNextTrack);
  670. State = "Playing";
  671. //Level1.Load();
  672. BaseLevels[currentLevel-1].Load(volume,sound,kongVar,savedData.data.difficultly);
  673. // BaseLevels[currentLevel-1].pauseMenu.renderVolume(volume);
  674. // BaseLevels[currentLevel-1].pauseMenu.renderSoundEffects(sound);
  675. // BaseLevels[currentLevel-1].addEventListener("SoundEffectsToggle",onBaseLevelSoundEffectsToggle);
  676. return true;
  677. }
  678. public function getLevelData()
  679. {
  680. currentLevel = 1;
  681. pausePosition = 0;
  682. damageTaken = 0;
  683. difficultly = 2;
  684. //Load Save Data
  685. savedData = SharedObject.getLocal("LevelData");
  686. if(savedData.data.Level != null)
  687. {
  688. currentLevel = savedData.data.Level;
  689. pausePosition = savedData.data.SoundPosition;
  690. damageTaken = savedData.data.damageTaken;
  691. difficultly = savedData.data.difficultly;
  692. }
  693. }
  694. public function removeClip(preloader)
  695. {
  696. removeChild(preloader);
  697. }
  698. private function onStartGameButtonClick(event:MouseEvent) {
  699. addChild(StartGameDifficulty);
  700. StartGameDifficultyAdded = true;
  701. //removeMenu();
  702. //State = "StartGame";
  703. }
  704. private function onEasyGameButtonClick(event:MouseEvent)
  705. {
  706. removeMenu();
  707. State = "StartGame";
  708. savedData.data.Level = 1;
  709. savedData.data.damageTaken = 0;
  710. savedData.data.difficultly = 1;
  711. }
  712. private function onMediumGameButtonClick(event:MouseEvent)
  713. {
  714. removeMenu();
  715. State = "StartGame";
  716. savedData.data.Level = 1;
  717. savedData.data.damageTaken = 0;
  718. savedData.data.difficultly = 2;
  719. }
  720. private function onHardGameButtonClick(event:MouseEvent)
  721. {
  722. removeMenu();
  723. State = "StartGame";
  724. savedData.data.Level = 1;
  725. savedData.data.damageTaken = 0;
  726. savedData.data.difficultly = 3;
  727. }
  728. private function onContinueGameButtonClick(event:MouseEvent) {
  729. removeMenu();
  730. State = "ContinueGame";
  731. }
  732. //Hover Texts
  733. private function onStartGameHover(event:MouseEvent)
  734. {
  735. textBubbleText.htmlText = "<font size='16' color='#000000'>This will erase saved game information</font>";
  736. }
  737. private function onContinueGameHover(event:MouseEvent)
  738. {
  739. textBubbleText.htmlText = "<font size='16' color='#000000'>Continue an Existing Game</font>";
  740. }
  741. private function onOptionsHover(event:MouseEvent)
  742. {
  743. textBubbleText.htmlText = "<font size='16' color='#000000'>Disable Sound or Reset Saved Data</font>";
  744. textBubbleText.autoSize = TextFieldAutoSize.CENTER;
  745. }
  746. private function onCreditsHover(event:MouseEvent)
  747. {
  748. textBubbleText.htmlText = "<font size='16' color='#000000'>This is where I give people credit for making the game. Mostly me.</font>";
  749. textBubbleText.autoSize = TextFieldAutoSize.CENTER;
  750. }
  751. private function onEasyGameHover(event:MouseEvent)
  752. {
  753. textBubbleText.htmlText = "<font size='16' color='#000000'>You have 20 health and enemies have 5 health. It will be a piece of cake</font>";
  754. textBubbleText.autoSize = TextFieldAutoSize.CENTER;
  755. }
  756. private function onMediumGameHover(event:MouseEvent)
  757. {
  758. textBubbleText.htmlText = "<font size='16' color='#000000'>You have 10 health and enemies have 10 health. This may take some effort</font>";
  759. textBubbleText.autoSize = TextFieldAutoSize.CENTER;
  760. }
  761. private function onHardGameHover(event:MouseEvent)
  762. {
  763. textBubbleText.htmlText = "<font size='16' color='#000000'>You have 8 health and enemies have 15 health. Only for the Hard Core gamers.</font>";
  764. textBubbleText.autoSize = TextFieldAutoSize.CENTER;
  765. }
  766. private function onHoverOut(event:MouseEvent)
  767. {
  768. textBubbleText.htmlText = "<font size='16' color='#000000'>I'm Boxy!</font>";
  769. }
  770. private function onLevelFinished(event:Event)
  771. {
  772. if(State == "CutScence")
  773. {
  774. removeCutScencesEventListeners();
  775. loadCurrentLevel();
  776. }
  777. else
  778. {
  779. removeLevelEventListeners();
  780. savedData.data.damageTaken += BaseLevels[currentLevel-1].DamageTaken;
  781. currentLevel++;
  782. savedData.data.Level = currentLevel;
  783. loadCutScence();
  784. }
  785. }
  786. private function onLevelQuit(event:Event)
  787. {
  788. removeLevelEventListeners();
  789. State = "InitLoading";
  790. }
  791. private function removeLevelEventListeners()
  792. {
  793. BaseLevels[currentLevel-1].removeEventListener("LevelFinished",onLevelFinished);
  794. BaseLevels[currentLevel-1].removeEventListener("LevelQuit",onLevelQuit);
  795. BaseLevels[currentLevel-1].removeEventListener("PrevTrack",onPrevTrack);
  796. BaseLevels[currentLevel-1].removeEventListener("NextTrack",onNextTrack);
  797. }
  798. private function removeCutScencesEventListeners()
  799. {
  800. CutScences[currentLevel-1].removeEventListener("LevelFinished",onLevelFinished);
  801. }
  802. private function onMainMenuButtonClick(event:MouseEvent) {
  803. removeTryAgainMenu();
  804. State = "LoadMainMenu";
  805. }
  806. private function onInstrutionsButtonClick(event:MouseEvent) {
  807. removeMenu();
  808. addChild(Instruction);
  809. }
  810. private function onCreditsButtonClick(event:MouseEvent) {
  811. removeMenu();
  812. addChild(Credit);
  813. }
  814. private function onOptionsButtonClick(event:MouseEvent) {
  815. removeMenu();
  816. addChild(OptionsMenu);
  817. OptionsMenu.renderVolume(volume);
  818. OptionsMenu.renderSoundEffects(sound);
  819. }
  820. private function onInstrutionsBack(event:InstructionPageEvent)
  821. {
  822. removeChild(Instruction);
  823. loadMenu();
  824. }
  825. private function onOptionsMenuBack(event:OptionsPageEvent)
  826. {
  827. removeChild(OptionsMenu);
  828. loadMenu();
  829. }
  830. private function onPrevTrack(event:Event)
  831. {
  832. if(volume && player != null)
  833. {player.getPrevTrack();}
  834. }
  835. private function onNextTrack(event:Event)
  836. {
  837. if(volume && player != null)
  838. {player.getNextTrack();}
  839. }
  840. private function onOptionsMenuVolumeToggle(event:OptionsPageEvent)
  841. {
  842. volume = !volume;
  843. OptionsMenu.renderVolume(volume);
  844. }
  845. private function onOptionsMenuSoundEffectsToggle(event:OptionsPageEvent)
  846. {
  847. sound = !sound;
  848. OptionsMenu.renderSoundEffects(sound);
  849. }
  850. private function onBaseLevelVolumeToggle(event:PauseMenuEvent)
  851. {
  852. volume = !volume;
  853. BaseLevels[currentLevel-1].volume = volume;
  854. BaseLevels[currentLevel-1].pauseMenu.renderVolume(volume);
  855. }
  856. private function onBaseLevelSoundEffectsToggle(event:PauseMenuEvent)
  857. {
  858. sound = !sound;
  859. BaseLevels[currentLevel-1].sound = sound;
  860. //BaseLevels[currentLevel-1].pauseMenu.renderSoundEffects(sound);
  861. }
  862. private function onOptionsMenuResetSavedData(event:OptionsPageEvent)
  863. {
  864. savedData.data.Level = 1;
  865. savedData.data.SoundPosition = 0;
  866. savedData.data.damageTaken = 0;
  867. savedData.data.difficultly = 2;
  868. savedData.flush();
  869. getLevelData();
  870. pausePosition = 0;
  871. currentLevel = 1;
  872. }
  873. private function onCreditsBack(event:CreditsPageEvent)
  874. {
  875. removeChild(Credit);
  876. loadMenu();
  877. }
  878. //Couldn't get preloader to work.
  879. static function onEnterFrame(event : Event)
  880. {
  881. LoadingBar.graphics.beginFill(0x0000FF);
  882. LoadingBar.graphics.drawRect(1,1,(Lib.current.root.loaderInfo.bytesLoaded/Lib.current.root.loaderInfo.bytesTotal)*498,48);
  883. LoadingBar.graphics.endFill();
  884. if (Lib.current.root.loaderInfo.bytesLoaded >= Lib.current.root.loaderInfo.bytesTotal)
  885. {
  886. Lib.current.root.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
  887. // Lib.current.gotoAndStop("start");
  888. // new Images();
  889. removeLoadingBar();
  890. try
  891. {
  892. var mainInstance:Main = new Main();
  893. }
  894. catch (err:flash.Error)
  895. {
  896. trace(err.message);
  897. }
  898. }
  899. }
  900. private static var LoadingBar:Sprite;
  901. public static function removeLoadingBar()
  902. {
  903. flash.Lib.current.removeChild(LoadingBar);
  904. }
  905. public static function addLoadingBar()
  906. {
  907. LoadingBar = new Sprite();
  908. LoadingBar.graphics.beginFill(0x000000);
  909. LoadingBar.graphics.drawRect(0,0,500,50);
  910. LoadingBar.graphics.endFill();
  911. flash.Lib.current.addChild(LoadingBar);
  912. LoadingBar.x = 50; LoadingBar.y = 225;
  913. }
  914. static function main() {
  915. // Log.trace("Starting preloader");
  916. addLoadingBar();
  917. Lib.current.root.addEventListener(Event.ENTER_FRAME, onEnterFrame);
  918. // Create the request object...
  919. // var loader = new flash.display.Loader();
  920. // // When the image is ready, instanciate the game class...
  921. // loader.contentLoaderInfo.addEventListener(flash.events.Event.COMPLETE,
  922. // function(_) { new Main(untyped loader.content.bitmapData); });
  923. // // Fire off the request and wait...
  924. // loader.load(new flash.net.URLRequest("img/file.gif"));
  925. //new Main();
  926. }
  927. }