19.3.15

Draw a Circle 7

Okay. Here is our code so far. If you look at the top, you can see that I have added a new variable (scaling) to the list. The purpose of this is to take two separate amounts and tie them into one variable, ostensibly so that if I want to change both values, I won't have to do it twice - only once. The change is highlighted in blue:

import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;

var cirqy:Number = 220;
var cirqx:Number = 250;
var angle:uint = 0;
var scaling:uint = 200;

timer = new Timer(100, 30);
timer.addEventListener(TimerEvent.TIMER, createCircle);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, stopCreatingCircles);
timer.start()

function createCircle(event:TimerEvent):void {
var circle:Sprite = new Sprite();
    circle.graphics.lineStyle(.5, 1);
    circle.graphics.beginFill(0xBC51F8);
    circle.graphics.drawCircle(cirqx, cirqy, 5);
    graphics.endFill();
   
    addChild (circle);
   
    cirqx = cirqx + (Math.sin(angle) * scaling) ;
    cirqy = cirqy - (Math.cos(angle) * scaling) ;
    angle = angle + 3;
   
}

function stopCreatingCircles(event:TimerEvent):void {
   
}

You should recognize the number 200. That's the amount that we multiplied by the sine and cosine of angle to give us a visible amplitude for our circle drawing. Put this code in your actions panel (Alt+F9) and then run it by clicking Control -> Test Movie -> Test. When you run this code, you should notice something. The program draws a circle on the screen, but there is something unusual. The program is supposed to draw one dot at a time, in a circle, from zero degrees upward, but it doesn't. Instead, it draws one dot; then, it goes more than 180 degrees in a circle and draws another dot. But we're only adding 3 degrees each iteration, so why is the program adding 180+ degrees with every iteration? I'll tell you why in the next post.

No comments:

Post a Comment