所以我一般来说是 AS3 和 AS 的新手。我确实在一定程度上了解 Java,并且非常了解 PHP。
我正在尝试通过编写一个以编程方式创建十六进制地图的应用程序来学习。我正在使用一个六边形库,我将在下面包含它,但是这是我的问题。当我运行一个 for 循环,比如说 10 个计数,然后我创建一个形状,添加形状并绘制形状,它没有问题。在同一个循环中,如果我将形状推送到数组,然后添加该数组元素,然后绘制它,我什么也得不到。
package
{
import flash.display.MovieClip;
import flash.display.Shape;
import flash.text.TextField;
public class Main extends MovieClip
{
private var radius:Number = 30;
private var sides:Number = 6;
private var myrotation:Number = 0;
private var lineColor:Number = 0x000000;
private var lineThickness:Number = 1;
private var l:TextField = new TextField();
private var f:Array = new Array(20);
public function Main()
{
l.text = "test";
addChild(l);
//WORKS should be red
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++)
{
var t:Polygon = new Polygon();
addChild(t);
t.drawPolygon(radius,sides,2*radius*j,radius*2,0xFF0000,lineThickness,myrotation);
}
//DOES NOT WORK (note the *3 is so it is lower down) should be blue
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++)
{
f.push(new Polygon());
addChild(f[j]);
f[j].drawPolygon(radius,sides,2*radius*j,radius*3,0x0000FF,lineThickness,myrotation);
}
}
}
}
我正在使用的多边形类如下。我从某处的互联网上抓取了它。
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Polygon extends MovieClip
{
//PROPERTIES
private var points:Array;
private var id:int;
private var ratio:Number;
private var top:Number;
private var fade_value:int;
//CONSTRUCTOR
public function Polygon()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
stage.frameRate=31;
}
//METHODS
public function drawPolygon(radius:int,segments:int,centerX:Number,centerY:Number,tint:uint,line:int,rotating:Number):void
{
id=0;
points=new Array();
ratio=360/segments;
top=centerY-radius;
for(var i:int=rotating;i<=360+rotating;i+=ratio)
{
var xx:Number=centerX+Math.sin(radians(i))*radius;
var yy:Number=top+(radius-Math.cos(radians(i))*radius);
points[id]=new Array(xx,yy);
if(id>=1)
{
drawing(points[id-1][0],points[id-1][1],points[id][0],points[id][1],tint,line);
}
id++;
}
id=0;
}
private function radians(n:Number):Number
{
return(Math.PI/180*n);
}
private function drawing(startX:Number,startY:Number,stopX:Number,stopY:Number,tint:Number,line:int):void
{
graphics.lineStyle(line,tint,1);
graphics.moveTo(startX,startY);
graphics.lineTo(stopX,stopY);
}
public function fadeOut():void
{
fade_value=0;
addEventListener(Event.ENTER_FRAME,fade);
}
public function fadeIn():void
{
fade_value=1;
addEventListener(Event.ENTER_FRAME,fade);
}
//PRIVATE
private function fade(evt:Event):void
{
var da:Number=fade_value-alpha;
var aa:Number=da*.1;
alpha+=aa;
if(Math.abs(da)<=.05)
{
alpha=fade_value;
removeEventListener(Event.ENTER_FRAME,fade);
if(fade_value==0)
dispatchEvent(new Event('fadeOutDone'));
else
dispatchEvent(new Event('fadeInDone'));
}
}
}
}
----EDIT---- 代码现在如下,出现同样的问题。我尝试添加一个向量并尝试转换数组元素。包 { 导入 flash.display.MovieClip; 导入 flash.display.Shape;导入 flash.text.TextField;
public class Main extends MovieClip
{
private var radius:Number = 30;
private var sides:Number = 6;
private var myrotation:Number = 0;
private var lineColor:Number = 0x000000;
private var lineThickness:Number = 1;
private var l:TextField = new TextField();
private var f:Array = new Array(20);
private var v:Vector.<Polygon> = new Vector.<Polygon>(10);
public function Main()
{
l.text = "test";
addChild(l);
//WORKS should be red
for(var i:int = 0; i<stage.stageWidth/(radius*2);i++)
{
var t:Polygon = new Polygon();
addChild(t);
t.drawPolygon(radius,sides,2*radius*i,radius*2,0xFF0000,lineThickness,myrotation);
}
//DOES NOT WORK (note the *3 is so it is lower down) should be blue
for(var j:int = 0; j<stage.stageWidth/(radius*2);j++)
{
f.push(new Polygon());
addChild(f[j]);
Polygon(f[j]).drawPolygon(radius,sides,2*radius*j,radius*3,0x0000FF,lineThickness,myrotation);
}
for(var k:int = 0; k<stage.stageWidth/(radius*2);k++)
{
v.push(new Polygon());
addChild(v[k]);
v[k].drawPolygon(radius,sides,2*radius*k,radius*3.5,0x00FF00,lineThickness,myrotation);
}
}
}
}
整理笔记...
加载配置文件 C:\Program Files (x86)\Adobe\Adobe Flash Builder 4.6\sdks\4.6.0\frameworks\flex-config.xml 初始设置:64ms 开始加载 swcs 14ms 运行总计:78ms 加载 30 个 SWC:484ms 预编译: 841ms C:\Users\jmasiello.place\Documents\flash\Main.as: 警告:此编译单元没有在 Frame 元数据中指定 factoryClass 来加载配置的运行时共享库。要在没有运行时共享库的情况下进行编译,请将 -static-link-runtime-shared-libraries 选项设置为 true 或删除 -runtime-shared-libraries 选项。
文件:145 时间:961ms 链接... 25ms 优化... 26ms SWF 编码... 11ms C:\Users\jmasiello.place\Documents\flash\Main.swf (1809 bytes) 后编译:65ms 总时间:1595ms 峰值内存使用:47 MB(堆:29,非堆:18)