如何在动作脚本3中绘制网格

时间:2011-02-19 02:01:37

标签: flash actionscript-3 actionscript

有没有简单的方法来绘制网格?

是否可以通过画线来做到这一点?

2 个答案:

答案 0 :(得分:4)

不要忘记你需要添加孩子(网格);所以它会显示出来,显然你需要从某个地方调用该函数。

    private var grid:Sprite = new Sprite();
    private var numColumns:Number = 10;
    private var numRows:Number = 10;
    private var cellHeight:Number = 40;
    private var cellWidth:Number = 80;

    private function drawGrid():void 
    {
        grid.graphics.clear();
        grid.graphics.lineStyle(1, 0x000000);

        // we drop in the " + 1 " so that it will cap the right and bottom sides.
        for (var col:Number = 0; col < numColumns + 1; col++)
        {
            for (var row:Number = 0; row < numRows + 1; row++)
            {
                trace(col, row);
                grid.graphics.moveTo(col * cellWidth, 0);
                grid.graphics.lineTo(col * cellWidth, cellHeight * numRows);
                grid.graphics.moveTo(0, row * cellHeight);
                grid.graphics.lineTo(cellWidth * numColumns, row * cellHeight);
            }
        }

    }

我更新了上面的代码以允许变量单元格大小,并添加了另一种方法(下面)来获得相同的东西。下面的代码是自包含的,所以你没有任何变量,除了显示网格的Sprite。

    /**
     * Draws a Grid with variable width and height to the supplied Sprite Object.
     * @param   numColumns      Number of columns in the grid.
     * @param   numRows         Number of rows in the grid.
     * @param   cellHeight      Cell height of the grid.
     * @param   cellWidth       Cell width of the grid.
     * @param   grid            Sprite Object that will be drawn to.
     */
    private function drawGrid(numColumns:Number, numRows:Number, cellHeight:Number, cellWidth:Number, grid:Sprite):void 
    {
        grid.graphics.clear();
        grid.graphics.lineStyle(1, 0x000000);

        // we drop in the " + 1 " so that it will cap the right and bottom sides.
        for (var col:Number = 0; col < numColumns + 1; col++)
        {
            for (var row:Number = 0; row < numRows + 1; row++)
            {
                trace(col, row);
                grid.graphics.moveTo(col * cellWidth, 0);
                grid.graphics.lineTo(col * cellWidth, cellHeight * numRows);
                grid.graphics.moveTo(0, row * cellHeight);
                grid.graphics.lineTo(cellWidth * numColumns, row * cellHeight);
            }
        }

    }

答案 1 :(得分:0)

您可以查看的一件事是Flex charting libraries

您还可以查看其中一些Flash solutions。列表中包含AnyChart,其中包含一些不错的图表和功能。