Task 5 - Animating: Ghost Milk Cow
(Downloadable .swf file and .fla file)
Code //initial setting
#include "Canvas.as"
var C = new Canvas();
var n_count = 0;
var n_duration = 30*5;
//original centerpoint of the cow
var cowX = .2;
var cowY = .3;
//how much the image will move on X and Y scale
var leftright = .01;
var updown = .01;
//varables for cow's size
var width = .2;
var height = .3;
var cowColor = new Array(1, 1, .95);
this.onEnterFrame = function() {
n_count = (n_count+1)%n_duration;
draw(n_count);
};
function draw(n) {
C.setBackgroundColor(0, .8, .4);
var centerPoint = new Array(cowX, cowY);
//when the ctrpoint is between .2 and .5 on X
//between .3 and .7 on Y
//move downwards and rightwards
if ((centerPoint[0]>=.2) && (centerPoint[0]<=.5) && (centerPoint[1]>=.3) && (centerPoint[1]<=.7)) {
cowX = cowX+leftright;
cowY = cowY+updown;
drawCow(centerPoint);
//when the ctrpoint is between .5 and .8 on X
//between .3 and .7 on Y
//move upwards and rightwards
} else if ((centerPoint[0]<=.8) && (centerPoint[0]>.5) && (centerPoint[1]<=.7) && (centerPoint[1]>=.3)) {
cowX = cowX+leftright;
cowY = cowY-updown;
drawCow(centerPoint);
//when the ctrpoint is <.3 on Y
//move leftwards
//ctrpoing >=.2 is to keep it in the canvas and go on to a new move
} else if ((centerPoint[0]>=.2) && (centerPoint[1]<.3)) {
cowX = cowX-leftright;
cowY = cowY;
drawCow(centerPoint);
//go on to a new move
} else {
cowX = .2;
cowY = .3;
var centerPoint = new Array(cowX, cowY);
drawCow(centerPoint);
}
}
//draw the cow image
function drawCow(A) {
drawHorns(A);
drawEars(A);
drawUpperHead(A);
drawLowerHead(A);
drawFeet(A);
}
//two horns
function drawHorns(centerPoint) {
C.setPenColor(.45, .45, .3);
var hornLeftCtr = new Array(centerPoint[0]-.06, centerPoint[1]-.15);
var hornRightCtr = new Array(centerPoint[0]+.06, centerPoint[1]-.15);
var hornRadiX = width*.1;
var hornRadiY = height*.15;
C.drawOval(true, hornLeftCtr[0], hornLeftCtr[1], hornRadiX, hornRadiY);
C.drawOval(true, hornRightCtr[0], hornRightCtr[1], hornRadiX, hornRadiY);
C.setPenWeight(.008);
C.setPenColor(.675, .675, .675);
C.drawOval(false, hornLeftCtr[0], hornLeftCtr[1], hornRadiX, hornRadiY);
C.drawOval(false, hornRightCtr[0], hornRightCtr[1], hornRadiX, hornRadiY);
}
//two ears
function drawEars(centerPoint) {
//main part of the ear-triangle
C.setPenColor(cowColor[0], cowColor[1], cowColor[2]);
var earLeft1 = new Array(centerPoint[0]-.15, centerPoint[1]-.15);
var earLeft2 = new Array(centerPoint[0]-.15, centerPoint[1]-.03);
var earLeft3 = new Array(centerPoint[0]-.09, centerPoint[1]-.09);
var earRight1 = new Array(centerPoint[0]+.15, centerPoint[1]-.15);
var earRight2 = new Array(centerPoint[0]+.09, centerPoint[1]-.09);
var earRight3 = new Array(centerPoint[0]+.15, centerPoint[1]-.03);
var left = new Array(earLeft1, earLeft2, earLeft3);
var right = new Array(earRight1, earRight2, earRight3);
C.drawRegion(true, left);
C.drawRegion(true, right);
C.setPenColor(.675, .675, .675);
C.drawRegion(false, left);
C.drawRegion(false, right);
//left earhole
C.setPenColor(1, .84, .78);
C.drawOval(true, centerPoint[0]-.15, centerPoint[1]-.09, width*.1, height*.2);
C.setPenColor(.675, .675, .675);
C.drawOval(false, centerPoint[0]-.15, centerPoint[1]-.09, width*.1, height*.2);
//right earhole
C.setPenColor(1, .84, .78);
C.drawOval(true, centerPoint[0]+.15, centerPoint[1]-.09, width*.1, height*.2);
C.setPenColor(.675, .675, .675);
C.drawOval(false, centerPoint[0]+.15, centerPoint[1]-.09, width*.1, height*.2);
}
//upper half face
function drawUpperFace(centerPoint) {
C.setPenColor(cowColor[0], cowColor[1], cowColor[2]);
var upperFaceCtr = new Array(centerPoint[0], centerPoint[1]+.05);
var widthUpperFace = width-.07;
var heightUpperFace = height-.08;
C.drawOval(true, upperFaceCtr[0], upperFaceCtr[1], widthUpperFace, heightUpperFace);
C.setPenColor(.675, .675, .675);
C.drawOval(false, upperFaceCtr[0], upperFaceCtr[1], widthUpperFace, heightUpperFace);
}
//two eyes on upper half face
function drawEyes(centerPoint) {
C.setPenColor(.3, .3, .3);
C.drawOval(true, centerPoint[0]-.04, centerPoint[1], width*.1, height*.15);
C.drawOval(true, centerPoint[0]+.04, centerPoint[1], width*.1, height*.15);
C.setPenColor(.675, .675, .675);
C.drawOval(false, centerPoint[0]-.04, centerPoint[1], width*.1, height*.15);
C.drawOval(false, centerPoint[0]+.04, centerPoint[1], width*.1, height*.15);
//pupil
C.setPenColor(1, 1, 1);
C.drawOval(true, centerPoint[0]-.035, centerPoint[1]-.01, .005, .005);
C.drawOval(true, centerPoint[0]+.045, centerPoint[1]-.01, .005, .005);
}
//two eyebrows on upper half face
function drawEyebrows(centerPoint) {
C.setPenColor(.3, .3, .3);
C.drawLine(centerPoint[0]-.05, centerPoint[1]-.11, centerPoint[0]-.03, centerPoint[1]-.11);
C.drawLine(centerPoint[0]+.03, centerPoint[1]-.11, centerPoint[0]+.05, centerPoint[1]-.12);
}
//combine upperface, eyes and eyebrows together
function drawUpperHead(centerPoint) {
drawUpperFace(centerPoint);
drawEyes(centerPoint);
drawEyebrows(centerPoint);
}
//lower half face
function drawLowerFace(centerPoint) {
C.setPenColor(.93, .80, .86);
var lowerFaceCtr = new Array(centerPoint[0], centerPoint[1]+.15);
var widthLowerFace = width-.045;
var heightLowerFace = height*.42;
C.drawOval(true, lowerFaceCtr[0], lowerFaceCtr[1], widthLowerFace, heightLowerFace);
C.setPenColor(.675, .675, .675);
C.drawOval(false, lowerFaceCtr[0], lowerFaceCtr[1], widthLowerFace, heightLowerFace);
}
//two nostrils on lower half face
function drawNostril(centerPoint) {
C.setPenColor(.875, .875, .875);
C.drawOval(true, centerPoint[0]-.06, centerPoint[1]+.08, width*.15, height*.06);
C.drawOval(true, centerPoint[0]+.06, centerPoint[1]+.08, width*.15, height*.06);
C.setPenColor(.675, .675, .675);
C.drawOval(false, centerPoint[0]-.06, centerPoint[1]+.08, width*.15, height*.06);
C.drawOval(false, centerPoint[0]+.06, centerPoint[1]+.08, width*.15, height*.06);
}
//mouth on lower half face
function drawMouth(centerPoint) {
C.setPenColor(1, .7, .56);
C.drawOval(true, centerPoint[0], centerPoint[1]+.2, width*.18, height*.1);
C.setPenColor(.675, .675, .675);
C.drawOval(false, centerPoint[0], centerPoint[1]+.2, width*.18, height*.1);
C.setPenColor(.675, .675, .675);
C.drawLine(centerPoint[0]-.036, centerPoint[1]+.2, centerPoint[0]+.036, centerPoint[1]+.2);
}
//combine lowerface, nostril, mouth together
function drawLowerHead(centerPoint) {
drawLowerFace(centerPoint);
drawNostril(centerPoint);
drawMouth(centerPoint);
}
function drawLeftFoot(centerPoint) {
//Leg part
C.setPenColor(cowColor[0], cowColor[1], cowColor[2]);
C.drawOval(true, centerPoint[0]-.15, centerPoint[1]+.2, width*.15, height*.15);
C.setPenColor(.675, .675, .675);
C.drawOval(false, centerPoint[0]-.15, centerPoint[1]+.2, width*.15, height*.15);
//hoof part - two triangles with a little bit overlap
var hoofLeftUp1 = new Array(centerPoint[0]-.155, centerPoint[1]+.155);
var hoofLeftUp2 = new Array(centerPoint[0]-.135, centerPoint[1]+.21);
var hoofLeftUp3 = new Array(centerPoint[0]-.11, centerPoint[1]+.16);
var hoofLeftDown1 = new Array(centerPoint[0]-.145, centerPoint[1]+.18);
var hoofLeftDown2 = new Array(centerPoint[0]-.13, centerPoint[1]+.23);
var hoofLeftDown3 = new Array(centerPoint[0]-.10, centerPoint[1]+.19);
var hoofLeftUp = new Array(hoofLeftUp1, hoofLeftUp2, hoofLeftUp3);
var hoofLeftDown = new Array(hoofLeftDown1, hoofLeftDown2, hoofLeftDown3);
C.setPenColor(.45, .45, .3);
C.drawRegion(true, hoofLeftup);
C.setPenColor(.675, .675, .675);
C.drawRegion(false, hoofLeftUp);
C.setPenColor(.45, .45, .3);
C.drawRegion(true, hoofLeftDown);
C.setPenColor(.675, .675, .675);
C.drawRegion(false, hoofLeftDown);
}
//milk bottle hold by right foot
function drawBottle(centerPoint) {
C.setPenColor(1, 1, 1);
C.drawOval(true, centerPoint[0]+.12, centerPoint[1]+.15, width*.18, height*.3);
C.setPenColor(.675, .675, .675);
C.drawOval(false, centerPoint[0]+.12, centerPoint[1]+.15, width*.18, height*.3);
C.setPenColor(.77, 0, 0);
var bottleCap1 = new Array(centerPoint[0]+.1, centerPoint[1]+.05);
var bottleCap2 = new Array(centerPoint[0]+.1, centerPoint[1]+.07);
var bottleCap3 = new Array(centerPoint[0]+.135, centerPoint[1]+.07);
var bottleCap4 = new Array(centerPoint[0]+.135, centerPoint[1]+.05);
var bottleCap = new Array(bottleCap1, bottleCap2, bottleCap3, bottleCap4);
C.drawRegion(true, bottleCap);
C.setPenColor(.675, .675, .675);
C.drawRegion(false, bottleCap);
C.setPenColor(.93, .80, .86);
C.drawOval(true, centerPoint[0]+.1, centerPoint[1]+.14, width*.05, height*.15);
C.setPenColor(.675, .675, .675);
C.drawOval(false, centerPoint[0]+.1, centerPoint[1]+.14, width*.05, height*.15);
centerPoint += leftright;
}
function drawRightFoot(centerPoint) {
//Leg part
C.setPenColor(cowColor[0], cowColor[1], cowColor[2]);
C.drawOval(true, centerPoint[0]+.15, centerPoint[1]+.2, width*.15, height*.15);
C.setPenColor(.675, .675, .675);
C.drawOval(false, centerPoint[0]+.15, centerPoint[1]+.2, width*.15, height*.15);
//hoof part - two triangles with a little bit overlap
var hoofRightUp1 = new Array(centerPoint[0]+.155, centerPoint[1]+.155);
var hoofRightUp2 = new Array(centerPoint[0]+.135, centerPoint[1]+.21);
var hoofRightUp3 = new Array(centerPoint[0]+.11, centerPoint[1]+.16);
var hoofRightDown1 = new Array(centerPoint[0]+.145, centerPoint[1]+.18);
var hoofRightDown2 = new Array(centerPoint[0]+.13, centerPoint[1]+.23);
var hoofRightDown3 = new Array(centerPoint[0]+.10, centerPoint[1]+.19);
var hoofRightUp = new Array(hoofRightUp1, hoofRightUp2, hoofRightUp3);
var hoofRightDown = new Array(hoofRightDown1, hoofRightDown2, hoofRightDown3);
C.setPenColor(.45, .45, .3);
C.drawRegion(true, hoofRightUp);
C.setPenColor(.675, .675, .675);
C.drawRegion(false, hoofRightUp);
C.setPenColor(.45, .45, .3);
C.drawRegion(true, hoofRightDown);
C.setPenColor(.675, .675, .675);
C.drawRegion(false, hoofRightDown);
}
//combine left foot, right foot and milk bottle
function drawFeet(centerPoint) {
drawLeftFoot(centerPoint);
drawBottle(centerPoint);
drawRightFoot(centerPoint);
}