Code:
/*
TransformMatrix.as
Matrix helper for Flash MX drawing functions
Christopher Thilgen
Macromedia
*/
function TransformMatrix() {
this.empty = true;
}
TransformMatrix.prototype.isEmpty = function() {
return this.empty;
}
TransformMatrix.prototype.init = function(a, b, d, e, g, h) {
this.a = a;
this.d = d;
this.g = g;
this.b = b;
this.e = e;
this.h = h;
this.c = 0;
this.f = 0;
this.i = 1;
this.empty = false;
}
TransformMatrix.prototype.concat = function(m) {
if (this.isEmpty()) {
this.a = m.a;
this.d = m.d;
this.g = m.g;
this.b = m.b;
this.e = m.e;
this.h = m.h;
this.c = m.c;
this.f = m.f;
this.i = m.i;
} else {
var result = new TransformMatrix();
result.a = this.a*m.a+this.b*m.d+this.c*m.g;
result.b = this.a*m.b+this.b*m.e+this.c*m.h;
result.c = this.a*m.c+this.b*m.f+this.c*m.i;
result.d = this.d*m.a+this.e*m.d+this.f*m.g;
result.e = this.d*m.b+this.e*m.e+this.f*m.h;
result.f = this.d*m.c+this.e*m.f+this.f*m.i;
result.g = this.g*m.a+this.h*m.d+this.i*m.g;
result.h = this.g*m.b+this.h*m.e+this.i*m.h;
result.i = this.g*m.c+this.h*m.f+this.i*m.i;
this.a = result.a;
this.d = result.d;
this.g = result.g;
this.b = result.b;
this.e = result.e;
this.h = result.h;
this.c = result.c;
this.f = result.f;
this.i = result.i;
}
this.empty = false;
}
TransformMatrix.prototype.scale = function(sx, sy) {
var m = new TransformMatrix();
m.init(sx, 0, 0, sy, 0, 0);
return (this.concat(m));
}
TransformMatrix.prototype.translate = function(tx, ty) {
var m = new TransformMatrix();
m.init(1, 0, 0, 1, tx, ty);
return (this.concat(m));
}
TransformMatrix.prototype.rotate = function(r) {
// r is in degrees - must convert to radians
var rad = (r/180)*Math.PI;
var m = new TransformMatrix();
var cosVal = Math.cos(rad);
var sinVal = Math.sin(rad);
m.init(cosVal, sinVal, -sinVal, cosVal, 0, 0);
return (this.concat(m));
}
function TransformMatrix() {
this.empty = true;
}
TransformMatrix.prototype.isEmpty = function() {
return this.empty;
}
TransformMatrix.prototype.init = function(a, b, d, e, g, h) {
this.a = a;
this.d = d;
this.g = g;
this.b = b;
this.e = e;
this.h = h;
this.c = 0;
this.f = 0;
this.i = 1;
this.empty = false;
}
TransformMatrix.prototype.concat = function(m) {
if (this.isEmpty()) {
this.a = m.a;
this.d = m.d;
this.g = m.g;
this.b = m.b;
this.e = m.e;
this.h = m.h;
this.c = m.c;
this.f = m.f;
this.i = m.i;
} else {
var result = new TransformMatrix();
result.a = this.a*m.a+this.b*m.d+this.c*m.g;
result.b = this.a*m.b+this.b*m.e+this.c*m.h;
result.c = this.a*m.c+this.b*m.f+this.c*m.i;
result.d = this.d*m.a+this.e*m.d+this.f*m.g;
result.e = this.d*m.b+this.e*m.e+this.f*m.h;
result.f = this.d*m.c+this.e*m.f+this.f*m.i;
result.g = this.g*m.a+this.h*m.d+this.i*m.g;
result.h = this.g*m.b+this.h*m.e+this.i*m.h;
result.i = this.g*m.c+this.h*m.f+this.i*m.i;
this.a = result.a;
this.d = result.d;
this.g = result.g;
this.b = result.b;
this.e = result.e;
this.h = result.h;
this.c = result.c;
this.f = result.f;
this.i = result.i;
}
this.empty = false;
}
TransformMatrix.prototype.scale = function(sx, sy) {
var m = new TransformMatrix();
m.init(sx, 0, 0, sy, 0, 0);
return (this.concat(m));
}
TransformMatrix.prototype.translate = function(tx, ty) {
var m = new TransformMatrix();
m.init(1, 0, 0, 1, tx, ty);
return (this.concat(m));
}
TransformMatrix.prototype.rotate = function(r) {
// r is in degrees - must convert to radians
var rad = (r/180)*Math.PI;
var m = new TransformMatrix();
var cosVal = Math.cos(rad);
var sinVal = Math.sin(rad);
m.init(cosVal, sinVal, -sinVal, cosVal, 0, 0);
return (this.concat(m));
}
//-----------Create World--------------
_root.createEmptyMovieClip("world", 100);
//Create world atmosphere
with (_root.world) {
colors = [0x0099FF, 0x006600];
alphas = [100, 100];
ratios = [0, 0xFF];
matrix = new TransformMatrix();
matrix.scale(400, 200);
matrix.translate(300, 300);
matrix.rotate(90);
beginGradientFill("linear", colors, alphas, ratios, matrix);
moveto(0, 0);
lineto(0, Stage.width);
lineto(Stage.width, Stage.height);
lineto(Stage.width, Stage.height);
lineto(Stage.width, 0);
endFill();
}
The above simply creates a world movieclip with a linear fill that covers the stage. In my next visit, I will describe how to create a 3rd dimension with Actionscript and place objects in the 3D world using arrays and for loops.
Bookmarks