16.3.15

Draw a Circle 4

Her is the code for the program so far:

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

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



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

function redTimerStep(event:TimerEvent):void {
Green.x -= 8;
}
function redTimerDone(event:TimerEvent):void {
Green.x += 240;
}


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) * 5) ;
    cirqy = cirqy - (Math.cos(angle) * 5) ;
    angle = angle + 3;
}

You notice that we have replaced the previous constant and variable, which were used in tandem, with a singular variable known as angle, which is initialized to zero. And also, the equations for using the sine and cosine functions are defined below. But we've forgotten something. In the middle of the program, there are references to the timer step event and the timer done event. These are a holdover from our Jump program. You can see that their main function is to manipulate an object called Green and move it across the screen in one direction. This was our green square from the Jump program. We don't need these anymore, so we're going to get rid of them. They are highlighted in blue.

No comments:

Post a Comment