0

我试图在 AS3 中创建一个简单的方法,添加 10 个圆圈,每个圆圈的直径为 30px,列在舞台左侧下方的一列中,因此第一个出现在左上角,最后一个出现在右下角. 我会很感激这里的任何帮助。谢谢。

我当前的代码如下: - 它目前只返回 10 个没有定位的圆圈。

package
{
    import flash.display.*;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    public class Circles extends MovieClip
    {
        private var timer:Timer = new Timer(100, 10);

        public function Circles()
        {

            timer.addEventListener(TimerEvent.TIMER, createCircles);
            timer.start();
        }
        private function createCircles(e:TimerEvent):void
        {
            var bcircle:MovieClip = new MovieClip();
            var xpos:int = 0;
            var ypos:int = 0;

                bcircle.graphics.beginFill(0x0033CC);
                bcircle.graphics.drawCircle(xpos,ypos,15);
                bcircle.graphics.endFill();
                bcircle.x = Math.random() * stage.stageWidth;
                bcircle.y = Math.random() * stage.stageHeight;
                addChild(bcircle);
        }

    }

}
4

2 回答 2

0
bcircle.x = Math.random() * stage.stageWidth;
bcircle.y = Math.random() * stage.stageHeight;

这些是您要注意的线条,因为这些线条控制添加到舞台时每个圆圈的放置位置。

圆直径 = 30px,舞台高度(例如)= 400px。30 进入 400 13.33 倍(400/30),因此每个圆圈需要比上一个圆圈更向下间隔 13.3 像素。

您可以存储一个变量来计算舞台上已有多少个圆圈,并使用它乘以 13.3 以找到合适的y值:

var i:int = 0;
private function createCircles(e:TimerEvent):void
{
    var bcircle:MovieClip = new MovieClip();
    var xpos:int = 0;
    var ypos:int = 0;

    bcircle.graphics.beginFill(0x0033CC);
    bcircle.graphics.drawCircle(xpos,ypos,15);
    bcircle.graphics.endFill();
    bcircle.x = 0;
    bcircle.y = (30 + 13.3) * i; // First circle placed at 0, Second placed at 43.3...
    i++; // Increment i each time function is called
    addChild(bcircle);
}

您可以应用类似的方法,然后在x我达到 10(已放置 10 个圆圈)后增加每个圆圈的位置,使用触发器来检测:if(i == 10)

于 2012-10-12T13:25:50.477 回答
0

这可能有助于...

package {
  import flash.display.Graphics;
  import flash.display.Sprite;
  import flash.events.TimerEvent;
  import flash.utils.Timer;

  public class Circles extends Sprite {

    private const _circles:Array = []; // reusable container
    private var _timer:Timer;

    public function Circles() {
      _timer = new Timer(1000, 10);
      _timer.addEventListener(TimerEvent.TIMER, timer_timerHandler);
      _timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_timerCompleteHandler);

      _timer.start();
    }

    private function createCircle():Sprite {
      const sprite:Sprite = new Sprite();

      drawCircleInGraphics(sprite.graphics);

      addChild(sprite);

      return sprite;
    }

    private function drawCircleInGraphics(graphics:Graphics, color:uint = 0x0033CC, radius:uint = 15):void {
      graphics.beginFill(color);
      graphics.drawCircle(0, 0, radius);
      graphics.endFill();
    }

    private function createNewCircleAndPositionIt():void {
      _circles[_circles.length] = createCircle();

      if (_circles.length > 1) {
        const last:Sprite = _circles[_circles.length - 1],
            predecessor:Sprite = _circles[_circles.length - 2];

        last.x = predecessor.x + 30;
        last.y = predecessor.y + 30;
      }
    }

    private function timer_timerHandler(event:TimerEvent):void {
      createNewCircleAndPositionIt();
    }

    private function timer_timerCompleteHandler(event:TimerEvent):void {
      _timer.removeEventListener(TimerEvent.TIMER, timer_timerHandler);
      _timer.removeEventListener(TimerEvent.TIMER_COMPLETE, timer_timerCompleteHandler);
      _timer = null;
    }
  }
}
于 2012-10-12T13:46:36.280 回答