A星算法,flash版

A星算法,flash 演示版

 

package { import flash.display.Sprite; import flash.events.MouseEvent; [SWF(width="550",height="550",backgroundColor="#999999")] public class astart extends Sprite { // 常数定义 private static const size:int = 50; private var points:Array = new Array(); private var bg:Sprite = new Sprite(); private var person:Sprite = null; private var line:Sprite = new Sprite(); public function astart() { var i:int = 0; // 填充颜色 bg.x = bg.y = 25; bg.graphics.beginFill(0xff0000,1); bg.graphics.drawRect(0,0,500,500); bg.graphics.endFill(); // 画格子线 bg.graphics.lineStyle(1,0); for ( i=0;i<=size;i++ ) { bg.graphics.moveTo(0,500/size*i); bg.graphics.lineTo(500,500/size*i); bg.graphics.moveTo(500/size*i,0); bg.graphics.lineTo(500/size*i,500); } // 初始化格子 for ( i = 0;i= size || px < 0 || py >= size || py < 0 ) continue; if ( points[px][py] == 1 ) continue; if ( locks[px + "-" + py] ) continue; if ( unlocks[px + "-" +py] ) continue; if ( i * j != 0 ) { if ( points[point.x][py] == 1 || points[px][point.y] == 1 ) continue; } var obj:Object = { x:px,y:py,parent:point }; obj.h = Math.abs(targetX - px)+Math.abs(targetY - py); obj.g = point.g+(i*j==0?10:14); obj.f = obj.h + obj.g; unlocks.push(obj); unlocks[px+"-"+py] = obj; locks[px+"-"+py] = obj; if ( px == targetX && py == targetY ) { endObj = obj; break; } } if ( endObj != null || unlocks.length == 0 ) break; unlocks.sortOn("f", Array.NUMERIC); } if ( endObj != null ) { var pre:Object = endObj.parent; line.graphics.lineStyle(2,0xffffff); line.graphics.moveTo((endObj.x+0.5)*500/size,(endObj.y+0.5)*500/size); while ( pre != null ) { line.graphics.lineTo((pre.x+0.5)*500/size,(pre.y+0.5)*500/size); pre = pre.parent; } person.x = targetX*500/size + 25; person.y = targetY*500/size + 25; } bg.mouseEnabled = true; } } }

你可能感兴趣的:(flash/flex,flash,算法,object,null,function,import)