繪圖API + GlowFilter + BitmapData運用
2011年5月1日 星期日
BitmapData pixelDissolve使用
點擊連結可以直接看程式碼,這個日本的網站很好,你可以把程式碼輸入他會幫你編譯成SWF,並且提供HTML的Embed code可以讓你把swf貼在blog,像我這種沒有空間可用的人覺得好方便。
這個例子你只要用loader取回圖片,將loader轉成bitmapData就可以使用兩張圖片互換
//用這取得BitmapData
public function cloneBitmapData(loader:Loader):BitmapData
{
var bd:BitmapData = new BitmapData(loader.content.width , loader.height);
bd.draw(loader.content);
return bd;
}
這個應用要注意的是,pixelDissolve()執行完會傳回一個新的seed,記得把這seed存下來,下次會用到。
2011年4月27日 星期三
Bitmap 粒子(一)
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BitmapFilterQuality;
import flash.filters.BlurFilter;
import flash.geom.Point;
[SWF(width="1024", height="768", frameRate="30", backgroundColor="#000000", pageTitle="粒子測試")]
public class particale extends Sprite
{
private var _bitmapData:BitmapData;
private var _photosphereMap:Bitmap;
private var _sprite:Sprite = new Sprite();
//活動場景大小
private var _mapWidth:Number = 400;
private var _mapHeight:Number = 400;
//顆粒大小
private var _particaleSize:Number = 10;
//位置與量
private var _position:Point;
private var _vector:Point;
//濾鏡強度
private var blurX:Number = 1.5;
private var blurY:Number = 1.5;
public function particale()
{
super();
init();
}
private function init():void
{
initPhotosphereMap();
deployPhotosphere();
initPostiton();
initVector();
onStart();
}
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BitmapFilterQuality;
import flash.filters.BlurFilter;
import flash.geom.Point;
[SWF(width="1024", height="768", frameRate="30", backgroundColor="#000000", pageTitle="粒子測試")]
public class particale extends Sprite
{
private var _bitmapData:BitmapData;
private var _photosphereMap:Bitmap;
private var _sprite:Sprite = new Sprite();
//活動場景大小
private var _mapWidth:Number = 400;
private var _mapHeight:Number = 400;
//顆粒大小
private var _particaleSize:Number = 10;
//位置與量
private var _position:Point;
private var _vector:Point;
//濾鏡強度
private var blurX:Number = 1.5;
private var blurY:Number = 1.5;
public function particale()
{
super();
init();
}
private function init():void
{
initPhotosphereMap();
deployPhotosphere();
initPostiton();
initVector();
onStart();
}
2011年4月26日 星期二
BitmapData Draw API 運用 : 將圖片切片
Draw API 圖片切片基本原理
public function draw(source:IbtimapDrawable,x:Number , y:Number , widht:Number , height:Number):Bitmap
{
var rectangle:Rectangle = new Rectangle(0,0,widht+0.5,height+0.5);
var bd:BitmapData = new BitmapData(widht+1,height+1);
var matrix:Matrix = new Matrix(1, 0, 0, 1, -x, -y);
bd.draw(sourceImage,matrix,null,null,_rectangle);
return new BitmapData(bd);
}
原理解說:
這是Draw API
public function draw(source:IBitmapDrawable, matrix:Matrix = null, colorTransform:flash.geom:ColorTransform = null, blendMode:String = null, clipRect:Rectangle = null, smoothing:Boolean = false):void
主要是使用ClipRect與matrix搭配來做到切片效果
public function draw(source:IbtimapDrawable,x:Number , y:Number , widht:Number , height:Number):Bitmap
{
var rectangle:Rectangle = new Rectangle(0,0,widht+0.5,height+0.5);
var bd:BitmapData = new BitmapData(widht+1,height+1);
var matrix:Matrix = new Matrix(1, 0, 0, 1, -x, -y);
bd.draw(sourceImage,matrix,null,null,_rectangle);
return new BitmapData(bd);
}
原理解說:
這是Draw API
public function draw(source:IBitmapDrawable, matrix:Matrix = null, colorTransform:flash.geom:ColorTransform = null, blendMode:String = null, clipRect:Rectangle = null, smoothing:Boolean = false):void
主要是使用ClipRect與matrix搭配來做到切片效果
- ClipRect是取一個固定點的可視窗口(將會由Rectangle來定義),在這窗口外的範圍圖片將不可見,所以我們draw的範圍也就是在這窗口中。
- 而藉由Matrix的垂直與水平位移來讓圖片各部份移到窗口。
- new Rectangle(0,0,widht+1,height+1); 與 new BitmapData(widht+1,height+1);會將長官都加1是因為有些狀況下,因為來源圖片想切的長寬無法整除,會造成無法貼齊圖片的現象,所以簡單的方式是以加1象素來解決。
控制使用實體的數量(一)
假設我需要拼圖切片,而拼圖最大數量為9*9,那麼可能數值有2*2~9*9的實體,但是使用者可能沒有選擇到9*9最大數量來玩拼圖,那麼既然他沒用那麼大就不該產生那麼多,所以如下
可以產出正確的實體數量,數量最大值由輸入參數column來控制,程式如下:
//這裡邏輯可以限定產生最大的實體為(9x9)
private var _storge:Vector.<Object>;
private function creatdInstance(column:int):void
{
var startCount:int = _storge.length;
var endCount:int = column*column;
for( startCount ; startCount < endCount ; startCount++)
{
_storge[startCount] = createImageDrawProxy(startCount);
}
}
private function selected(column:int):void
{
creatdInstance(column);
}
//如此當selected被呼叫如下
另外還有一種控管實體數量的方式叫做Pool,在Flex List組件中的ItemRenderer中即有使用到此觀念,其次在遊戲中畫面中產生的swf數量以及像是google地圖切片顯示也都會使用到Pool方式。
可以產出正確的實體數量,數量最大值由輸入參數column來控制,程式如下:
//這裡邏輯可以限定產生最大的實體為(9x9)
private var _storge:Vector.<Object>;
private function creatdInstance(column:int):void
{
var startCount:int = _storge.length;
var endCount:int = column*column;
for( startCount ; startCount < endCount ; startCount++)
{
_storge[startCount] = createImageDrawProxy(startCount);
}
}
private function selected(column:int):void
{
creatdInstance(column);
}
//如此當selected被呼叫如下
- selected(4);
- selected(2);
- selected(6);
另外還有一種控管實體數量的方式叫做Pool,在Flex List組件中的ItemRenderer中即有使用到此觀念,其次在遊戲中畫面中產生的swf數量以及像是google地圖切片顯示也都會使用到Pool方式。
2011年4月25日 星期一
避免重複程式碼:flash/flex開發中對於遠端處理常用偵聽式提出
package com.loaderProxy
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.HTTPStatusEvent;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
public class EventListenerProxy extends EventDispatcher
{
public function EventListenerProxy(target:IEventDispatcher=null)
{
super(target);
}
//event handel method
public var completeHandler:Function = function(event:Event):void {};
public var openHandler:Function = function(event:Event):void{};
public var progressHandler:Function = function(event:ProgressEvent):void{};
public var securityErrorHandler:Function = function(event:SecurityErrorEvent):void{};
public var httpStatusHandler:Function = function(event:HTTPStatusEvent):void{};
public var ioErrorHandler:Function = function(event:IOErrorEvent):void{};
public function addLoaderEventListener(target:IEventDispatcher):void
{
addEventListenerProxy(target , Event.COMPLETE ,completeHandler);
addEventListenerProxy(target , Event.OPEN, openHandler);
addEventListenerProxy(target , ProgressEvent.PROGRESS, progressHandler);
addEventListenerProxy(target , SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
addEventListenerProxy(target , HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
addEventListenerProxy(target , IOErrorEvent.IO_ERROR, ioErrorHandler);
}
public function removeLoaderEventListener(target:IEventDispatcher):void
{
removeEventListenerProxy(target , Event.COMPLETE ,completeHandler);
removeEventListenerProxy(target , Event.OPEN, openHandler);
removeEventListenerProxy(target , ProgressEvent.PROGRESS, progressHandler);
removeEventListenerProxy(target , SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
removeEventListenerProxy(target , HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
removeEventListenerProxy(target , IOErrorEvent.IO_ERROR, ioErrorHandler);
}
//EventListener Proxy
public function addEventListenerProxy(target:IEventDispatcher , type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
{
target.addEventListener(type, listener, useCapture, priority, useWeakReference);
}
public function removeEventListenerProxy(target:IEventDispatcher , type:String, listener:Function, useCapture:Boolean = false):void
{
target.removeEventListener(type , listener , useCapture );
}
}
}
而更好的話將event handel method所有的method宣告到一個Interface,如此若是需要不同動作處理者,可實作該Interface來實現。
因此可以:
public var handel:IHandelMethod = new LoaderHandelMethod();
addEventListenerProxy(target , Event.COMPLETE ,handel.completeHandler);
如此程式碼可以簡化並能鬆綁個物件間之關係
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.HTTPStatusEvent;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
public class EventListenerProxy extends EventDispatcher
{
public function EventListenerProxy(target:IEventDispatcher=null)
{
super(target);
}
//event handel method
public var completeHandler:Function = function(event:Event):void {};
public var openHandler:Function = function(event:Event):void{};
public var progressHandler:Function = function(event:ProgressEvent):void{};
public var securityErrorHandler:Function = function(event:SecurityErrorEvent):void{};
public var httpStatusHandler:Function = function(event:HTTPStatusEvent):void{};
public var ioErrorHandler:Function = function(event:IOErrorEvent):void{};
public function addLoaderEventListener(target:IEventDispatcher):void
{
addEventListenerProxy(target , Event.COMPLETE ,completeHandler);
addEventListenerProxy(target , Event.OPEN, openHandler);
addEventListenerProxy(target , ProgressEvent.PROGRESS, progressHandler);
addEventListenerProxy(target , SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
addEventListenerProxy(target , HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
addEventListenerProxy(target , IOErrorEvent.IO_ERROR, ioErrorHandler);
}
public function removeLoaderEventListener(target:IEventDispatcher):void
{
removeEventListenerProxy(target , Event.COMPLETE ,completeHandler);
removeEventListenerProxy(target , Event.OPEN, openHandler);
removeEventListenerProxy(target , ProgressEvent.PROGRESS, progressHandler);
removeEventListenerProxy(target , SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
removeEventListenerProxy(target , HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
removeEventListenerProxy(target , IOErrorEvent.IO_ERROR, ioErrorHandler);
}
//EventListener Proxy
public function addEventListenerProxy(target:IEventDispatcher , type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
{
target.addEventListener(type, listener, useCapture, priority, useWeakReference);
}
public function removeEventListenerProxy(target:IEventDispatcher , type:String, listener:Function, useCapture:Boolean = false):void
{
target.removeEventListener(type , listener , useCapture );
}
}
}
而更好的話將event handel method所有的method宣告到一個Interface,如此若是需要不同動作處理者,可實作該Interface來實現。
因此可以:
public var handel:IHandelMethod = new LoaderHandelMethod();
addEventListenerProxy(target , Event.COMPLETE ,handel.completeHandler);
如此程式碼可以簡化並能鬆綁個物件間之關係
訂閱:
文章 (Atom)