flash as3中10x10乘法表的代码,

时间:2013-11-26 08:34:37

标签: actionscript-3 flash

我是AS3编码的新手,我想要一些帮助。我收到了一个任务,我必须编写一个乘法表(10行,10列)。我已设法对表进行编码,但我需要帮助才能添加所需的数字,而无需手动添加文本。 它应该看起来像这样;

http://gyazo.com/65ac93e1197ba7e9a679dff3fc50c644.png

有人可以帮助我完成这项任务吗? 这是我的代码:

var xColumns:uint=10;
var xRows:uint=10;
var _columnWidth:Number=40;
var _rowHeight:Number=40
var _width:Number=_columnWidth*xColumns;
var _height:Number=_rowHeight*xRows;

graphics.lineStyle(2, 0x0000ff, 1);


for(var i:int=1; i<=xColumns; i++){
    graphics.moveTo(i*_columnWidth,0);
    graphics.lineTo(i*_columnWidth,_height);
}

for(i=1; i<=xRows; i++){
    graphics.moveTo(0,i*_rowHeight);
    graphics.lineTo(_width,i*_rowHeight);

}

1 个答案:

答案 0 :(得分:0)

这是“Cell”类的样本:

package  
{
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;


    public class Cell extends Sprite
    {
        //---------------------------------------
        protected var m_oLabel:TextField;
        protected var m_nWidth:Number = 20;
        protected var m_nHeight:Number = 20;
        protected var _backgroundColor:Number;
        //---------------------------------------
        //Group: CONSTRUCTOR
        public function Cell() 
        {
            super();
            m_oLabel = new TextField();
            m_oLabel.text = "999";
            m_oLabel.autoSize = TextFieldAutoSize.CENTER;


            addChild(m_oLabel);


            setLabel("999");
        }
//--------------------------------------------------//
//          PUBLIC  PUBLIC  PUBLIC  PUBLIC          //
//--------------------------------------------------//
        public function setLabel(p_sLabel:String):void
        {
            m_oLabel.text = p_sLabel || "";
            updateLayout();
        }
        public function get backgroundColor():Number 
        {
            return _backgroundColor;
        }

        public function set backgroundColor(value:Number):void 
        {
            _backgroundColor = value;
            redraw();
        }
 //------------------- OVERRIDDEN -------------------//
        override public function get width():Number 
        {
            return m_nWidth;
        }

        override public function set width(value:Number):void 
        {
            m_nWidth = value;
            updateLayout();
            redraw();
        }
        override public function get height():Number 
        {
            return m_nHeight;
        }

        override public function set height(value:Number):void 
        {
            m_nHeight = value;
            updateLayout();
            redraw();
        }


//--------------------------------------------------//
//          PRIVATE/PROTECTED PRIVATE/PROTECTED     //
//--------------------------------------------------//

        protected function updateLayout():void 
        {
            //adjust layout
            if (m_oLabel)
            {
                m_oLabel.x = width * .5 - m_oLabel.width * .5;
                m_oLabel.y = height * .5 - m_oLabel.height * .5;
            }
        }

        protected function redraw():void 
        {

            var g:Graphics = this.graphics;
            g.beginFill(_backgroundColor);
            g.drawRect(0, 0, width, height);
            g.endFill();
        }
//------------------- OVERRIDDEN -------------------//

//--------------------------------------------------//
//          EVENTS  EVENTS  EVENTS  EVENTS          //
//--------------------------------------------------//
//------------------- OVERRIDDEN -------------------//

//--------------------------------------------------//
//          UTILS   UTILS   UTILS   UTILS           //
//--------------------------------------------------//
//------------------- OVERRIDDEN -------------------//

    }
}

和用法:

        var container:Sprite = new Sprite();
        addChild(container);
        container.x = 10;
        container.y = 10;

        for (var i:int = 0; i <= 10; i++) 
        {
            for (var j:int = 0; j <= 10; j++) 
            {
                var cell:Cell = new Cell();

                if (i == 0 || j == 0)
                {
                    //headers
                    cell.setLabel(Math.max(i, j).toString());
                    cell.backgroundColor = 0x808080;
                }
                else
                {
                    var mul:Number = i * j;
                    cell.setLabel(mul.toString());
                    cell.backgroundColor = 0xC0C0C0;
                }
                container.addChild(cell);
                cell.x = cell.width * j;
                cell.y = cell.height * i;

            }
        }

看起来像这样:

output of the multiplication table

新类“Cell”为您提供灵活性和封装代码,用于创建单个表格单元格。在我的例子中,它只是textfield的容器,并且能够绘制背景。我创建了公共方法/访问器,允许修改单元格的“功能”(即TextField中显示的内容和背景颜色是什么)。我不得不覆盖默认的宽度/高度行为,否则它会返回它的内容大小,这取决于TextField的内容和大小,在表中我们需要稳定的大小。