Flashでベジェ曲線にそって動くMovieClipのサンプルを制作する。
NM_Irregularcircleの均等分割を使用してMCがベジェ曲線の上をなぞるように動くサンプル。
現在のポイントと次のポイントから矢印の向きを三角関数で指定している。曲線はリロードでランダムに変形します。
SAMPLE VIEW CODE
package {
import flash.events.*;
import flash.display.*;
public class NMain extends MovieClip {
//-------------------------------------------------------------------------------- Properties
private var _this:*;
private var _stage:Stage;
private var _main:MovieClip;
//-------------------------------------------------------------------------------- Constractor
public function NMain() {
_this = this;
_this.addEventListener(Event.ADDED_TO_STAGE, Init);
}
//-------------------------------------------------------------------------------- Function
/**
* Init
*/
public function Init(eo:* = null):void {
_this.removeEventListener(Event.ADDED_TO_STAGE, Init);
_stage = stage;
_stage.scaleMode = StageScaleMode.NO_SCALE;
_stage.align = StageAlign.TOP_LEFT;
_main = new MovieClip();
_this.addChild(_main);
_stage.addEventListener(Event.RESIZE, stageresizeaction);
stageresizeaction();
initirregularcircle();
initarrow();
}
/**
* initialize irregularcircle
*/
private var points:Array;
private function initirregularcircle():void {
var args = {};
args.anchorlength = 8;
args.radius = 80;
args.roundval = 400;
args.irregular_ap = -400;
args.irregular_cp = 100;
args.linesize = 1;
args.linealpha = .1;
args.fillalpha = 0;
args.linecolor = 0x000000;
args.fillcolor = args.linecolor;
var irrcircle = new NM_Irregularcircle();
irrcircle.Init(args);
_main.addChild(irrcircle);
points = irrcircle.getSplitPoints(200, 10000, .1);
trace(points.length);
var nmg = new NM_Gradation();
var cols = nmg.Colors(0x00A0E9, 0xE4007F, args.anchorlength);
var _t:MovieClip;
for(var i:uint = 0; i < args.anchorlength; i ++) {
_t = irrcircle.getChildByName((i + 1) + "_a");
_t.tf.textColor = cols[i];
}
}
/**
* initialize arrow
*/
private var _arrow:MovieClip;
private var movecounter:int;
private function initarrow():void {
movecounter = 0;
_arrow = new arrowmc();
_main.addChild(_arrow);
_arrow.addEventListener(Event.ENTER_FRAME, arrowaction);
}
/** */
private function arrowaction(eo:* = null):void {
if(movecounter == points.length) movecounter = 0;
var now_cnt = movecounter;
var next_cnt = now_cnt + 1;
if(next_cnt == points.length) next_cnt = 0;
var now_p = points[now_cnt];
var next_p = points[next_cnt];
_arrow.rotation = Math.atan2(now_p.y - next_p.y, now_p.x - next_p.x) * 180 / Math.PI;
_arrow.x = now_p.x;
_arrow.y = now_p.y;
_arrow.alpha = .5;
movecounter ++;
}
/**
* stage resize
*/
private function stageresizeaction(eo:* = null):void {
_main.x = Math.round(_stage.stageWidth / 2);
_main.y = Math.round(_stage.stageHeight / 2);
}
}
}