このサイトは、只今WEB業界で活躍中のデザイナー、プログラマーの方々の情報を集めたweb統合情報サイトです。

Archives Details

ActionScript ランダム関数 Math.random() まとめ

ActionScript

2012.04.18

この記事は最終更新日から1年以上が経過しています。

ランダム関数 Math.randomについてまとめてみました。

「Math.random」は、0 以上 1 未満の浮動小数で結果を返します。

では調べましょう。

STARTボタンを押して下さい。

Math.random

数字(乱数)がダーと出ましたね。

ActionScript

btn.onRelease = function(){ 
  ransu = Math.random(); 
  randomTxt = ransu; 
}

数字が表示されるところはダイナミックテキストで作成し、変数「randomTxt」に設定しております。

このままではとても使うには不便なので、使えるように数字を一桁にしましょう。

Math.random Math.floor

その為には、まず生成した乱数に10を掛けます。

ransu = Math.random()*10;

 

ransu = Math.floor(Math.random()*10);

10掛けた乱数をMath.floorで整数にします。

これで0~9の整数をランダムに生成することが出来ます。

おみくじ

では、Math.radomを使い、おみくじを作ります。

0〜3の場合、「凶」、4、5の場合「中吉」、6〜9の場合「大吉」にします。

btn.onRelease = function(){ 
  if(ransu<=3){ 
   uranaiTxt = "凶"; 
  }else if(ransu>3 && ransu<6){
   uranaiTxt = "中吉"; 
  }else if(ransu>=6){ 
   uranaiTxt = "大吉"; 
  }
  }

 

ボール移動

それでは今度は、ボールオブジェクトをランダムに移動させましょう。

Startボタンを押すとランダムにX座標を移動します。

btn.onRelease = function(){ 
  ransu = Math.random()*10;
  ransu = Math.floor(ransu)/2*100;
  randomTxt = ransu; ball._x = ransu;
}

X座標0〜450まで50単位でランダムに移動するのが確認できました。

そのままだと瞬間移動しているので、Tweenerを使い動いているように作成します。

import caurina.transitions.Tweener;

import文で読み込み、以下のように指定。

Tweener.addTween(ball,{ _x:ransu, time:1, transition:'easeOutQuint'});

 

ランダムに色を変化

続いてはランダムに色の変化を。

色の変化を行う場合は以下のスクリプトを

btn.onRelease = function(){ 
  newColor = new Color(ball);
  newColorNum = Math.floor(Math.random()*256*256*256);
  newColor.setRGB(newColorNum); randomTxt = newColorNum;
}

new Colorでカラーオブジェクトを生成。setRGBで設定します。

が、これはすでに推奨されていないので、

以下のスクリプトを。

import flash.geom.ColorTransform;
import flash.geom.Transform; 
btn.onRelease = function(){ 
  var trans:Transform = new Transform(ball);
  var newColor : ColorTransform = new ColorTransform(-1, -1, -1, 1, 255, 255, 255, 0);    
  trans.colorTransform = newColor;
}

flash.geom.ColorTransform、flash.geom.Transformをインポート。

new TransformでTransformオブジェクトを生成。

new ColorTransformで新しい色を生成し、trans.colorTransform = newColor;でセットします。

最後にランダムに色とX座標を変化させましょう。

import flash.geom.ColorTransform;
import flash.geom.Transform;
import caurina.transitions.Tweener;
btn.onRelease = function(){ 
  newColorNum1 = Math.floor(Math.random()*256);
  newColorNum2 = Math.floor(Math.random()*256);
  newColorNum3 = Math.floor(Math.random()*256);
  var trans:Transform = new Transform(ball);
  var newColor : ColorTransform = new ColorTransform(-1, -1, -1, 1, newColorNum1, newColorNum2, newColorNum3, 0);
  trans.colorTransform = newColor; ransu = Math.random()*10; ransu = Math.floor(ransu)/2*100;   
  randomTxt = ransu; Tweener.addTween(ball,{ _x:ransu, time:1, transition:'easeOutQuint'});
}

Related Article

LATEST NEWS

RANKING

ARCHIVE

SPONSOR

現在、掲載募集中です。



About Us

WEBデザイナーの、WEBデザイナーによる、WEBデザイナーの為のサイト。「みんなで書こう!」と仲間を募ってみたが、結局書くのは自分だけとなってしまいました。日々のメモを綴っていきます。

Entry Profile

Graphical FrontEnd Engineer
- Daisuke Takayama

MAD CITY 北九州市で生まれ育つ。20代はバンド活動に明け暮れ、ふと「webデザイナーになりたい。」と思い、デジタルハリウッド福岡校入学。卒業後、数々の賞を受賞、web業界をざわつかせる。
現在、主に、ゲーム制作中心に港区六本木界隈で活動中。