Gradient Matrixの練習
↓夏blogエントリーから勉強させていただきました。
http://nutsu.com/blog/2009/020921_as_gradient_matrix.html
/*
↓夏blogエントリーから勉強させていただきました。
http://nutsu.com/blog/2009/020921_as_gradient_matrix.html
*/
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.display.GradientType;
public class Main extends MovieClip{
var x0:int = 100;
var x1:int= 275;
var y0:int= 100;
var y1:int = 100;
var speedX:int = 25;
var speedY:int = 10;
var pt0:Sprite = new Sprite();
var pt1:Sprite = new Sprite();
//グラデーションの設定
var colors:Array = [0xff66ff,0x55aacc];
var alphas:Array = [100,100];
var ratios:Array = [0,0xff];
public function Main() {
pt0.x = x0;
pt0.y = y0;
pt0.graphics.beginFill(0xFF0000);
pt0.graphics.drawCircle(0,0,10);
pt0.graphics.endFill();
addChild(pt0);
pt1.x = x1;
pt1.y = y1;
pt1.graphics.beginFill(0x0000FF);
pt1.graphics.drawCircle(0,0,10);
pt1.graphics.endFill();
addChild(pt1);
addEventListener(Event.ENTER_FRAME,xStageEnterFrame);
}
function xStageEnterFrame(e:Event):void {
if(pt0.x > stage.stageWidth){
speedX = speedX*-1
}
if(pt0.x < 0){
speedX = speedX*-1
}
if(pt1.y > stage.stageHeight){
speedY = speedY*-1
}
if(pt1.y < 0){
speedY = speedY*-1
}
pt0.x += speedX;
pt1.y += speedY;
var matrix:Matrix = new Matrix();
//とりあえず単位行列的な
matrix.createGradientBox( 1, 1, 0, -0.5, -0.5 );
//線分のベクトル
var vx:Number = pt1.x - pt0.x;
var vy:Number = pt1.y - pt0.y;
//線分の長さ(p0,p1の距離)
var w:Number = Math.sqrt( vx*vx + vy*vy );
//傾き
var r:Number = Math.atan2( vy, vx );
//中点
var cx:Number = (pt0.x + pt1.x)/2;
var cy:Number = (pt0.y + pt1.y)/2;
//あとはあててくだけ
matrix.scale( w, 1 );//線形なんで高さは 1 にしとく
matrix.rotate( r );
matrix.translate( cx, cy );
graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
}
}
}