在动作中输入角度时绘制圆圈?

时间:2013-02-27 13:30:41

标签: actionscript-3 actionscript actionscript-2 geometry

    var theTextField:TextField = new TextField();
    var theText:TextField = new TextField();

    theTextField.type = TextFieldType.INPUT;
    theTextField.border = true;
    theTextField.x = 50;
    theTextField.y = 10;
    theTextField.height = 20;
    theTextField.multiline = true;
    theTextField.wordWrap = true;

    theText.border = false;
    theText.x = 10;
    theText.y = 10;
    theText.text = "Angle";
    addChild(theText);
    addChild(theTextField);


    submit.addEventListener(MouseEvent.CLICK, click_handler);
    function click_handler(event:MouseEvent):void
    {
       var txt:String = theTextField.text;
       ang = Number(txt);

       if (ang<0)
       {
          angle =  -  ang;
       }
       else
       {
          angle = 360 - ang;

        }
       var circleSlider:CircleSlider=new CircleSlider(120,angle); //draw Circle    According to the angle i think here is problem becoz every time clicked it creates new circle and draw over the old circle.


       circleSlider.x = stage.stageWidth / 2;
       circleSlider.y = stage.stageHeight / 2;
       circleSlider.addEventListener(CircleSliderEvent.CHANGE,  circleSliderEventHandler);
       addChild(circleSlider);
           }

有人可以帮助我。

      var circleSlider:CircleSlider=new CircleSlider(120,angle);//draw Circle    According to the angle i think here is problem becoz every time clicked it creates new circle and draw over the old circle.

此代码是问题所在。 CircleSlider是一个单独的类。我试过这个

      circleSlider.CircleSlider(120,angle);  

但它给出了一个错误“”通过静态类型CircleSlider的引用调用可能未定义的方法CircleSlider。“”

当我运行程序并输入值为90时。 123

然后我输入另一个值为180然后它变成了 1233

我怎样才能克服这个错误

1 个答案:

答案 0 :(得分:0)

每次执行单击处理程序时,您都会创建一个新的圆类实例,并将其添加到舞台而不删除旧实例。我认为解决问题的最佳方法是将您在CircleSlider类的构造函数中的逻辑移动到一个单独的公共方法中,比如说draw并在单击处理程序中调用它。

您的代码看起来像这样:

// Set up the circle once
var circleSlider = new CircleSlider();
circleSlider.x = stage.stageWidth / 2;
circleSlider.y = stage.stageHeight / 2;
circleSlider.addEventListener(CircleSliderEvent.CHANGE,  circleSliderEventHandler);

// and add it to the stage once
addChild(circleSlider);

function click_handler(event:MouseEvent):void
{
       var txt:String = theTextField.text;
       ang = Number(txt);

       if (ang<0)
       {
          angle =  -  ang;
       }
       else
       {
          angle = 360 - ang;

      }
        // Now simply redraw in the same circle instance
       circleSlider.draw(120,angle); //draw Circle    According to the angle i think here is problem becoz every time clicked it creates new circle and draw over the old circle.
}

假设您正在使用绘图API绘制图形,您可以在构造函数中绘制圆(似乎是常量)(一次)和绘制绘制方法中的角度(重复)。你需要每次都清除旧行:

// Assumes you're drawing in the graphics property of the class
this.graphics.clear();