15.3.15

Draw a Circle 3

Okay, now that we have mashed up the two programs, we come to the confusing part. We must fuse them into one cohesive program without error. First, since we are no longer in the jump program - and since we are going to use a true sine function instead of a fake one - there are certain parts of both programs that we no longer need, or must change. These are highlighted in blue:

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

var cirqy:Number = 220
var cirqx:Number = 250
const addam:Number = -1.2
var total:Number = .8


Red.addEventListener(MouseEvent.CLICK, RedJump);
function RedJump(event:MouseEvent):void

{

timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, redTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, redTimerDone);
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 + addam + 16 ;
    total = total + addam;
    cirqy = cirqy - (Math.pow(total, 2)+16);
}

We don't need the constant addam or the variable total anymore because we're going to be using the Math.sin() and Math.cos() functions. These functions only receive one parameter, which is the angle.
We're getting rid of the Red event listener because we're not using buttons this time. The timer will simply operate on its own. We will also have to change the names of the timer's functions to reflect the reality of what we're trying to do. Just to make things clearer, we're also going to have to change the timer's intervals from .01 times a thousand milliseconds (a hundredth of a second)  to just a hundred milliseconds (a tenth of a second). This was practice was really a holdover from when I learned timers. 100 is just more bug-free than .01 times 1000.

No comments:

Post a Comment