topical media & game development

talk show tell print

student-twitter-swarm-Tree.ax

student-twitter-swarm-Tree.ax (swf ) [ flash ] flex


  package {
          import flash.filters.ColorMatrixFilter;
          
          import mx.controls.Image;
          
          public class @ax-student-twitter-swarm-Tree {
                  private var x:int, y:int;
                  private var radius:int = 75, mood:String;
                  public var image:Image;
                  
                  public function @ax-student-twitter-swarm-Tree (x:int, y:int, image:Image, mood:String):void {
                          this.x = x;
                      this.y = y;
                      
                      this.image = image;
                      this.image.x = x - 75;
                      this.image.y = y - 75;
                      
                      this.mood = mood;
                      setColor(mood);
                  }
                  
                  public function getX ():int {
                          return this.x;
                  }
                  public function getY ():int {
                          return this.y;
                  }
                  public function getRadius ():int {
                          return this.radius;
                  }
                  public function getImage ():Image {
                          return this.image;
                  }
                  public function getMood ():String {
                          return this.mood;
                  }
                  
                  public function distanceToBoid (boid:student_twitter_swarm_Boid):int {
                          var distX:int = this.x - boid.getX(),
                                  distY:int = this.y - boid.getY();
                          return Math.sqrt(distX * distX + distY * distY);
                  }
                  
                  public function shake (boids:Array):void {
                          // Find all boids within the tree radius and give them a random impulse.
                          // The result is that they scatter.
  
                          for(var i:int = 0; i < boids.length; i++) {
                                  if (this.distanceToBoid(boids[i]) <= this.radius) {
                                          boids[i].addRandomImpulse (10.0);
                                  }
                          }
                  }
                  
                  public function setColor (mood:String):void {
                          // Sets the color of the filter to the given color.
  
                          var color:Array = [];
                          color[color.length] = ["happy", 0.3, 1.0, 0.3];
                          color[color.length] = ["sad",         0.2, 0.2, 1.0];
                          color[color.length] = ["angry", 0.9, 0.9, 0.0];
                          color[color.length] = ["tired", 0.8, 0.8, 0.8];
                          color[color.length] = ["love",         1.0, 0.2, 0.2];
                          
                          for(var i:int = 0; i < color.length; i++) {
                                  if (mood == color[i][0]) {
                                          setFilter(color[i][1], color[i][2], color[i][3]); 
                                  }
                          }
                  }
                  private function setFilter (red:Number, green:Number, blue:Number): void {
                          // Filters the image back to the color that the boid was initialised with.
                          
                          this.image.filters = []
  
                          // channel Red, Green, Blue and Alpha
                          var bwMatrix: Array = [        red, 0, 0, 0, 0,
                                                                          0, green, 0, 0, 0,
                                                                          0, 0, blue, 0, 0,
                                                                          0, 0, 0, 1, 0];
                          
                          var filter:ColorMatrixFilter = new ColorMatrixFilter (bwMatrix); 
                          this.image.filters = [filter]; 
                  }
          }
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.