topical media & game development

talk show tell print

actionscript-graph-medium-CharacterHandler.ax

actionscript-graph-medium-CharacterHandler.ax [swf] flex


  package ConceptGraphMedium {
          import ConceptGraphMedium.Character;
          import flash.display.*;        
          import flash.filters.*
          import flash.net.NetConnection;
          import flash.net.NetStream;
          import flash.media.Video;
          import flash.text.TextFieldAutoSize;
          import flash.text.TextField;
            import flash.events.Event;
          import flash.events.MouseEvent;
          import flash.geom.Transform;
          import flash.geom.Matrix;
          public class @ax-actionscript-graph-medium-CharacterHandler        extends Sprite{
                  private var characterHandler:Array = new Array()                //Array with the updated index of the elements in arrayCharacter
                  private var allCharacters:Array = new Array;                        //Array with all the character objects contained in the XML File (charData)
                  private var arrayCharacter:Array = new Array;                        //Array containing the character Object (arrayCharacter.object) and the quality of the relation (arrayCharacter.relation)
                  private var arrayRelations:Array = new Array;                        //Contains relations among persons: fromID, toID, quality
                  private var subRelation:Array = new Array;
                  private var container:Sprite = new Sprite();
                  private var originX:uint = 400;                                                        //Set the X center (where the clicked character is set)
                  private var originY:uint = 365;                                                        //Set the Y center (where the clicked character is set)
                  private var affineTransform:Matrix;
                  private var distanceBetweenTurns:uint = 70;
                  private var radiusCircle:uint = 100;
                  private var radius:uint = 50;
                  private var numCharactersFirstTurn:uint = 15;                        //Minimum 3
                  private var increaseCharactersFactor:uint = 3;
                  private var selectedLabel:TextField;
                  private var descriptionLabel:TextField;        
                  private var ns:NetStream;        
                  public function @ax-actionscript-graph-medium-CharacterHandler (charData:Array, arrayRelations:Array, selectedLabel:TextField, descriptionLabel:TextField, ns:NetStream):void {
                          this.arrayRelations = arrayRelations;                        
                          this.selectedLabel=selectedLabel;
                          this.descriptionLabel=descriptionLabel;
                          this.ns = ns;
                          for (var j:int=0; j<charData.length; j++) {                //Add all the characters to arrayCharacter and allCharacters
                                  var character:Character = new Character(radius, charData[j].id, charData[j].imageURL, charData[j].nameChar, charData[j].description, charData[j].videoURL); 
                                  trace(charData[j].nameChar);
                                  trace(charData[j].description);
                                  allCharacters.push(character);
                                  arrayCharacter.push(character);
                                  addFigure(arrayCharacter[j], originX, originY, j);                                //Add all the figures of arrayCharacter to the container                                
                          }
                          addEventListener(Event.ENTER_FRAME, onEnterFrame);                        //Activate the movement
                          click(arrayCharacter[0]);                                        //We click on the first item 
                  }
                  public function addFigure(character:Character, x:int, y:int, index:int):void {
                          character.addEventListener(MouseEvent.MOUSE_UP, onPress);                //Handle event when click on one character
                          character.addEventListener(MouseEvent.MOUSE_OVER, overCharacter);                //Handle event when the mouse is over one character
                          character.addEventListener(MouseEvent.MOUSE_OUT, outCharacter);                //Handle event when the mouse is out one character (previously over)
                          character.setX(x);                        //Set the initial coordinate X of the character
                          character.setY(y);                        //Set the initial coordinate Y of the character
                          characterHandler.push(index);                //The index of the actual character is added to the characterHandler array
                          container.addChild(character);                //Add the character to the container
                  }
                  public function getContainer() :Sprite {
                          return container;
                  }
                  private function onEnterFrame(event:Event):void {
                          container.graphics.clear();
                          //Lines to connect the character in the center with the rest of the characters
                          container.graphics.lineStyle(3);
                          for (var i:int = 1; i<arrayCharacter.length; i++) {
                                  container.graphics.moveTo(arrayCharacter[0].getX(),arrayCharacter[0].getY());
                                  container.graphics.lineTo(arrayCharacter[i].getX(),arrayCharacter[i].getY());                                
                          }
                          //Curves to indicate the subrelations among outer characters
                          container.graphics.lineStyle(1);
                          for (var j:int = 0; j<subRelation.length; j++) {
                                  var relation:Relation = subRelation[j];                
                                  container.graphics.moveTo(arrayCharacter[Number(relation.getFromID())].getX(),arrayCharacter[Number(relation.getFromID())].getY())
                                  container.graphics.curveTo((arrayCharacter[Number(relation.getToID())].getX()/2)+160, (arrayCharacter[Number(relation.getToID())].getY()/2)+160, arrayCharacter[Number(relation.getToID())].getX(),arrayCharacter[Number(relation.getToID())].getY());
                          }
                  }
                  //Check the characters in tempArray with the existing in arrayCharacters and update it
                  //The array with the updated characters is arrayCharacter
                  private function checkExistence(tempArray:Array):void  {
                          var isInTheList:Boolean = false;
                          for (var i:int = 0; i<arrayCharacter.length;i++) {
                                  for (var j:int = 0; j< tempArray.length; j++) {
                                          //If the character is in tempArray and in arrayCharcter we remove it from tempArray
                                          if (arrayCharacter[i] == tempArray[j]) {
                                                  arrayCharacter[i] = tempArray[j];
                                                  tempArray.splice(j, 1);
                                                  j--;
                                                  isInTheList = true;
                                          } 
                                  }
                                  //If the character is only in arrayCharacter, and not in tempArray, we remove it from arrayCharacter
                                  if (isInTheList==false) {
                                          container.removeChild(arrayCharacter[characterHandler[i]]);
                                          arrayCharacter.splice(i, 1);
                                          i--;
                                  }
                                  isInTheList = false;
                          }
                          //If there is some character in tempArray and not in arrayCharacter we add them to arrayCharacter from the center (originX originY)
                          for (var k:int =0; k<tempArray.length; k++) {
                                  tempArray[k].setX(originX);
                                  tempArray[k].setY(originY);
                                  arrayCharacter.push(tempArray[k]);
                                  container.addChild(tempArray[k]);
                          }
                          subRelation.splice(0, subRelation.length);
                  }
                  private function characterGeometry():void {
                          var existentRelation:Boolean = false;
                          var angle:Number = 0;
                          var characterOne:Character;
                          var indexTurns:int = 1;
                          var increaseSize:Number = 0                //The size of the circle, the number of turs
                          var currentTurn:int = 1;                //The current number of turns
                          var constantDivisionNum:int;
                          var value:int;
                          arrayCharacter[0].setXWanted(originX);
                          arrayCharacter[0].setYWanted(originY);
                          for (indexTurns; (increaseSize + numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, indexTurns)) < arrayCharacter.length; indexTurns++) {
                                  increaseSize += numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, indexTurns)
                          }
                          //Check for subrelations among characters in arrayCharacters (characterOne and characterTwo)
                          for (var i:int = 1; i<arrayCharacter.length; i++) {
                                  characterOne = arrayCharacter[i];
                                  for (var j:int=0; j< arrayRelations.length; j++) {
                                          var relation:Relation=arrayRelations[j];
                                          for (var k:int = 1; k<arrayCharacter.length; k++) {
                                                  var characterTwo:Object = arrayCharacter[k];
                                                  if (relation.getFromID()==characterOne.getId() && relation.getToID() == characterTwo.getId()) {
                                                          var subrelation:Relation;
                                                          for (var l:int = 0; l<subRelation.length; l++) {
                                                                  subrelation = subRelation[l];
                                                                  if (int(subrelation.getFromID()) == i && int(subrelation.getToID()) == k) {
                                                                          existentRelation=true;
                                                                  }
                                                          }
                                                          if (existentRelation==false) {                //If the subrelation is not already included, we do it
                                                          var newSubrelation:Relation = new Relation(String(k), String(i)); //Subrelations are stored with the index of the arrayCharacter
                                                          subRelation.push(newSubrelation); //Store the subrelation
                                                          }
                                                          existentRelation=false;
                                                  }
                                          }                        
                                  }
                                  for (currentTurn; (i-1)>(value + numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, currentTurn)) || (i-1)==(value + numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, currentTurn)); currentTurn++) {
                                          value += numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, currentTurn)
                                  }
                                  if ((i-1) < increaseSize) { //If we are over increaseSize we divide the angle among the constantDivisionNum
                                          constantDivisionNum = numCharactersFirstTurn - 3 + Math.pow(increaseCharactersFactor, currentTurn);
                                          angle += (360/(constantDivisionNum))*Math.PI/180;
                                  } else {                //Otherwise, we divide it amongst the rest of the characters
                                          angle += 360/(arrayCharacter.length-increaseSize-1)*Math.PI/180;
                                  }
                                  arrayCharacter[i].setXWanted(originX + (((radiusCircle  + (currentTurn * distanceBetweenTurns))*Math.cos(angle))));        //Final destination of the character
                                  arrayCharacter[i].setYWanted(originY + (((radiusCircle  + (currentTurn * distanceBetweenTurns))*Math.sin(angle))));        //Final destination of the character                        
                          }
                  }
                  public function click(characterClicked:Character): void {
                          var tempArray:Array = new Array();
                          selectedLabel.htmlText = "<b>You have selected: </b>" + characterClicked.getName();
                          descriptionLabel.htmlText = "<b>Description: </b>" + characterClicked.getDescription();
                          trace ("esto"+characterClicked.getVideoURL()+"es");
                          if ((characterClicked.getVideoURL()==null) || (characterClicked.getVideoURL()== "")) {
                                  ns.pause();
                          } else {
                                  ns.play(characterClicked.getVideoURL());
                          }
                          for (var i:int = 0; i<arrayCharacter.length; i++) {                //Look for the clicked element and we eliminate it from its position
                                  if (arrayCharacter[i].getId() == characterClicked.getId()) {
                                          arrayCharacter.splice(i, 1);                                                
                                          i--;
                                  }        
                          }
                          arrayCharacter.splice(0,0, characterClicked);                //Store the clicked element in the first position of the array arrayCharacter
                          tempArray.push(characterClicked);                        //Store the clicked element in the tempArray
                          for (i = 0; i<arrayRelations.length; i++) {                //Check the relations with the other characters
                                  var relation:Relation = arrayRelations[i];
                                  if (relation.getFromID() == characterClicked.getId()) {        //If the relation is between the clicked character...
                                          var possibleCharacter:Character;
                                          for (var j:int = 0; j < allCharacters.length; j++) {        //Search in allCharacters the toID character and push it in tempArray
                                                  possibleCharacter = allCharacters[j];
                                                  if (relation.getToID() == possibleCharacter.getId()) {
                                                          tempArray.push(possibleCharacter);
                                                  }
                                          }
                                  }
                          }
                          //arrayCharacter => All the characters when the mouse was clicked
                          //tempArray => All the characters that are related to the clicked character
                          checkExistence(tempArray);         //Update arrayCharacter with tempArray
                          characterGeometry();                        //Set the characters (math and geometry)        
                  }
                  public function overCharacter(ev:MouseEvent):void {
                          ev.target.filters=[new BevelFilter(  )];                //Bevel Filter when the mouse is over the character
                          ev.target.increaseSize(60);                                                //Increase the size
                          ev.target.setIndexDisplay(container.getChildIndex(Character(ev.target)));        
                          container.addChildAt(Character(ev.target), container.numChildren);                                //Display the character in the front
                  }
                  public function outCharacter(ev:MouseEvent):void {
                          ev.target.decreaseSize();                                                                //Decrease size
                          ev.target.filters=[];                                                                        //Eliminate Bevel Filter
                          container.addChildAt(Character(ev.target), 1);                        //Place the character where it was situated
                  }
                  public function onPress(ev:MouseEvent):void {
                          click(Character(ev.target));
                  }
          }
  }


(C) Æliens 27/08/2009

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.