同系統でランダム色
色をランダムにしたいけど完全にランダムではなく、青系統でとか明るい色でとかいうときはColorTransformを使うと便利です
解説はつづきから
今回もTweenMaxを使います
詳しくは以前の記事参照
新規ファイル作成
ステージ設定はサイズを400×300、背景を黒、フレームレートは30に
新規シンボル作成からActionScriptに書き出しにチェックを入れて、クラス名をStarに
多角形ツールでオプションのスタイルを星にし、星を描く
サイズは60くらい、色は白
シーン1に戻り、新規レイヤーを追加しActionScriptを記述
//TweenMaxをインポート import gs.*; import gs.easing.*; stage.mouseChildren = false; //colorTransformを用意 var ctf1:ColorTransform = new ColorTransform(); var ctf2:ColorTransform = new ColorTransform(); var ctf3:ColorTransform = new ColorTransform(); var num:Number = 0; //配列の番号 //切り替え用に配列に入れる var ctfs:Array = [ctf1,ctf2,ctf3]; //ステージクリックで適用するcolorTransformを切り替え stage.addEventListener(MouseEvent.CLICK, onClick); function onClick(e:Event):void { num++; if(num>ctfs.length-1) num=0; } //星の作成と動きのイベント追加 addEventListener(Event.ENTER_FRAME, loop); function loop(e:Event):void { //star作成 var star:Star = new Star(); //青系統の色設定 ctf1.redMultiplier = 0; ctf1.greenMultiplier = Math.random(); ctf1.blueMultiplier = 1; //紫系統の色設定 ctf2.redMultiplier = Math.random()*0.5+0.5; ctf2.greenMultiplier = 0; ctf2.blueMultiplier = Math.random()*0.5+0.5; //黄系統の色設定 ctf3.redMultiplier = Math.random()*0.2+0.8; ctf3.greenMultiplier = Math.random()*0.2+0.8; ctf3.blueMultiplier = Math.random()*0.2; //starに色適用 star.transform.colorTransform = ctfs[num] ; //starの初期位置、大きさ、透明度設定 star.x = stage.stageWidth/2; star.y = stage.stageHeight/2; star.scaleX = star.scaleY = 0; star.alpha = 0.5; //star設置 addChild(star); var tx:Number = Math.random()*stage.stageWidth; var ty:Number = Math.random()*stage.stageHeight; //Tween設定 TweenMax.to(star, 1, { //starを1秒間で x:tx, y:ty, //位置をランダムに scaleX:1, scaleY:1, //大きさ比率を1に alpha:1, //透明度を1に rotation: Math.random()*360-180, //角度を+-180度でランダムに ease:Cubic.easeIn, //徐々に速くなるイージング onComplete: removeStar, //トゥイーンが終わったら onCompleteParams: [star] //removeStar()実行 }); } function removeStar(star:Star):void { removeChild(star);//star削除 }
これでムービープレビューすると以下のようになってるかと思います
以上、同系統でランダム色の作り方でした