在actionscript中的阶段边界内使用计时器绘制随机圆圈

时间:2010-09-29 02:43:04

标签: actionscript-3

我需要使用定时器进行时间控制动画,每500毫秒时间进行一次绘图。总共绘制20个圆圈。我还需要确保圆圈完全被绘制在舞台的极限之内。我一直在努力想象这一点,这让我疯狂。这是我一直在玩的代码,我无法理解。请尽快帮助!!!

import flash.events.TimerEvent;
import flash.utils.Timer;

// creates a new hundred-second Timer,  ticks every 250 milliseconds
var faster_minuteTimer:Timer = new Timer(250, 6);

// designates listeners for the interval and completion events
faster_minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
faster_minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);

// starts the timer ticking
faster_minuteTimer.start();

function onTick(event:TimerEvent):void
{
// displays the tick count so far
trace("Count... " + event.target.currentCount);
}
function onTimerComplete(event:TimerEvent):void
{
trace("Play Done.");
}
var xCoord, yCoord, radius, Width, Height:uint; // declare variables

// not using any variables for the first one


xCoord = (Math.random()* stage.stageWidth); // somewhere on the stage
yCoord = (Math.random() * stage.stageHeight);
radius = Math.max(Math.random() * 85, 20); // radius between two numbers

graphics.beginFill(Math.random() * 0xffffff); // random color
graphics.drawCircle(xCoord,yCoord,radius); // coordinates x & y, radius
graphics.endFill(); // end color fill

2 个答案:

答案 0 :(得分:2)

import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;

var timer:Timer = new Timer(500, 20);
timer.addEventListener(TimerEvent.TIMER, timerTick);
timer.start();

function timerTick(e:TimerEvent):void {
    var newCircle:Sprite = new Sprite();
    var radius:Number = Math.max(Math.random() * 85, 20);
    var safeX:Number = ((stage.stageWidth - radius) - radius) * Math.random() + radius;
    var safeY:Number = ((stage.stageHeight - radius) - radius) * Math.random() + radius;
    newCircle.graphics.beginFill(Math.random() * 0xFFFFFF, 1);
    newCircle.graphics.drawCircle(0, 0, radius);
    newCircle.graphics.endFill();
    newCircle.x = safeX;
    newCircle.y = safeY;
    stage.addChild(newCircle);
}
  1. 设置计时器
  2. 创建一个圆形精灵
  3. 根据问题的参数生成随机半径大小
  4. 确定max(stage.stageWidth - radius)和min(radius)
  5. 之间的安全值
  6. 在圆形精灵中画一个圆圈,使其中心点位于精灵的原点(0,0)
  7. 将圆圈定位在其给定的随机安全坐标
  8. 将圆圈添加到舞台
  9. 希望这有帮助!

    经过测试和工作:)

    编辑:以下是2000个圈子分布的示例图片:http://grab.by/6C1O

答案 1 :(得分:0)

我没有编译器方便,但这样的事情应该很接近。

全局变量maxWidth,maxHeight,maxSize确定最适合的圆圈以及可以绘制的圆圈。

drawOne()函数绘制一个随机半径圆。圆心被随机设置为至少距离舞台两侧的圆的半径。

计时器的每个刻度都调用drawOne()。

import flash.events.TimerEvent; 
import flash.utils.Timer;

// Timer ticks 20 times 500 msec apart 
var circleTimer:Timer = new Timer(500, 20);

// designates listeners for the interval and completion events 
circleTimer.addEventListener(TimerEvent.TIMER, onTick);
circleTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);

// starts the timer ticking 
circleTimer.start();

function onTick(event:TimerEvent):void 
{
   trace("Count... " + event.target.currentCount);
   drawOne();
} 

function onTimerComplete(event:TimerEvent):void 
{
  trace("Play Done.");
} 

// globals for size of stage, circles
var maxWidth:uint = stage.stageWidth;
var maxHeight:uint = stage.stageHeight;
var maxSize:uint = Math.min(maxWidth, maxHeight);
var minSize:uint = Math.min(20, Math.floor(maxSize/2));

function drawOne():void
{
 // to fit box, radius must be 1/2 shortest side or less
    var radius:uint = Math.max(Math.floor(Math.random() * maxSize/2), minSize);
 // center circle at least radius from any side
    var xCoord:uint = Math.random()*(maxWidth - 2*radius) + radius;
    var yCoord:uint = Math.random()*(maxHeight - 2*radius) + radius;

    graphics.beginFill(Math.random() * 0xffffff); // random color
    graphics.drawCircle(xCoord,yCoord,radius);
    graphics.endFill();
}

希望这会有所帮助......