task 09

martin voshell



/* task 09
frame update displays multiple distinct items in time
displays a car, street sign, marker, and David Hasslehoff

*/



//set up globals to start

//for doing the if statements at intervals
var timekeeper = 0;

//for the wave functions
var x = 0;
var x1 = 0;
var x2 = 0;
var x3 =0;


//zero out my three shapes that appear and disappear
david._alpha = 0;
sixty._alpha = 0;
faster._alpha = 0;


//coordinates for the road lines
var roadEndLeft= 0.2;
var roadEndRight= 0.8;

var roadStartLeft= 0.4;
var roadStartRight= 0.6;


//===============================================

//draw the tarmac
function drawBackground() {

Canvas.setBackgroundColor(0.8, 0.8, 0.8);


}





//===============================================

// this function does the slight car movement
function shiftCarx(){

// wavelength = amplitude * periodicFunction((frequency * time) + phaseShift);
// moves the car smoothly left and right over a very small space
var frequency = 8;
var amplitude = .2;
var phaseShift = 0;

var t = (2 * Math.PI) * ((x*2)/Stage.width);
var sine = amplitude * Math.sin(frequency*t + phaseShift);
y= (sine * (Stage.height/2 )) + (Stage.height/1.5);
x=x+1;
return y;

}




//function for horizontal shifting
function tanny(){

// wavelength = amplitude * periodicFunction((frequency * time) + phaseShift);

var frequency = 3;
var amplitude = 0.2;
var phaseShift = 0;

var t = (2 * Math.PI) * ((x1*2)/Stage.width);
var sine = amplitude * Math.tan(frequency*t + phaseShift);
y= (sine * (Stage.height/2 )) + (Stage.height/1.5);
x1=x1+1;
return y;

}


// a sin function to handle my scaling
function horiz(){
var frequency = 8;
var amplitude = 0.3;
var phaseShift = 0;


var t = (2 * Math.PI) * (x / Stage.width);
var sine = amplitude * Math.sin(frequency*t + phaseShift);

y = (sine * (Stage.height/2 )) + (Stage.height/2);

x3=x3+1;

return y;

}






//this funtion draws the two sides of the road converging on the horizon
function drawRoad(pass) {



Canvas.setPenWeight(.05);
Canvas.setPenColor(0, 0, 0);
//left
Canvas.drawLine(roadStartLeft, .5, roadEndLeft, 1);
//right
Canvas.drawLine(roadStartRight, .5, roadEndRight, 1);


}


//===============================================


//ues an if statement to give the illusion of motion on the road
//by alternating which vertical lines are drawn


function drawRoadLines(pass) {

//takes in ms between 0-999 and mods

Canvas.setPenColor(1, 1, .6);
Canvas.setPenWeight(.05);
if (pass%2==0){
Canvas.drawLine(0.5, .51, 0.5, .61);
Canvas.drawLine(0.5, .91, 0.5, 1);
}

if (pass%2!=0){
Canvas.drawLine(0.5, .71, 0.5, .81);
}


}



//initialize the canvas for drawing

function initCanvas() {
Canvas.setPenWeight(.01);
Canvas.setPenColor(1, 1, 1);

}
//
// the frame updater function

function frameUpdate() {
//get the date
var D = new Date();
//get current time between 0-999
var ms = D.getMilliseconds();
//get current time between 0-59
var ss = D.getSeconds();

//draw the background
drawBackground(ms);

//set the apropriate alphas to show the road and sky
road._alpha = 100;
sky._alpha =100;

//draw the ferrari and move it
enzo._x = shiftCarx();

//make the road
drawRoad(ms);
drawRoadLines(ms);

//set up a function to scale the checkpoint banner
faster._xscale= horiz();
faster._yscale= horiz();

// set up a function to move the speed limit sign
sixty._y = tanny();



//incr our functions time to keep internal scaling and transitions in sync
timekeeper = timekeeper + 1;


//----------------
//now set up the many if's
//basically functions are always drawing, they just get opacity every once in a while

//david hasslehoff easter egg... randomly appears, sometimes on the screen, sometimes not
if (timekeeper == 10){
david._alpha =100;
david._x = (0.2+ms);
}
if (timekeeper == 40){
david._alpha =0;
}

//----------------

//set up a jerky animation for the checkpoint sign
if (timekeeper == 60){
faster._alpha = 50;
}

if (timekeeper == 70){
faster._alpha = 100;
}

if (timekeeper == 90){
faster._alpha =0;
}

//----------------

//make the speed limit sign pass by
if (timekeeper == 100){
sixty._alpha = 100;
}
if (timekeeper == 130){
sixty._alpha =0;

}

//----------------

//turns the time into a loop, we keep repeating the drawing cycle after 160 iterations

if (timekeeper == 160){
timekeeper = 0;


}
}


//===============================================

// event handling:
// associate a function with the enterFrame event
Canvas.onEnterFrame = frameUpdate;
// associate a function with the onLoad event
this.onLoad = initCanvas;