このサイトは、只今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'});
}

Comment

Related Article

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

2012.04.18

ActionScript トゥイーン制御ライブラリ Tweenerの使い方

2012.04.06

ActionScript ムービークリップからメインタイムラインの関数実行

2012.02.27

ActiinScript2.0 ムービークリップを複製 duplicateMovieClip

2011.11.04

ActiinScript2.0 縦横移動ボールアクション

2011.11.04

ActiinScript2.0 ボールアクション 重力、摩擦、跳ね返り係数

2011.11.04

ActiinScript2.0 縦横移動 ボールを掴んで投げる

2011.11.04

ActiinScript2.0 縦横移動 ボールを掴む

2011.11.04

ActionScript onClipEvent()ハンドラ

2011.11.04

ActionScript2.0 ボール反転

2011.11.03

CATEGORY LIST

LATEST NEWS

Rust - Actix Web mongo ユーザー登録 JWT認証

Rust

2024.03.24

Rust - Actix Web JWT 認証認可 APIの作成

Rust

2024.02.25

Rust - Actix Web × JSON 静的ファイルをAPIで返却

Rust

2024.01.19

Rust - Actix Web × MongoDB環境をサクッと起動

Rust

2024.01.18

5分で学ぶ RustでWave Function Collapse (波動関数崩壊アルゴリズム)

Rust

2024.01.15

LLaMAモデル GGMLフォーマット(llama.cpp)をRustフレームワーク Leptosを用いて M1MacMiniでサクッと動かす。

Rust

2024.01.11

2024年 狙っているモバイルノートPC

tool

2024.01.07

MacOS XcodeにSDL2を追加

tool

2023.12.26

php 7.4にアップデート

PHP

2023.12.24

5分で覚える Flutter Flameで作る Wave Function Collapse - 波動関数崩壊アルゴリズム

AI・Bot・algorithm

2023.12.20

Flutter - Flameでゲーム作成 (キャラクターの移動)

Flutter

2023.07.23

Flutterで作る ChatGPT Prompt Manager

Flutter

2023.07.12

RANKING

Follow

SPONSOR

現在、掲載募集中です。



Links

About Us

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

Entry Profile

Graphical FrontEnd Engineer
- Daisuke Takayama

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

FOLLOW US