Zeether

Navigation events

Feb 9th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // a navigation event
  2. package
  3. {
  4.     import flash.events.Event;
  5.     public class NavigationEvent extends Event
  6.     {
  7.         public static const RESTART:String = "restart"; // defining a constant for RESTART
  8.         public static const START:String = "start"; // constant for START
  9.         public static const MENUSCREEN:String = "menu"; // constant for MENU
  10.        
  11.         public function NavigationEvent(type:String)
  12.         {
  13.             super(type);
  14.         }
  15.     }
  16. }
  17.  
  18. // below is the code for the Game Over screen, the restart button works and calls the navigation event, but the onClickMenu function gives a compiler error "TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::MouseEvent@271aa089 to NavigationEvent."
  19.  
  20. // Game Over screen class
  21. package
  22. {
  23.     import flash.display.MovieClip;
  24.     import flash.display.SimpleButton;
  25.     import flash.events.MouseEvent;
  26.     import flash.text.TextField;
  27.     import flash.ui.Mouse;
  28.    
  29.     public class GameOverScreen extends MovieClip
  30.     {
  31.         public function GameOverScreen()
  32.         {
  33.             Mouse.show(); // show mouse cursor
  34.             restartButton.addEventListener( MouseEvent.CLICK, onClickRestart ); // add click listener to button
  35.             menuButton.addEventListener( MouseEvent.CLICK, onClickMenu); // add menu button listener
  36.         }
  37.        
  38.         public function onClickRestart(mouseEvent:MouseEvent):void // restarts the game when the restart button is clicked!
  39.         {
  40.             dispatchEvent(new NavigationEvent(NavigationEvent.RESTART));
  41.         }
  42.        
  43.         public function setFinalScore(scoreValue:Number):void // sets the final score display
  44.         {
  45.             finalScore.text = scoreValue.toString();
  46.         }
  47.        
  48.         public function onClickMenu(navigationEvent:NavigationEvent):void // brings you back to the menu!
  49.         {
  50.             dispatchEvent(new NavigationEvent(NavigationEvent.MENUSCREEN));
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment