今回から,
[プロパティ]インスペクタを使った3次元空間の操作
コンピュータグラフィックス
だが,
これは,
[プロパティ]インスペクタの[3D位置とビュー]セクションには,
- ※1
- [プロパティ]インスペクタの[消失点]は,
ドキュメントプロパティだ。つまり, どのタイムラインで設定しても, ステージ上のすべてのMovieClipインスタンスの3次元表現を変える。ただし, その設定をするには, いずれかのMovieClipインスタンスを選んでおかなければならない ([ヘルプ]の[Flash CS4 Professionalユーザガイド] > [アートワークの作成および編集] > [3Dグラフィック]参照)。
ステージにランダムなシェイプを配置する
遠近法を操作すると表現がどう変わるかは,
Shape.
Graphicsオブジェクト.beginFill(カラー値, アルファ値)
カラーは通常16進数の整数,
Graphicsオブジェクト.drawCircle(中心のx座標値, 中心のy座標値, 半径の長さ)
これらの知識をもとにして,
// フレームアクション
function xCreateCircle(nRadius:Number, nColor:uint = 0):Shape {
var myShape:Shape = new Shape();
var myGraphics:Graphics = myShape.graphics;
myGraphics.beginFill(nColor);
myGraphics.drawCircle(0, 0, nRadius);
myGraphics.endFill();
return myShape;
}
同じタイムラインのフレームアクションからつぎのように関数xCreateCircle()を呼び出すと,
var myShape:Shape = xCreateCircle(10, 0x0000FF);
addChild(myShape);
myShape.x = stage.stageWidth / 2;
myShape.y = stage.stageHeight / 2;
このShapeインスタンスをいくつもステージ上につくるとき,
Math.random() * (最大値 - 最小値) + 最小値
カラー値は整数にする必要がある。ランダムな整数値を求める場合,
Math.floor(Math.random() * (最大値 - 最小値 + 1) + 最小値)
そこで,
function xGetRandom(nMax:Number = 1, nMin:Number = 0, bInt:Boolean = false):Number {
if (nMax < nMin) {
var nTemp:Number = nMax;
nMax = nMin;
nMin = nTemp;
}
var nRandom:Number = Math.random();
if (bInt) {
return Math.floor(nRandom * (nMax - nMin + 1) + nMin);
} else {
return nRandom * (nMax - nMin) + nMin;
}
}
以下のスクリプト1には,
スクリプト1 ステージ上にランダムな位置とサイズおよびカラーの円のShapeインスタンスをつくる
// タイムライン: メイン
// フレームアクション
xCreateShapes(100);
function xCreateShapes(nCount:uint):void {
for (var i:uint = 0; i < nCount; i++) {
var nColor:uint = xGetRandom(0xFFFFFF, 0, true);
var nRadius:Number = xGetRandom(10);
var myShape:Shape = xCreateCircle(nRadius, nColor);
addChild(myShape);
myShape.x = xGetRandom(stage.stageWidth);
myShape.y = xGetRandom(stage.stageHeight);
myShape.z = xGetRandom(stage.stageWidth);
}
}
function xCreateCircle(nRadius:Number, nColor:uint = 0):Shape {
var myShape:Shape = new Shape();
var myGraphics:Graphics = myShape.graphics;
myGraphics.beginFill(nColor);
myGraphics.drawCircle(0, 0, nRadius);
myGraphics.endFill();
return myShape;
}
function xGetRandom(nMax:Number = 1, nMin:Number = 0, bInt:Boolean = false):Number {
if (nMax < nMin) {
var nTemp:Number = nMax;
nMax = nMin;
nMin = nTemp;
}
var nRandom:Number = Math.random();
if (bInt) {
return Math.floor(nRandom * (nMax - nMin + 1) + nMin);
} else {
return nRandom * (nMax - nMin) + nMin;
}
}
もっとも,
- ※2
- SpriteクラスもSprite.
graphicsプロパティ にGraphicsオブジェクトの参照をもつ。したがって,Shapeインスタンスと同じようにベクターが描ける。 - ※3
- ランダムな整数値の求め方について詳しくは,
少し古いが 「Math. random()でランダムな整数を取得する方法 」を参照してほしい。