Fill Pattern

Bước 1:

_ Tạo 1 thư mục “com” dùng để chứa các class
_ Tạo document flash “myPattern.fla” nằm cùng cấp với thư mục “com”

Bước 2: Code cho myPattern.fla

Chọn frame 1 mở bảng actionScript code:

import com.Main;
new Main(this);

Bước 3: Tạo 1 class có tên Pattern.as trong thư mục “com”

package com {

import flash.display.MovieClip;

import flash.display.BitmapData;

import flash.geom.Matrix;


/**

* @author Yoko

*/

public class Pattern {

private var _fla : MovieClip;

private var background : MovieClip;


private var def_array : Array;


public function Pattern(fla : MovieClip) {

_fla = fla;

init();

createBackground();

}


private function init() : void {


// Dòng này quan trọng nhất vì hoa văn sẽ được vẽ từ đây


def_array = new Array(

’111000110110110110110110′,

’111000110110110110110110′,

’111000110110110110110110′,

’111000110110110110110110′,

’111000110110110110110110′,

’111000110110110110110110′,

’111000110110110110110110′,

’111000110110110110110110′,

’111000110110110110110110′,

’111000110110110110110110′);

}


private function createBackground() : void {

background = new MovieClip();

_fla.addChild(background);

background.graphics.beginBitmapFill(drawBitmapPattern(def_array, 0x22FFFFFF), new Matrix(), true);

background.graphics.lineTo(_fla.stage.stageWidth, 0);

background.graphics.lineTo(_fla.stage.stageWidth, _fla.stage.stageHeight);

background.graphics.lineTo(0, _fla.stage.stageHeight);

background.graphics.lineTo(0, 0);

}


private function drawBitmapPattern(def : Array,color : Number) : BitmapData {

var w : Number = def[0].length;

var h : Number = def.length;

var pattern : BitmapData = new BitmapData(w, h, true, 0x00FFFFFF);

for(var r : int = 0;r < h;r++) {

for(var c : int = 0;c < w;c++) {

if(def[r].charAt(c) == ’1′)

pattern.setPixel32(c, r, color);

}

}

return pattern;

}

}

}

Bước 4: Tạo tiếp 1 class Main.as trong thư mục “com” để hiển thị.

package com {
import flash.display.MovieClip;

/**
* @author Yoko
*/
public class Main {
public function Main(main : MovieClip) {
new Pattern(main);
}
}
}

Bước 5: Ctrl+Enter để xem kết quả.

Tham khảo:

_ Mẫu 1:

'00011010101010010010101010110001',
'00100000000001000100000000001000',
'01000000000000101000000000000100',
'10000000000000010000000000000011',
'10000000000000101000000000000010',
'01000000000001000100000000000100',
'01000000000010000010000000000100',
'00111000000100010001000000111000',
'00000100000010000010000001000000',
'00111000000100010001000000111000',
'01000000000010000010000000000100',
'01000000000001000100000000000100',
'10000000000000101000000000000010',
'10000000000000010000000000000011',
'01000000000000101000000000000100',
'00100000000001000100000000001000',
'00011010101010010010101010110001',
'10010101010100111001010101010011'

_ Mẫu 2:

'00000000000000000000000000000000000000',
'00000000000000000000000000000000000000',
'00000000000000000000000000000000000000',
'00000011010101010010010101010110001000',
'00000100000000001000100000000001000000',
'00001000000000000101000000000000100000',
'00010000000000000010000000000000011000',
'00010000000000000101000000000000010000',
'00001000000000001000100000000000100000',
'00001000000000010000010000000000100000',
'00000111000000100010001000000111000000',
'00000000100000010000010000001000000000',
'00000111000000100010001000000111000000',
'00001000000000010000010000000000100000',
'00001000000000001000100000000000100000',
'00010000000000000101000000000000010000',
'00010000000000000010000000000000011000',
'00001000000000000101000000000000100000',
'00000100000000001000100000000001000000',
'00000011010101010010010101010110001000',
'00010010101010100111001010101010011000',
'00000000000000000000000000000000000000',
'00000000000000000000000000000000000000',
'00000000000000000000000000000000000000'

_ Mẫu 3:

'0001101010101001001010101011000111011011011011011 0',
'0010000000000100010000000000100011011011011011011 0',
'0100000000000010100000000000010011011011011011011 0',
'1000000000000001000000000000001111011011011011011 0',
'1000000000000010100000000000001011011011011011011 0',
'0100000000000100010000000000010011011011011011011 0',
'0100000000001000001000000000010011011011011011011 0',
'0011100000010001000100000011100011011011011011011 0',
'0000010000001000001000000100000011011011011011011 0',
'0011100000010001000100000011100011011011011011011 0',
'0100000000001000001000000000010011011011011011011 0',
'0100000000000100010000000000010011011011011011011 0',
'1000000000000010100000000000001011011011011011011 0',
'1000000000000001000000000000001111011011011011011 0',
'0100000000000010100000000000010011011011011011011 0',
'0010000000000100010000000000100011011011011011011 0',
'0001101010101001001010101011000111011011011011011 0',
'1001010101010011100101010101001111011011011011011 0'

2 phản hồi tới “Fill Pattern”

  1. bactien nói:

    em Copy nguyên code báo lỗi file Pattern.as :|

    • yokovnfx nói:

      Báo lỗi như thế nào đưa lên để hướng dẫn chỉnh sửa
      @: Có lẽ bạn chưa cấu hình ClassPath cho file FLA
      Cách thực hiện như sau:
      _ Nhấn Ctrl+Shift+F12
      _ Chọn thẻ FLASH chọn Setting
      _ Nhấn vô biểu tượng Browse to path chọn đường dẫn thư mục chứa file fla và “com”
      _ Link tham khảo : http://yokovnfx.wordpress.com/category


Gửi phản hồi

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Thay đổi )

Twitter picture

You are commenting using your Twitter account. Log Out / Thay đổi )

Facebook photo

You are commenting using your Facebook account. Log Out / Thay đổi )

Connecting to %s

Follow

Get every new post delivered to your Inbox.