Link:INB Home|INB English| INB русский язык|INB العربية|INB Türkiye|INB فارسی|INB Español|INB Français|INB Português|INB Deutsch|INB 國語|INB 中文|INB 日本语|INB 한국어|INB ภาษาไทย|INB tiếng Việt||FL 教程Flash 使用bitmapData打造隨機凹凸拼圖效果
INB 國語論壇
歡迎光臨(工業與草根研究員)對工業時代充滿了戰鬥精神,你和我都通過網絡空間與當地草根精神研究員來到瘋狂實驗室。家inbforum.com,永久名稱:tw-inbforum.forums2u.com
INB 國語論壇
歡迎光臨(工業與草根研究員)對工業時代充滿了戰鬥精神,你和我都通過網絡空間與當地草根精神研究員來到瘋狂實驗室。家inbforum.com,永久名稱:tw-inbforum.forums2u.com
INB 國語論壇

歡迎光臨(工業與草根研究員)對工業時代充滿了戰鬥精神,你和我都通過網絡空間與當地草根精神研究員來到瘋狂實驗室。家inbforum.com,永久名稱:tw-inbforum.forums2u.com


您沒有登錄。 請登錄註冊

《《《《《《《上一页INBforum   向下

上一页INBforum》》》》》》》上一篇主題 下一篇主題 向下  內容 [第1頁(共1頁)]

1FL 教程Flash 使用bitmapData打造隨機凹凸拼圖效果 Empty FL 教程Flash 使用bitmapData打造隨機凹凸拼圖效果 周四 3月 03, 2011 7:48 am

Admin

Admin
Admin
下午沒事做,就弄了個隨機切拼圖效果,還沒做判定,只是個切法的計算,稍后整理下就應該是個成品,嘿嘿~原理主要就是用bitmap進行切圖,以前都是用遮照做太占資源.效果如下:








主要代碼:puzzle.as


/*
* Puzzle CLASS
*
* @ CREATED BY: ycccc8202
* @ PURPOSE: 方便設計拼圖游戲
* @ DATE:2007.6.10
* Usage example:
* var url:String = "http://pic.ent.tom.com/data2/upload/888/407/117984810018150686780.jpg";
* var puzzleuzzle = new Puzzle(this, url);
* 設置擺放位置
* puzzle.setPosition(30, 20);
* 設置行/列
* puzzle.setRowAndLine(30, 10);
*/
import com.ycccc.PuzzleGame.MovieClipDrag;
import mx.events.EventDispatcher;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.filters.BevelFilter;
class com.ycccc.PuzzleGame.Puzzle {
/**
* Private members
*/
//////////加載圖片長寬///////////
private var _imageW:Number;
private var _imageH:Number;
//////////設置最大寬高///////////
private var _imageMaxW:Number = 800;
private var _imageMaxH:Number = 500;
////////////////////////////////
private var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;
////////////////////////////////
private var _oldURL:String;
private var _newURL:String;
private var _x:Number;
private var _y:Number;
private var _row:Number;
private var _line:Number;
private var _path:MovieClip;
private var _imageM:MovieClip;
private var _pieceBoard:MovieClip;
private var _imageLoader:MovieClipLoader;
private var _imageBitmap:BitmapData;
private var _pieceW:Number;
private var _pieceH:Number;
private var _pieceMinWH:Number;
private var _pieceD:Number;
///////////內切矩形寬高(通過矩形畫近似橢圓)///////////
private var _pieceOW:Number;
private var _pieceOH:Number;
///////////////比例系數///////////////
private var _pieceD_k:Number = 10;
private var _pieceO_k:Number = 4;
private var _pieceOWH_k:Number = 3/4;
/////////////////////////////////
/**
* Constructor
*/
public function Puzzle(path:MovieClip, imageLink:String) {
EventDispatcher.initialize(this);
_path = path;
_newURL = imageLink;
_imageM = _path.createEmptyMovieClip("imageM", _path.getNextHighestDepth());
_imageLoader = new MovieClipLoader();
_imageLoader.addListener(this);
loadImage(_newURL);
}
/**
* Public methods
*/
public function set _url(url:String) {
loadImage(url);
}
public function get _url():String {
return _oldURL;
}
public function set row(r:Number) {
_row = r;
}
public function set line(l:Number) {
_line = l;
}
public function removeAllPiece() {
for (var all in _pieceBoard) {
_pieceBoard[all].removeMovieClip();
}
}
public function bitmapCut() {
pieceSet();
removeAllPiece();
for (var i:Number = 0; i<_row; i ) {
for (var j:Number = 0; j<_line; j ) {
var Piece = _pieceBoard.createEmptyMovieClip("iece" (_line*i j), _pieceBoard.getNextHighestDepth());
Piece.beginBitmapFill(_imageBitmap, new Matrix(1, 0, 0, 1, -j*_pieceW, -i*_pieceH), true, true);
Piece._x = j*_pieceW;
Piece._y = i*_pieceH;
Piece.i = i;
Piece.j = j;
new MovieClipDrag(Piece);
drawPiece(Piece, getAllDotArray(Piece));
//畫小塊外形
Piece.filters = [new BevelFilter(3, 30)];
}
}
}
public function setMaxWH(w:Number, h:Number) {
//設置答應的最大寬高
_imageMaxW = w;
_imageMaxH = h;
}
public function setRowAndLine(row:Number, line:Number) {
//設置切割的 行/列
if (row<=0 || line<=0) {
trace("行/列不能為0,按默認10*10進行切圖");
return;
}
if (row*line>900) {
trace("數量太大,運算困難,按默認10*10進行切圖");
return;
}
_row = row;
_line = line;
}
public function setPosition(x:Number, y:Number) {
//設置
if (x<0 || y<0) {
trace("超出場景范圍,按默認(0,0)位置進行擺放");
return;
}
_x = x;
_y = y;
if (_pieceBoard<>undefined) {
_pieceBoard._x = _x;
_pieceBoard._y = _y;
}
}
public function getPieceBoard():MovieClip {
//取得碎片載體
return _pieceBoard;
}
public function toString():String {
return "uzzle::凹凸外形的拼圖切割";
}
/**
* Private methods
*/
private function loadImage(url:String) {
_newURL = url;
try {
_imageLoader.loadClip(_newURL, _imageM);
} catch (e:Error) {
trace(e);
}
}
private function onLoadStart(target:MovieClip) {
target._visible = false;
dispatchEvent({type:"onStart", target:this});
}
private function onLoadError(target:MovieClip, errorCode:String) {
dispatchEvent({type:"onError", target:this});
trace("errorCode:" errorCode);
trace("出錯,繼續讀取上張圖片");
_newURL = _oldURL;
loadImage(_newURL);
}
private function onLoadInit(target:MovieClip) {
dispatchEvent({type:"onInit", target:this});
_oldURL = _newURL;
if (target._width<10 || target._height<10) {
throw new Error("圖片太小,不適合切割!");
}
if (isNaN(_imageMaxW _imageMaxH)) {
_imageMaxW = _imageMaxH=600;
}
if (isNaN(_row _line)) {
_row = 10;
_line = 10;
}
_imageW = target._width>_imageMaxW ? _imageMaxW : target._width;
_imageH = target._height>_imageMaxH ? _imageMaxH : target._height;
_pieceBoard.removeMovieClip();
mcToBitmap(target);
}
private function mcToBitmap(mc:MovieClip) {
_imageBitmap = new BitmapData(_imageW, _imageH, true, 0x00ffffff);
_imageBitmap.draw(mc);
//隱藏掉
_pieceBoard = _path.createEmptyMovieClip("pieceBoard", _path.getNextHighestDepth());
_pieceBoard._x = _x;
_pieceBoard._y = _y;
bitmapCut();
}
private function pieceSet() {
_pieceW = _imageW/_line;
_pieceH = _imageH/_row;
_pieceMinWH = Math.min(_pieceW, _pieceH);
_pieceD = _pieceMinWH/_pieceD_k;
_pieceOW = _pieceMinWH/_pieceO_k;
_pieceOH = _pieceOW/_pieceOWH_k;
}
private function getRndD():Number {
//返回與邊界錯開的高度
return _pieceD-Math.random()*2*_pieceD;
}
private function drawPiece(mc:MovieClip, dotArr:Array):Void {
with (mc) {
lineStyle(0);
moveTo(0, 0);
for (var k:Number = 0; k<dotArr.length; k ) {
if (dotArr[k].x<>undefined) {
lineTo(dotArr[k].x, dotArr[k].y);
} else {
curveTo(dotArr[k][0].x, dotArr[k][0].y, dotArr[k][1].x, dotArr[k][1].y);
}
}
endFill();
}
}
private function getOvalDotArray(mc:MovieClip, position:String):Array {
var rnd:Number = random(2) ? 1 : -1;
var circleDotArr:Array = ;
switch (position) {
case "right" :
var a0oint = new Point(_pieceW getRndD(), (_pieceH-_pieceOW)/2 _pieceOW/4-Math.random()*_pieceOW/2);
var a1:Array = [new Point(a0.x rnd*(_pieceOH/2), a0.y-_pieceOW/2), new Point(a0.x rnd*_pieceOH, a0.y)];
var a2:Array = [new Point(a0.x rnd*(_pieceOH _pieceOW/3), a0.y _pieceOW/2), new Point(a0.x rnd*_pieceOH, a0.y _pieceOW)];
var a3:Array = [new Point(a0.x rnd*_pieceOH/2, a0.y _pieceOW _pieceOW/2), new Point(a0.x, a0.y _pieceOW)];
circleDotArr = [a0, a1, a2, a3];
break;
case "down" :
var a0oint = new Point(_pieceW-((_pieceW-_pieceOW)/2 _pieceOW/4-Math.random()*_pieceOW/2), _pieceH getRndD());
var a1:Array = [new Point(a0.x _pieceOW/2, a0.y rnd*(_pieceOH/2)), new Point(a0.x, a0.y rnd*_pieceOH)];
var a2:Array = [new Point(a0.x-_pieceOW/2, a0.y rnd*(_pieceOH _pieceOW/3)), new Point(a0.x-_pieceOW, a0.y rnd*_pieceOH)];
var a3:Array = [new Point(a0.x-_pieceOW-_pieceOW/2, a0.y rnd*_pieceOH/2), new Point(a0.x-_pieceOW, a0.y)];
circleDotArr = [a0, a1, a2, a3];
break;
}
return circleDotArr;
}
private function getAllDotArray(mc:MovieClip):Array {
var allDotArray:Array = ;
//a,b,c,d四面
if (mc.i == 0) {
mc.a = ;
} else {
var tempArray:Array = mc._parent["iece" (Number(mc._name.substr(5))-_line)].c;
var a:Array = new Array(4);
a[0] = new Point(tempArray[3][1].x, tempArray[3][1].y-_pieceH);
a[1] = [new Point(tempArray[3][0].x, tempArray[3][0].y-_pieceH), new Point(tempArray[2][1].x, tempArray[2][1].y-_pieceH)];
a[2] = [new Point(tempArray[2][0].x, tempArray[2][0].y-_pieceH), new Point(tempArray[1][1].x, tempArray[1][1].y-_pieceH)];
a[3] = [new Point(tempArray[1][0].x, tempArray[1][0].y-_pieceH), new Point(tempArray[0].x, tempArray[0].y-_pieceH)];
mc.a = a;
}
if (mc.j == 0) {
mc.d = ;
} else {
var tempArray:Array = mc._parent["Piece" (Number(mc._name.substr(5))-1)].b;
var a:Array = new Array(4);
a[0] = new Point(tempArray[3][1].x-_pieceW, tempArray[3][1].y);
a[1] = [new Point(tempArray[3][0].x-_pieceW, tempArray[3][0].y), new Point(tempArray[2][1].x-_pieceW, tempArray[2][1].y)];
a[2] = [new Point(tempArray[2][0].x-_pieceW, tempArray[2][0].y), new Point(tempArray[1][1].x-_pieceW, tempArray[1][1].y)];
a[3] = [new Point(tempArray[1][0].x-_pieceW, tempArray[1][0].y), new Point(tempArray[0].x-_pieceW, tempArray[0].y)];
mc.d = a;
}
if (mc.i == _row-1) {
mc.c = ;
} else {
mc.c = getOvalDotArray(mc, "down");
}
if (mc.j == _line-1) {
mc.b = ;
} else {
mc.b = getOvalDotArray(mc, "right");
}
allDotArray = allDotArray.concat(mc.a);
allDotArray.push(new Point(_pieceW, 0));
allDotArray = allDotArray.concat(mc.b);
allDotArray.push(new Point(_pieceW, _pieceH));
allDotArray = allDotArray.concat(mc.c);
allDotArray.push(new Point(0, _pieceH));
allDotArray = allDotArray.concat(mc.d);
return allDotArray;
}
}]

http://tw.inbforum.com

2FL 教程Flash 使用bitmapData打造隨機凹凸拼圖效果 Empty 回復: FL 教程Flash 使用bitmapData打造隨機凹凸拼圖效果 周三 2月 11, 2015 11:22 pm

tutuqwe


溪頭大富華森林會館
[您必需注冊登錄才能查看本鏈接。]
1.溪頭民宿,溪頭住宿推薦
[您必需注冊登錄才能查看本鏈接。]
2.妖怪村民宿,妖怪村住宿
[您必需注冊登錄才能查看本鏈接。]
3.妖怪村客房介紹
[您必需注冊登錄才能查看本鏈接。]
4.妖怪村民宿環境設施
[您必需注冊登錄才能查看本鏈接。]
5.妖怪村住宿地圖導覽
[您必需注冊登錄才能查看本鏈接。]
6.溪頭熱門旅游景點介紹
[您必需注冊登錄才能查看本鏈接。]

氬焊機
堆高機
電動車
推高機
拖板車
物料架
料架
led招牌
字幕機
電視牆
跑馬燈
廣告招牌
花蓮民宿
花蓮民宿
金門租車
金門旅遊
超音波熔接機
超音波熔接機
台北搬家
台北搬家]

http://www.songf.com.tw

上一页INBforum   向下

上一页INBforum上一篇主題 下一篇主題 回頂端  內容 [第1頁(共1頁)]

這個論壇的權限:
無法 在這個版面回復文章

Copyright ©2009-2010 LTD Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

IT:SINGLESERVGoogle谷歌翻译TranslateFORUMSOFTLAYERGoogle谷歌广告联盟AdSenseAsia

 

Free forum | 汽車 | Tuning | ©phpBB | 免費論壇互助中心 | 違法舉報 | Cookies | Latest discussions