AS3でテクスチャを描画する「drawTexture」クラス

drawTexture

drawTexture

ActionScript3.0で任意の大きさ・デザインのテクスチャを描画する「drawTexture」クラスを作成しました。

ボタンのベースデザインや、フルスクリーン表示のムービー補正用テクスチャとして使用できるかもしれません。

サンプルでは、210px四方のテクスチャを 4個ステージ上に配置しています。

下記のボタンより、ソースのダウンロード及びサンプルの閲覧ができます。

Sample Download

【使用例】

1px四方の黒いテクスチャを、横幅:210px*縦幅:210pxの範囲に描画


drawTexture.init(texture, 0x000000, 210, 210);

10px四方の赤いテクスチャを、横幅:200px*縦幅:40pxの範囲に描画


drawTexture.init(texture, 0xCC0000, 200, 40, 10, 10);

1px四方の黒いテクスチャを、横幅:200px*縦幅:40pxの範囲へ縦横3px置きに半透明で描画


drawTexture.init(texture, 0x000000, 200, 40, 1, 1,
3, 3, 0.5);

【パラメータ説明】


$scope : テクスチャを描画するターゲット [DisplayObject] [省略不可]
$color : テクスチャの描画色 [初期値: 0x000000] [省略可]
col : テクスチャを描く横範囲 [初期値: 100] [省略可]
row : テクスチャを描く縦範囲 [初期値: 100] [省略可]
w : テクスチャの横幅 [初期値: 1] [省略可]
h : テクスチャの縦幅 [初期値: 1] [省略可]
intervalX : テクスチャ毎の横間隔 [初期値: 2] [省略可]
intervalY : テクスチャ毎の縦間隔 [初期値: 2] [省略可]
$alpha : テクスチャの不透明度 [初期値: 1] [省略可]

ソースコード

import jp.atziluth.gui.drawTexture;


package jp.atziluth.gui{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Graphics;
import jp.atziluth.utils.cleaner;
public class drawTexture extends Sprite {
public static function init(
$scope:DisplayObject,
$color:uint=0x000000,
col:uint=100,
row:uint=100,
w:uint=1,
h:uint=1,
intervalX:uint=2,
intervalY:uint=2,
$alpha:Number=1
):void {
if ($scope == null) {
return;
}
var shape:Shape = new Shape();
var scope:Sprite = Sprite($scope);
var g:Graphics = shape.graphics;
function _beginFill():void {
g.beginFill($color, $alpha);
}
function _drawRect():void {
function drawTextureY():void {
for (var Y:uint; Y<(row/h); Y++) {
drawTextureX(Y);
}
}
function drawTextureX(Y:uint):void {
for (var X:uint; X<(col/w); X++) {
if (X % intervalX == Y % intervalY) {
g.drawRect(X*w, Y*h, w, h);
}
}
}
drawTextureY();
}
function addShape(
$scope:Sprite,
$shape:Shape
):void {
if ($scope) {
cleaner.deleteSprite($scope, 0, null);
}
$scope.addChild($shape);
$scope.cacheAsBitmap = true;
}
_beginFill();
_drawRect();
addShape(scope, shape);
}
}
}

サンプルソースコード】

Sample.as


package {
import flash.display.MovieClip;
import jp.atziluth.gui.drawTexture;
public class Sample extends MovieClip {
private var texture1:_container = new _container();
private var texture2:_container = new _container();
private var texture3:_container = new _container();
private var texture4:_container = new _container();
public function Sample():void {
this.addChildAt(texture1, 0);
with (texture1) {
x = 0;
y = 0;
}
drawTexture.init(
texture1, 0x000000, 210, 210
);
this.addChildAt(texture2, 0);
with (texture2) {
x = 210;
y = 0;
}
drawTexture.init(
texture2, 0x000000, 210, 210, 3, 3
);
this.addChildAt(texture3, 0);
with (texture3) {
x = 0;
y = 210;
}
drawTexture.init(
texture3, 0x000000, 210, 210, 10, 10
);
this.addChildAt(texture4, 0);
with (texture4) {
x = 210;
y = 210;
}
drawTexture.init(
texture4, 0x000000, 210, 210, 1, 1, 4, 4
);
}
}
}

Download