BitmapDataクラス

| コメント(0) | トラックバック(0)

FLASH8から追加されたクラス、BitmapData。
参考書をもとにこんなの作ってみた。スクリーンクリックでスタート

FLASH PLAYER

Get Adobe Flash player

_rootに画面サイズの写真をムービークリップ化したもの(インスタンス名:bg)と透明ボタン(インスタンス名:btn)を配置。以下のコードをタイムライン2フレーム目に記述。1フレーム目は「stop();」。

import flash.display.BitmapData;

var b:Number=0;//マスのナンバー
var row:Number=20;//行数
var col:Number=20;//列数

//マスごとビットマップ化する関数定義
function makeBMP():Void{
 for(var g:Number=0;g<row;g++){
  for(var d:Number=0;d<col;d++){
   var container:MovieClip=_root.createEmptyMovieClip("con"+b,b);
   var bmp=new BitmapData(Stage.width/col,Stage.height/row,false);
   //元画像をマスごとずらす
   _root.bg._y-=Math.floor((g*(Stage.height/row)));
   _root.bg._x-=Math.floor((d*(Stage.width/col)));
   //_rootをbmpに一旦保存
   bmp.draw(_root);
   //保存した_rootをcontainerに乗せる
   container.attachBitmap(bmp,0);
   container._x=-10000;
   
   _root.bg._y+=Math.floor((g*(Stage.height/row)));
   _root.bg._x+=Math.floor((d*(Stage.width/col)));
   
   b++;
  }
 }
 b=0;
 for(g=0;g<row;g++){
  for(d=0;d<col;d++){
   //containerの位置を元に戻す
   _root["con"+b]._y=g*(Stage.height/row);
   _root["con"+b]._x=d*(Stage.width/col);
   //vanishの目標値ランダム設定
   _root["con"+b].nextx=Math.random()*Stage.width;//_x
   _root["con"+b].nexty=Math.random()*Stage.height;//_y
   _root["con"+b].nextr=Math.floor(Math.cos(b*10)*(Math.random()*90));//_rotation
   _root["con"+b].nexta=0;//_alpha
   _root["con"+b].nexts=Math.random()*200;//_xscale&_yscale
   //vanish関数を呼び出す
   _root["con"+b].onEnterFrame=vanish;
   b++;
  }
 }
 //元画像のvisibleをfalseに
 _root.bg._visible=false;
}
//vanish関数定義
function vanish(){
 this._x+=(this.nextx-this._x)/10;
 this._y+=(this.nexty-this._y)/10;
 this._rotation+=(this.nextr-this._rotation)/12;
 this._alpha+=(this.nexta-this._alpha)/15;
 this._xscale+=(this.nexts-this._xscale)/15;
 this._yscale+=(this.nexts-this._yscale)/15;
 if(this._alpha<0.5){
  delete this.onEnterFrame;
  removeMovieClip(this);
  //元画像のvisibleをtrueに戻す
  _root.bg._visible=true;
 }
}
//ボタンアクション定義
_root.btn.onRelease=function(){
 makeBMP();
 _root.play();
}

以下、公式。

import flash.display.BitmapData;
var BitmapDataインスタンス:BitmapData=new BitmapData(幅:Number,高さ:Number,[transparent:Boolean], [fillColor:Number]);
BitmapDataインスタンス.draw(Bitmap化するインスタンス);
MovieClip.attachBitmap(BitmapDataインスタンス,0,auto,true);

【まとめ】
生成するBitmapDataは、常に指定した大きさで左上に作られる。したがって、今回のサンプルのようなものを製作するためには、マスごとずらしながら生成していく必要がある。左上隅の画像をスクリーンショットで、取るイメージか。
また、BitmapDataはかなりメモリを使用するみたいなので、サンプルの行と列の数を増やしすぎるとPCがフリーズするので注意。
勉強不足のため、現段階ではBitmapDataを使って、このほかにどのような面白いことができるのか模索中。

トラックバック(0)

トラックバックURL: http://www.ayabin.jp/mt5/mt-tb.cgi/165

コメントする

このブログ記事について

このページは、webmasterが2009年4月30日 21:41に書いたブログ記事です。

ひとつ前のブログ記事は「MovieClipLoaderクラス」です。

次のブログ記事は「Yahoo!ドームの時計」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。