在ActionScript 3中创建10个圆圈

时间:2012-10-12 13:08:57

标签: actionscript-3 actionscript actionscript-2 flash

Iam尝试在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);
        }

    }

}

2 个答案:

答案 0 :(得分:0)

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

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

圆直径= 30px,阶段高度(例如)= 400px。 30进入400 13.33次(400/30)所以每个圆需要比最后一个圆向下13.3px。

您可以存储一个变量来计算舞台上已经有多少个圆圈,并使用它来乘以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);
}

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

答案 1 :(得分: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;
    }
  }
}