views:

35

answers:

2

I need to use a Timer for time controlled animation, time the drawing to occur every 500 milliseconds & Draw 20 circles in total. I also need to make sure the circles are completely drawn inside the limits of the stage.. I've been trying to figure this out forever and it's driving me crazy. This is the code I've been playing around with and I can't figure it out. PLEASE HELP ASAP!!!

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

+1  A: 
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. Setup your timer
  2. Create a circle sprite
  3. Generate a random radius size per your question's parameters
  4. Determine a safe value between max (stage.stageWidth - radius) and min (radius)
  5. Draw a circle in the circle sprite such that its center point is on the Sprite's origin (0, 0)
  6. Position the circle at its given, randomized safe coordinates
  7. Add the circle to the stage

Hope this helps!

Tested and working :)

Edit: Here is a sample image of a distribution of 2000 circles: http://grab.by/6C1O

Tegeril
THANK YOU SO MUCH!!!! Exactly what I needed
Jen
You're welcome. I'd appreciate it if you could mark it as the accepted answer. Good luck with your work!
Tegeril
A: 

I don't have a compiler handy, but something like this should be close.

The global variables maxWidth, maxHeight, maxSize determine the largest circle that will fit and where it can be drawn.

The drawOne() function draws a circle of random radius. The center of the circle is randomly set to be at least the circle's radius away from each side of the stage.

Each tick of the timer calls 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();
}

Hope this helps...

Lars