AS3对象阵列未添加到舞台

时间:2012-10-01 19:52:21

标签: actionscript-3

所以我是AS3和AS的新手。我非常了解Java程度和PHP。

我正在尝试通过编写一个以编程方式创建十六进制映射的应用程序来学习。我正在使用六边形库,我将在下面包含,但这是我的问题。 当我为一个10循环运行一个for循环,然后我创建一个形状,添加形状并绘制形状,它没有问题。如果我将一个形状推送到一个数组,然后添加该数组元素,然后绘制它,我什么也得不到。

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 现在代码如下,同样的问题发生了。我试过添加一个矢量,我试着投射数组元素。     包     {         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);
        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)\ Adob​​e \ Adob​​e Flash Builder 4.6 \ SDK的\ 4.6.0 \框架\柔性-config.xml中 初始设置:64ms 开始加载swcs 14ms运行总计:78ms 加载30个SWC:484ms 预编译:841ms C:\ Users \ jmasiello.place \ Documents \ flash \ Main.as:警告:此compila tion单元没有在Frame元数据中指定的factoryClass来加载co nfigured运行时共享库​​。没有运行时共享库​​的编译e ither将-static-link-runtime-shared-libraries选项设置为true或删除  -runtime-shared-libraries选项。

档案:145时间:961毫秒 链接... 25ms 优化... 26ms SWF编码... 11ms C:\ Users \ jmasiello.place \ Documents \ flash \ Main.swf(1809 bytes) 后编译:65ms 总时间:1595ms 峰值内存使用量:47 MB​​(堆:29,非堆:18)

2 个答案:

答案 0 :(得分:2)

你做什么

private var f:Array = new Array(20);

您在工具中创建一个包含20个元素的数组 然后你将元素推入同一个数组 f.push(new Polygon()); 通过推动元素添加到最后一个元素
这使得数组的前20个元素为null,第21个元素为多边形 您甚至可以通过执行验证。

for(var j:int = 0; j<stage.stageWidth/(radius*2);j++){
    f.push(new Polygon());
    trace(f)
}   

因此,在您的代码中,您将进入第21个元素,然后通过

访问数组
addChild(f[j]);

其中j = 0且0中的元素仍然为空 然后解决问题的最佳方法是

private var f:Array = new Array();

答案 1 :(得分:0)

你有任何错误吗?您可能需要转换为Polygon,以便编译器接受drawPolygon方法。

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;
} 

要跳过转换,您可以使用Vector而不是Array,因为编译器会知道它只处理多边形。