开发者

How do i draw grid in actionscript 3

Is there any simple method to draw a grid?开发者_如何学JAVA

Is it possible to do this by drawing lines?


Don't forget you need to addchild(grid); so that it will show up and obviously you need to call the function from somewhere.

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

    }

I updated the above code to allow for variable cell sizes and added another method (below) of getting to the same thing. The code below is self contained though so you don't have any variables laying around except the Sprite that is displaying the grid.

    /**
     * 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);
            }
        }

    }


One thing you can check out is the Flex charting libraries.

You can also look at some of these Flash solutions. Included on the list is AnyChart, which has some nice charts and features.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜