1
    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

我该如何克服这个错误

4

1 回答 1

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 来绘制图形,您可以在构造函数中(一次)绘制圆(似乎是恒定的),并在 draw 方法中绘制说明角度的线(重复)。您每次都需要像这样清除旧行:

// Assumes you're drawing in the graphics property of the class
this.graphics.clear(); 
于 2013-02-27T13:44:14.127 回答