topical media & game development
actionscript-graph-basic-CharacterHandler.ax
actionscript-graph-basic-CharacterHandler.ax
[swf]
flex
package {
//import ConceptGraphBasic.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-basic-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;
public function @ax-actionscript-graph-basic-CharacterHandler (charData:Array, arrayRelations:Array):void {
this.arrayRelations = arrayRelations;
for (var j:int=0; j<charData.length; j++) { //Add all the characters to arrayCharacter and allCharacters
var character:actionscript_graph_basic_Character = new actionscript_graph_basic_Character(radius, charData[j].id, charData[j].imageURL);
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:actionscript_graph_basic_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:actionscript_graph_basic_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:actionscript_graph_basic_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:actionscript_graph_basic_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:actionscript_graph_basic_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:actionscript_graph_basic_Relation = new actionscript_graph_basic_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(character:actionscript_graph_basic_Character): void {
var tempArray:Array = new Array();
var characterClicked:actionscript_graph_basic_Character;
characterClicked=character;
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:actionscript_graph_basic_Relation = arrayRelations[i];
if (relation.getFromID() == character.getId()) { //If the relation is between the clicked character...
var possibleCharacter:actionscript_graph_basic_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(actionscript_graph_basic_Character(ev.target)));
container.addChildAt(actionscript_graph_basic_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(actionscript_graph_basic_Character(ev.target), 1); //Place the character where it was situated
}
public function onPress(ev:MouseEvent):void {
click(actionscript_graph_basic_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.