Hello mates, I have a problem with this code. I am trying to add a new node to the graph by clicking a button, Everything looks normal, but the method getPath() doesn’t run correctly and the route can’t be calculated. Maybe I’m missing something, I need help!
I attach the whole .zip
package
{
import __AS3__.vec.Vector;
import flash.display.Sprite;
import flash.events.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.*;
import flash.utils.getTimer;
import structs.graphs.Dijkstra;
import structs.graphs.Grafo;
import structs.graphs.Vertice;
public class GGrafo extends Sprite
{
private var _graph:Grafo;
private var _vertices:Vector.<GVertice>;
private var _edges:GLink;
private var _items:uint;
private var _src:Vertice;
private var _dst:Vertice;
private var _text:Textos;
private var _time:Number;
private var nodes:Array;
private var edges:Array;
private var vertex:GVertice;
private var edge:GLink;
private var pesoLabel:TextField;
private var pushMe:TextField;
private var pintar:Sprite;
private var square:Sprite;
private var xInput:TextField;
private var yInput:TextField;
private var pInput:TextField;
private var withInput:TextField;
private var xLabel:TextField;
private var yLabel:TextField;
private var conLabel:TextField;
private var posLabel:TextField;
private var strx:String;
private var stry:String;
private var strp:String;
private var strc:String;
private var boldText:TextFormat = new TextFormat();
public function GGrafo()
{
addEventListener( Event.ADDED_TO_STAGE, init );
}
private function init( e:Event ):void
{
_graph = new Grafo();
_edges = new GLink();
_vertices = new Vector.<GVertice>();
addChild( _edges );
_items = 0;
nodes = [ [50, 250], [200, 125], [200, 300], [330, 205],
[315, 350], [445, 125], [440, 235], [440, 345],
[535, 235], [535, 345], [400, 495], [300, 495], [250, 400] ];
edges = [ [0, 1], [0, 2], [1, 2], [1, 3], [1, 5], [2, 3], [2, 4],
[2, 12], [3, 5], [3, 6], [3, 7], [4, 7], [4, 10], [5, 6],
[5, 8], [6, 7], [6, 8], [6, 9], [7, 9], [7, 10],[8, 9], [10, 11], [11, 12]];
for ( var i:int = 0; i < nodes.length; ++i )
{
addChild( vertex = new GVertice( "v" + i ) );
vertex.x = nodes[ i ][ 0 ];
vertex.y = nodes[ i ][ 1 ];
_vertices.push( vertex );
_graph.addVertex( new Vertice( "v" + i ) );
}
for ( i = 0; i < edges.length; ++i )
{
var w:int = 1 + Math.random() * 20;
_edges.connect( _vertices[ edges[ i ][ 0 ] ], _vertices[ edges[ i ][ 1 ] ], w );
_graph.addEdge( _graph.vertices.getNode( edges[ i ][ 0 ] ).data,
_graph.vertices.getNode( edges[ i ][ 1 ] ).data, w );
}
cuadradito();
addChild( _text = new Textos() );
_text.x = _text.y = 10;
removeEventListener( Event.ADDED_TO_STAGE, init );
addEventListener( MouseEvent.CLICK, selectVertex );
buttonMode = true;
useHandCursor = true;
}
private function reset():void
{
for ( var i:int = 0; i < _vertices.length; ++i )
{
_vertices[ i ].selected = false;
_vertices[ i ].marked = false;
_vertices[ i ].render();
}
_items = 0;
}
private function selectVertex( e:MouseEvent ):void
{
if ( e.target is GVertice )
{
if ( _items == 0 )
{
_edges.reset();
reset();
}
var id:int;
var target:GVertice = e.target as GVertice;
target.marked = true;
target.render();
if ( _items < 2 )
{
if ( _items == 0 )
{
_src = _graph.vertices.getNode( getChildIndex( target ) - 1 ).data;
//_text.setSourceName( _src.name );
}
else if ( _items == 1 )
{
_dst = _graph.vertices.getNode( getChildIndex( target ) - 1 ).data;
//_text.setDestName( _dst.name );
}
_items++;
}
if ( _items == 2 )
{
reset();
getPath();
}
}
}
private function getPath():void
{
_time = getTimer();
var d:Dijkstra = new Dijkstra( _graph, _src, _dst );
var res:Vertice = d.search();
var w:int = res.weight;
while ( res )
{
if ( res.parent )
{
var rid:int = int( res.name.split( "v" )[ 1 ] );
var pid:int = int( res.parent.name.split( "v" )[ 1 ] );
_vertices[ rid ].selected = true;
_vertices[ rid ].render();
_vertices[ pid ].selected = true;
_vertices[ pid ].render();
_edges.connect( _vertices[ rid ], _vertices[ pid ], 0, true );
}
res = res.parent;
}
_text.setResult( String( w ) );
//_text.setExecTime( String( getTimer() - _time ) );
}
public function cuadradito():void
{
square= new Sprite();
addChild(square);
square.graphics.lineStyle(3,0x99cccc);
square.graphics.beginFill(0xFCFCFC);
square.graphics.drawRoundRect(200,200,120,120,20);
square.graphics.endFill();
square.x = stage.stageWidth/2-square.width/2;
square.y = stage.stageHeight/2-square.height/2;
square.mouseChildren = false;
xInput=new TextField();
xInput.text = " ";
xInput.type = TextFieldType.INPUT;//use TextFieldType.INPUT property to edit it in runtime
xInput.x=535;
xInput.y=442;
xInput.width=30;
xInput.height=16;
xInput.border=true;
yInput=new TextField();
yInput.text = " ";
yInput.type = TextFieldType.INPUT;//use TextFieldType.INPUT property to edit it in runtime
yInput.x=535;
yInput.y=461;
yInput.width=30;
yInput.height=16;
yInput.border=true;
pInput=new TextField();
pInput.text = " ";
pInput.type = TextFieldType.INPUT;//use TextFieldType.INPUT property to edit it in runtime
pInput.x=535;
pInput.y=480;
pInput.width=30;
pInput.height=16;
pInput.border=true;
withInput=new TextField();
withInput.text = "";
withInput.type = TextFieldType.INPUT;//use TextFieldType.INPUT property to edit it in runtime
withInput.x=535;
withInput.y=499;
withInput.width=30;
withInput.height=16;
withInput.border=true;
boldText.bold = true;
xLabel=new TextField();
xLabel.text = "X:";
xLabel.x=493;
xLabel.y=443;
xLabel.width=30;
xLabel.height=16;
xLabel.textColor=0x660033;
xLabel.setTextFormat(boldText);
yLabel=new TextField();
yLabel.text = "Y:";
yLabel.x=493; ;
yLabel.y=463;
yLabel.width=30;
yLabel.height=16;
yLabel.textColor=0x660033;
yLabel.setTextFormat(boldText);
pesoLabel=new TextField();
pesoLabel.text = "PESO:";
pesoLabel.x=493; ;
pesoLabel.y=483;
pesoLabel.width=40;
pesoLabel.height=16;
pesoLabel.textColor=0x660033;
pesoLabel.setTextFormat(boldText);
conLabel=new TextField();
conLabel.text = "CON:";
conLabel.x=493; ;
conLabel.y=503;
conLabel.width=40;
conLabel.height=16;
conLabel.textColor=0x660033;
conLabel.setTextFormat(boldText);
posLabel=new TextField();
posLabel.type = TextFieldType.DYNAMIC;
posLabel.x=403;
posLabel.y=533;
posLabel.width=80;
posLabel.height=16;
posLabel.textColor=0x660033;
posLabel.setTextFormat(boldText);
addEventListener(MouseEvent.MOUSE_OVER, mousePosition);
//2.
function mousePosition(event:MouseEvent) {
posLabel.text= "X: " + String(mouseX);
posLabel.appendText(" Y: " + String(mouseY));
//xmovie_txt.text= "X MovieClip: " + String(box_mc.mouseX);
//ymovie_txt.text= "Y MovieClip: " + String(box_mc.mouseY);
}
addChild(posLabel);
addChild(xInput);
addChild(yInput);
addChild(pInput);
addChild(xLabel);
addChild(yLabel);
addChild(pesoLabel);
addChild(conLabel);
addChild(withInput);
pushMe=new TextField()
pushMe.text = "AGREGAR!";
pushMe.x = 225;
pushMe.y = 290;
pushMe.textColor=0xffffff;
pushMe.selectable=false;
pintar=new Sprite(); //boton AGREGAR
addChild(pintar);
pintar.graphics.lineStyle(3,0x99cccc);
pintar.graphics.beginFill(0xCC6699);
pintar.graphics.drawRoundRect(215,284,90,30,20);
pintar.graphics.endFill();
pintar.x = stage.stageWidth/2-square.width/2;
pintar.y = stage.stageHeight/2-square.height/2;
//pintar.useHandCursor=true;
pintar.buttonMode=true;
square.mouseChildren = false;
pintar.addChild(pushMe);
pintar.addEventListener(MouseEvent.CLICK, buttonClickHandler);
pintar.addEventListener(MouseEvent.MOUSE_OVER, buttonOverHandler);
pintar.addEventListener(MouseEvent.MOUSE_OUT, buttonOutHandler);
}
public function buttonClickHandler(event:MouseEvent):void
{
strx= xInput.text;
stry= yInput.text;
strp= pInput.text;
strc= withInput.text;
var posX:int=int(strx);
var posY:int=int(stry);
var Peso:int=int(strp);
var con:int=int(strc);
nodes.push( [posX, posY]); //posicion
var indi:int=nodes.length-1;
edges.push([14,con]); //cual con cual
vertex = new GVertice( "v" + indi);
vertex.x = nodes[ indi ][ 0 ];
vertex.y = nodes[ indi ][ 1 ];
_vertices.push( vertex );
_graph.addVertex( new Vertice( "v" + indi ) );
var indice2:int=edges.length-1;
_edges.connect( _vertices[ edges[ indice2 ][ 0 ] ], _vertices[ edges[ indice2 ][ 1 ] ], Peso );
_graph.addEdge( _graph.vertices.getNode( edges[ indice2 ][ 0 ] ).data,
_graph.vertices.getNode( edges[ indice2 ][ 1 ] ).data, Peso );
function TODO(){}
addChild(vertex);
addEventListener( MouseEvent.CLICK, selectVertex );
buttonMode = true;
useHandCursor = true;
}
}
}