Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 10th, 2010 | Syntax: ActionScript 3 | Size: 1.62 KB | Hits: 28 | Expires: Never
Copy text to clipboard
  1.  The dispatching Class
  2.  
  3.  
  4. package Classes.Scoreboard
  5. {
  6.         import Classes.Events.*;
  7.         import flash.display.MovieClip;
  8.         import flash.events.Event;
  9.         import flash.events.EventDispatcher;
  10.        
  11.         /**
  12.          * ...
  13.          * @author Niels van Aken
  14.          */
  15.         public class Counter extends MovieClip
  16.         {
  17.                
  18.                 public var score:Number = 0;
  19.                
  20.                 public function Counter()
  21.                 {
  22.                         this.addEventListener("CHANGINGSCORE", testfunc);
  23.                 }
  24.                
  25.                 private function testfunc(e:Event) {
  26.                         trace("Schiet mij maar lek");
  27.                 }
  28.                
  29.                 public function addToScore(count:Number):void {
  30.                         if (!isNaN(count)) {
  31.                                 if (count < 0) {
  32.                                         Math.abs(count);
  33.                                 }
  34.                                 this.score += count;
  35.                                 dispatchEvent(new ScoreChangeEvent("CHANGINGSCORE", true));
  36.                                 trace ('COUNTER: Adding to score, dispatching event');
  37.                         }
  38.                 }
  39.                
  40.                 public function substractFromScore(count:Number):void {
  41.                         if (!isNaN(count)) {
  42.                                 if (count < 0) {
  43.                                         Math.abs(count);
  44.                                 }
  45.                                 this.score -= count;
  46.                                 dispatchEvent(new ScoreChangeEvent("CHANGINGSCORE", true));
  47.                                 trace ('COUNTER: Substract from score, dispatching event');
  48.                         }
  49.                 }
  50.                
  51.         }
  52.  
  53. }
  54.  
  55.  I've used a custom class (which isn't custom at all actually, but just to be able to debug a little bit)
  56.  
  57. package Classes.Events
  58. {
  59.         import flash.events.Event;
  60.  
  61.         /**
  62.          * ...
  63.          * @author Niels van Aken
  64.          */
  65.         public class ScoreChangeEvent extends Event
  66.         {
  67.                 public function ScoreChangeEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false)  
  68.                 {
  69.                         super(type, bubbles, cancelable);
  70.                 }
  71.                 public override function clone():Event
  72.                 {
  73.                         return new ScoreChangeEvent( type, bubbles, cancelable );
  74.                 }
  75.         }
  76.  
  77. }