() translation by (you can also view the original English article)
XML adalah format data yang cocok untuk galeri gambar dinamis . Hal itu juga sangat mudah untuk diproses di Flash dengan AS3. Dengan bantuan dari XML dan ActionScript, kita akan membuat galeri foto yang dinamis dan dikategorikan menjadi tema yang berbeda.
Kami juga akan belajar bagaimana menggunakan sangat praktis tweening kelas, yang disebut Tweener, untuk memindahkan ikon tema berbeda dan thumbnail. Dalam tutorial ini saya akan berkonsentrasi pada efektif pengolahan data yang Diperoleh dari XML; Semua animasi gambar mewah terserah Anda ;)
Langkah 1: Menyiapkan panggung
Pertama kali aku akan berbagi bagaimana cara mengatur panggung saya. Ini tidak tetap pilihan, jadi jika Anda ingin mengubah hal-hal, yang baik-baik saja. Hanya pastikan Anda mengubah kode dalam tutorial ini untuk menyesuaikan setelan Anda (khususnya mengenai ukuran panggung).
Tahap saya adalah 900 x 600 px; Saya menetapkan warna latar belakang hitam dan frame rate menjadi 25fps. Meskipun film SWF adalah hanya satu jangka panjang, menetapkan FPD penting karena akan mempengaruhi tweening kita akan melakukan.



Seperti Anda dapat melihat kita akan menggunakan dokumen kelas, tetapi sebelum itu, kita perlu membuat lapisan, dan menempatkan benda-benda yang kita akan menggunakan.
Langkah 2: Utama gambar movieklip
Semua konten-bergerak kami akan pada lapisan yang disebut 'BigPic + judul + Arrows' (membuat ini sekarang). Yang pertama adalah movieklip kosong yang disebut Img (nama contoh: img); ini adalah Mc di mana kita akan memuat setiap gambar yang besar. Membuat ini, dan menempatkannya di (x: 450, y:220).
Langkah 3: Membuat navigasi Panah
Cara termudah untuk membuat ini adalah dengan menggunakan besar ' <' karakter. Membuat bidang teks statis baru, pilih apa pun font dan ukuran yang anda inginkan, jenis ' <', dan kemudian menggunakan perintah 'Break apart': pilih karakter dan tekan CTRL + B atau Ubah > Pecah.
Kemudian, mengubah bentuk ini menjadi sebuah movieklip, nama 'Panah' dan mengubah titik bergabung ke pusat sebelah kiri (di mana panah poin).



Langkah 4: Menempatkan panah navigasi
Kita akan membutuhkan dua ini di panggung, jadi tarik satu lagi dari perpustakaan. Flip salah satunya horizontal (Ubah > mengubah > Flip Horizontal). Posisi kiri satu pada (x: 0, y:220), dan memberikan nama contoh dari 'prevArrow'. Orang yang tepat harus pergi di (x: 900, y:220), dan memiliki nama contoh dari 'nextArrow'.
Langkah 5: Teks Field Title
Menciptakan sebuah lapangan teks lebar dinamis 900px (sehingga Anda dapat menggunakan judul selama yang Anda inginkan). Menempatkannya di (x: 0, y:440), dan memberikan nama contoh dari 'txtField'. Saya menggunakan Arial font di 16pt, selaras dengan pusat; Jangan lupa untuk menanamkan!



Langkah 6: Menggambar penutup piring
Ini adalah elemen desain hanya sedikit untuk ikon tema dan thumbnail untuk membuat mereka menghilang di perbatasan panggung. Membuat 30px lebar, 130px tinggi persegi panjang (tanpa stroke - hanya mengisi), dan dengan persegi panjang masih dipilih, pergi ke panel warna dan mengubah gaya isi dari padat ke linier. Mengatur warna kedua ke hitam, namun tetapkan salah satu dari mereka ke alpha 0%.



Langkah 7: Mengkonversi dan menempatkan pelat penutup
Sekarang, mengubah persegi untuk sebuah movieklip (reg. titik: sudut kiri atas, nama: Cover). Melakukan hal yang sama seperti yang Anda lakukan dengan panah: drag dua contoh ke panggung, dan flip salah satu dari mereka. Dalam setiap kasus, sisi alpha 0% harus terdekat ke pusat panggung.
Menempatkan mereka pada sisi panggung: (meninggalkan satu: (x: 0, y:470); kanan satu: (x: 900, y:470)).
Semua benar - kami selesai dengan lapisan ini.
Langkah 8: "Tema + jempol" lapisan
Di bawah lapisan kami yang sudah ada, membuat yang baru bernama 'tema + jempol'. Pada lapisan ini, membuat movieklip kosong untuk ikon tema (nama: 'Tema', reg. titik: sudut kiri atas, nama contoh: 'tema', x: 0, y: 470) dan satu lagi untuk thumbnail (nama: 'smallPics', reg. titik: sudut kiri atas, nama contoh: 'smallPicsMc', x: 0, y: 510).
Kita harus menciptakan beberapa simbol lain yang tidak mulai keluar di panggung, tetapi kami akan membuat ini di ActionScript kemudian.
Langkah 9: Simbol Thumbnail
Membuat movieklip kosong (nama: 'SmallPicButton', reg. titik: pojok kiri). MC ini akan berisi satu thumbnail; kita akan memiliki banyak contoh dari simbol ini, jadi kami akan membuat masing-masing pada saat runtime.
Untuk melakukan itu, kita harus mengekspor simbol ini untuk ActionScript: di panel Perpustakaan, klik kanan pada simbol ini, kemudian pilih Properties. Centang kotak 'Ekspor untuk ActionScript', kemudian set kelas SmallPicButton. Kelas dasar tetap seperti itu.



Setelah Anda klik OK, klik dua kali pada simbol ini di perpustakaan untuk memasuki modus edit, kemudian pergi ke panel tindakan - kita akan menulis beberapa kode sekarang. Kita akan memastikan bahwa movieklip ini berperilaku seperti sebuah tombol yang tepat.
Langkah 10: Tindakan untuk SmallPicButton
Copy dan paste kode ini ke dalam tindakan panel:
1 |
|
2 |
//if the cursor is over the symbol we change its alpha to 1
|
3 |
function rollOver(e:MouseEvent):void{ |
4 |
alpha = 1; |
5 |
}
|
6 |
|
7 |
//if the cursor just moved off the symbol, we change its alpha to 0.5
|
8 |
function rollOut(e:MouseEvent):void{ |
9 |
alpha = 0.5; |
10 |
}
|
11 |
|
12 |
//register the event listeners to run the above functions
|
13 |
addEventListener(MouseEvent.ROLL_OVER, rollOver); |
14 |
addEventListener(MouseEvent.ROLL_OUT, rollOut); |
15 |
|
16 |
//this guarantees that the cursor changes to the little pointing hand over the symbol
|
17 |
buttonMode = true; |
18 |
useHandCursor = true; |
19 |
|
20 |
//set its initial alpha to 0.5
|
21 |
alpha = 0.5; |
Langkah 11: ThemeButton
Simbol ini akan digunakan untuk ikon Anda dapat mengklik untuk memilih kategori bertema yang berbeda - apa kejutan:). Hal ini mirip dengan SmallPicButton, jadi untuk menciptakan itu ulangi langkah 8-10 - saja, mengubah nama dan kelas untuk ThemeButton daripada SmallPicButton. Anda tidak perlu mengubah apa pun dalam kode.
Langkah 12: Apa yang kita miliki sejauh
Jadi, kami membuat dua lapisan: atas adalah BigPic + judul + Arrows, Bagian bawah salah satu tema + jempol.
Pada lapisan BigPic + judul + Arrows kami memiliki img, prevArrow, nextArrow, txtField, dan pelat penutup dua di sisi di bagian bawah.
Pada tema + jempol lapisan kami memiliki dua movieklip kosong: tema, yang akan berisi semua ikon tema, dan smallPicsMc, yang akan berisi semua thumbnail.
Dalam Perpustakaan, kami memiliki SmallPicButton dan simbol-simbol ThemeButton dengan benar mengatur hubungan pilihan.
Saya berharap Anda dengan saya sejauh ini. Sangat penting untuk memiliki simbol-simbol yang bernama dengan benar, karena jika tidak kode mendatang tidak akan bekerja.
Langkah 13: Mengatur struktur Folder
Semua hak - jadi kami selesai dengan bagian Flash. Kami memiliki dua hal yang harus dilakukan sebelum kita mulai pengkodean: meletakkan semua gambar dalam folder dapat digunakan struktur dan membuat file XML kami.
Dalam folder yang sama sebagai file FLA Anda, membuat sebuah folder bernama 'foto'. Dalam folder itu membuat dua lainnya: salah satu dari mereka disebut 'kecil' (untuk mengandung thumbnail) dan lainnya disebut 'besar' (untuk memuat gambar besar). Dalam folder kedua Anda akan memiliki struktur map yang sama: satu folder untuk setiap tema.



Langkah 14: File gambar
Masing-masing thumbnail harus memiliki nama yang sama sebagai gambar besar yang sesuai. Penting bahwa dalam folder 'kecil' Semua gambar harus 80px tinggi agar sesuai UI kami. Ukuran gambar besar ini tidak penting, karena kita akan merubah ukuran mereka sebelum menambahkannya ke panggung, namun perlu diingat bahwa ukuran maksimum gambar kita akan mampu menunjukkan adalah 840px oleh 420px.



Langkah 15: Membuat XML File
Buat sebuah file teks baru dalam program manajer file dan nama itu 'pics.xml'.
Anda dapat mengedit file ini dengan setiap editor teks seperti Notepad, tapi itu jauh lebih mudah untuk mengeditnya dengan Flash.
Langkah 16: Mengedit XML File
Saya hanya akan memberitahu Anda hal-hal yang kita butuhkan untuk XML untuk bekerja dengan baik, jika Anda tertarik untuk belajar lebih banyak tentang XML Anda dapat menemukan banyak tutorial di sini di Activetuts + (seperti yang satu ini oleh Druid Kepple) atau halaman lain di web - dan jangan lupa : Google adalah teman Anda ;)
Dalam file XML ini kami akan menjelaskan struktur folder (dan gambar-gambar di dalamnya), sehingga Flash dapat membaca dan menafsirkan itu.
1 |
|
2 |
<pics>
|
3 |
<theme name="The name of the theme"> |
4 |
<pic title="The title of the picture" filename="FilenameOfThePicture.jpg" /> |
5 |
</theme>
|
6 |
</pics>
|
Jadi, pics adalah elemen root, dan semuanya berjalan antara tag pics. Jika Anda ingin menambahkan tema baru, Anda hanya menyalin dan menyisipkan elemen tema yang sudah ada dan mengubah sifat-sifatnya. Jika Anda ingin menambahkan gambar baru untuk salah satu tema Anda hanya copy dan paste pic tag di dalam hak mereka, dan mengubah sifat-sifatnya. Nama atribut dari tag tema harus sama dengan nama folder, karena itulah di mana kami going untuk memberitahu Flash untuk mencari file. Nama dan nama file bersifat case-sensitive.
Langkah 17: Menciptakan kelas dokumen
Klik File > baru dan pilih ActionScript File. Copy dan paste kode berikut.
1 |
|
2 |
package { |
3 |
|
4 |
public class Gallery extends MovieClip |
5 |
{
|
6 |
|
7 |
}
|
8 |
}
|
Pada dasarnya kode ini tidak melakukan apa pun, tapi itu mengatakan bahwa kelas ini adalah dalam paket global, dan kelas umum yang disebut galeri.
Langkah 18: Constructor
Fungsi pertama kita mulai untuk menulis adalah konstruktor. Itu adalah fungsi dasar seluruh program, dan akan menjadi kode pertama yang berjalan ketika SWF dimulai.
Copy dan paste kode ini antara kurung kurawal yang pergi dengan garis "kelas publik galeri meluas movieklip":
1 |
|
2 |
public function Gallery():void |
3 |
{
|
4 |
|
5 |
}
|
Langkah 19: URLLoader
Hal pertama yang perlu kita lakukan adalah untuk memuat file XML yang kita buat. Kita akan melakukannya dengan bantuan URLLoader kelas, yang dalam paket flash.net.
Untuk menggunakannya, kita perlu untuk impor paket. Sebenarnya, mari kita hanya mengimpor setiap paket yang akan kita butuhkan. Salinan dan berikut kode dan paste di atas garis "kelas publik galeri meluas movieklip":
1 |
|
2 |
import flash.display.*; |
3 |
import flash.events.*; |
4 |
import flash.net.*; |
5 |
import flash.text.*; |
6 |
import flash.xml.*; |
7 |
import caurina.transitions.*; |
Ingat, impor akan selalu di awal kode (setelah ' paket {' bagian), Deklarasi variabel non-lokal akan sebelum konstruktor dan semua fungsi akan setelah konstruktor.
Dalam rangka untuk Flash untuk impor paket "caurina.transitions", Anda akan perlu memiliki paket ini dalam folder yang sama sebagai FLA Anda dan kelas dokumen Anda. Jadi, membuka sumber file zip, dan Salin \caurina\ folder ke dalam folder proyek Anda.
Setelah kami telah diimpor paket kita akan membutuhkan, kita dapat menyatakan - dan membuat sebuah instance baru dari - variabel URLLoader kami:
1 |
|
2 |
var urlLoader:URLLoader = new URLLoader(); |
(Menempatkan kode dalam fungsi constructor.)
Sekarang kita dapat memberitahu Flash untuk memuat file XML kami, dan menambahkan acara pendengar untuk urlLoader:
1 |
|
2 |
public function Gallery():void |
3 |
{
|
4 |
urlLoader.load(new URLRequest("pics.xml")); |
5 |
urlLoader.addEventListener(Event.COMPLETE, urlLoadComplete); |
6 |
}
|
Jadi ketika urlLoader selesai loading pics.xml, Flash akan menjalankan fungsi urlLoadComplete, yang merupakan hal berikutnya kami menulis...
Langkah 20: UrlLoadComplete fungsi
Ketika XML file dimuat kita akan memiliki untuk menempatkan semua info yang berisi ke objek XML. Kemudian, untuk menggunakan itu, kita akan membutuhkan sebuah objek XMLList terlalu:
1 |
|
2 |
//these are non-local variables
|
3 |
var xml:XML; |
4 |
var list:XMLList; |
Setelah konstruktor paste ini:
1 |
|
2 |
protected function urlLoadComplete(e:Event):void |
3 |
{
|
4 |
xml = new XML(urlLoader.data); |
5 |
list = new XMLList(xml.children()); |
6 |
}
|
Sekarang, Semua anak node elemen pics - yaitu semua tema Tag - berada dalam daftar variabel. Jika Anda ingin melihat apa yang ada di sana, paste 'trace(list);' setelah baris kedua fungsi urlLoadComplete.
Langkah 21: Membuat tombol tema
Untuk melakukan ini kita akan membutuhkan untuk loop. Jadi pertama kita perlu mendeklarasikan variabel loop untuk loop untuk bekerja dengan baik dan array untuk objek themeButton:
1 |
|
2 |
//these are non-local variables
|
3 |
var themeButtons:Array = new Array(); |
4 |
var i:int; |
Setelah dua baris sudah ada fungsi urlLoadComplete kita harus mulai menulis loop kami:
1 |
|
2 |
for(i = 0; i< list.length(); i++) |
3 |
{
|
4 |
//we create a new instance of the ThemeButton symbol
|
5 |
var themeButton:MovieClip = new ThemeButton(); |
6 |
//we set the content of the textfield to the name of the theme
|
7 |
themeButton.txt.text = list[i].@name; |
8 |
//we set the name of the textfield instance to be the same as its content
|
9 |
themeButton.name = list[i].@name; |
10 |
//we add the click eventListener
|
11 |
themeButton.addEventListener(MouseEvent.CLICK, themeClick); |
12 |
//we place the clips in a row with 10px padding
|
13 |
themeButton.x = i*(themeButton.width+10); |
14 |
//we put the movieclip in the themes MC
|
15 |
themes.addChild(themeButton); |
16 |
//...and in the themeButtons array also
|
17 |
themeButtons[i] = themeButton; |
18 |
}
|
Setelah ini, kami menempatkan semua tombol dalam tema MC, maka kita menyelaraskan ke pusat tahap:
1 |
|
2 |
//change 450 to match the width of your stage
|
3 |
themes.x = 450-themes.width/2; |
Anda dapat menguji film Anda sekarang dan melihat apakah ia bekerja. Anda akan melihat tombol tema Anda (bekerja seperti tombol) di baris...
Langkah 22: Pindah themeButtons
Untuk memiliki themeButtons yang bergerak, kita menambah mouseMove eventListener tahap; paste ini dalam konstruktor:
1 |
|
2 |
stage.addEventListener(MouseEvent.MOUSE_MOVE, slideTheme); |
Sebelum kita menulis fungsi slideTheme, kita mendeklarasikan variabel non-lokal lain:
1 |
|
2 |
//this will help calculate the position to which the themes should slide
|
3 |
var ratio:Number; |
Dalam slideTheme fungsi kita akan menggunakan Tweener kelas - secara khusus, salah satu fungsi dari kelas itu, yang disebut addTween(). (Anda dapat membaca semua tentang kelas ini di sini.)
1 |
|
2 |
protected function slideTheme(e:MouseEvent):void |
3 |
{
|
4 |
if(mouseY>470 && mouseY< 600)//we move the themeButtons if the cursor is the themeButtons-thumbs area |
5 |
{
|
6 |
ratio = themes.width/850; |
7 |
//we move the themes MC if it is longer then the stage
|
8 |
if(themes.width > 900) |
9 |
{
|
10 |
//if it is longer, the position depends on the cursor's X position
|
11 |
Tweener.addTween(themes, {x: (450-themes.width/2)-(mouseX-450)*ratio, time: 1}); } |
12 |
else
|
13 |
{
|
14 |
//otherwise, we align it to the center of the stage
|
15 |
Tweener.addTween(themes, {x: (450-themes.width/2), time: 1}); |
16 |
}
|
17 |
}
|
18 |
}
|
Menguji film Anda; tema MC harus bergerak sekarang (jika itu lebih lama daripada tahap).
Langkah 23: Variabel untuk themeClick fungsi
Sebelum kita mulai untuk menulis fungsi, kita perlu menyatakan beberapa variabel lebih non-lokal:
1 |
|
2 |
var j:int; //another loop-variable |
3 |
//if we click on one of the theme icons, it will be contained in this variable
|
4 |
var themeClicked:MovieClip = new ThemeButton(); |
5 |
//the list of the pictures in a particular theme
|
6 |
var picsList:XMLList; |
7 |
var smallPicSource:Array; //the thumb's source |
8 |
var bigPicSource:Array; //the big picture's source |
9 |
//this will contain all the thumbs
|
10 |
var smallPics:Array; |
11 |
//this will contain the titles of the pictures in the selected theme
|
12 |
var picTitles:Array; |
Langkah 24: themeClick() Reset negara
Awal fungsi ini menangani ulang hal-hal ketika Anda mengklik salah satu themeButtons.
1 |
|
2 |
protected function themeClick(e:MouseEvent):void |
3 |
{
|
4 |
//the whole function shall be executed only if the theme you selected is not the same as you selected just before
|
5 |
if (themeClicked != e.currentTarget) |
6 |
{
|
7 |
//makes the smallPicsMc invisible - it's on the stage, but it contains nothing yet
|
8 |
smallPicsMc.visible = false; |
9 |
//removes the mouseMove eventListener - added afterwards
|
10 |
stage.removeEventListener(MouseEvent.MOUSE_MOVE, slideSmallPics); |
11 |
j = 0; //resets the loop counter to zero |
Langkah 25: themeClick() jelas gambar dan teks
Jika ada gambar pada tahap (dalam img Mc), akan dihapus, dan bidang teks judul akan dihapus:
1 |
|
2 |
if(img.numChildren > 0) |
3 |
{
|
4 |
img.removeChildAt(0); |
5 |
}
|
6 |
titleTxt.text = ""; |
Langkah 26: themeClick() ingat Clicked tema
Bagian ini menjamin bahwa program akan mampu 'ingat' tema yang Anda mengklik, dengan menyimpan referensi dalam variabel themeClicked.
1 |
|
2 |
if (themeClicked != null){ //if it's not the first time you click on a theme |
3 |
themeClicked.alpha = 0.5; //the previous themeButton's alpha is set to 50% themeClicked.addEventListener(MouseEvent.ROLL_OUT, themeClicked.rollOut); //its rollOut eventListener is re-added |
4 |
}
|
5 |
themeClicked = MovieClip(e.currentTarget); //the content of the themeClicked MC is changed to the themeButton you've just clicked on |
6 |
themeClicked.alpha = 1; //its alpha is set to 100% |
7 |
themeClicked.removeEventListener(MouseEvent.ROLL_OUT, themeClicked.rollOut); //...and its rollOut eventListener is removed |
Langkah 27: themeClick() Filter XML
Bagian selanjutnya dari fungsi ini akan menyaring konten daftar hanya unsur-unsur dari tema yang dipilih:
1 |
|
2 |
//loop goes through the themeButtons array
|
3 |
for (i=0; i< themeButtons.length; i++) |
4 |
{
|
5 |
//if the name of the themeButton you clicked on matches the current item in the array
|
6 |
if (e.currentTarget.name == themeButtons[i].name) |
7 |
{
|
8 |
//we make picsList contain all the <pic> elements from that theme
|
9 |
picsList = new XMLList(list[i].pic); |
10 |
}
|
11 |
}
|
Langkah 28: themeClick() penghapusan
Jika smallPicsMc berisi item (yaitu Anda sudah mengklik pada beberapa tema), kami menghapus mereka semua.
1 |
|
2 |
if (smallPicsMc.numChildren != 0) |
3 |
{
|
4 |
for(i=smallPicsMc.numChildren; i>0; i--) |
5 |
{
|
6 |
smallPicsMc.removeChildAt(i-1); |
7 |
}
|
8 |
}
|
Langkah 29: themeClick() mengisi array
Jika dipilih tema memiliki setidaknya satu gambar, kita mengisi sumber array dan array judul dan beban jempol.
1 |
|
2 |
if (picsList.length() > 0)// if the picsList XMLList has at least 1 element in it |
3 |
{
|
4 |
//we create new arrays
|
5 |
//(if they have already been created, we clear their contents)
|
6 |
smallPicSource = new Array(); |
7 |
bigPicSource = new Array(); |
8 |
smallPics = new Array(); |
9 |
picTitles = new Array(); |
10 |
|
11 |
//loop through picsList
|
12 |
for(i=0; i< picsList.length(); i++) |
13 |
{
|
14 |
//now we create the source path of the images
|
15 |
smallPicSource[i] = "pics/small/" + e.currentTarget.name +"/" + picsList.@filename[i]; |
16 |
bigPicSource[i] = "pics/big/" + e.currentTarget.name +"/" + picsList.@filename[i]; |
17 |
//put the titles in the picTitles array
|
18 |
picTitles[i] = picsList.@title[i]; |
19 |
}
|
20 |
|
21 |
j = -1; |
22 |
//this will eventually load the small pics
|
23 |
smallPicLoad(null); |
Langkah 30: themeClick() berurusan dengan tema kosong
Jika thema yang dipilih tidak memiliki gambar apapun di dalamnya, kami memberitahu pengguna bahwa itu akan datang segera.
1 |
|
2 |
else
|
3 |
{
|
4 |
//if img had a picture in it...
|
5 |
if(img.numChildren > 0) |
6 |
{
|
7 |
img.removeChildAt(0); //...we remove it |
8 |
}
|
9 |
//then we write, instead of the title, that this theme is coming soon
|
10 |
titleTxt.text = "Coming soon..."; |
11 |
}
|
Langkah 31: SlideSmallPics fungsi
Fungsi ini melakukan hal yang sama sebagai fungsi slideTheme, tetapi dengan smallPicsMc bukan tema MC.
1 |
|
2 |
protected function slideSmallPics(e:MouseEvent):void |
3 |
{
|
4 |
if(mouseY>470 && mouseY< 600){ |
5 |
ratio = smallPicsMc.width/850; |
6 |
if(smallPicsMc.width > 900) |
7 |
{
|
8 |
Tweener.addTween(smallPicsMc, {x: (450-smallPicsMc.width/2)-(mouseX-450)*ratio, time: 1}); |
9 |
}
|
10 |
else
|
11 |
{
|
12 |
Tweener.addTween(smallPicsMc, {x: (450-smallPicsMc.width/2), time: 1}); |
13 |
}
|
14 |
}
|
15 |
}
|
Langkah 32: Fungsi smallPicLoad
Fungsi ini banyak ibu jari dalam smallPics array dan menampilkan proses loading.
1 |
|
2 |
protected function smallPicLoad(e:Event):void |
3 |
{
|
4 |
if (j < smallPicSource.length-1) |
5 |
{
|
6 |
j++; |
7 |
//it tells the user which thumbs are loading now
|
8 |
loadTxt.text = "Loading thumbs: " + (j+1).toString() +" / " + (smallPicSource.length).toString(); |
9 |
//creates a Loader object in the array
|
10 |
smallPics[j] = new Loader(); |
11 |
//loads the next element of the smallPicSource array
|
12 |
smallPics[j].load(new URLRequest(smallPicSource[j])); |
13 |
//if it's loaded, call this function again
|
14 |
smallPics[j].contentLoaderInfo.addEventListener(Event.COMPLETE, smallPicLoad); |
15 |
}
|
16 |
else
|
17 |
{
|
18 |
//if all of the elements of the array are loaded, call the smallPicComplete function
|
19 |
smallPicComplete(); |
20 |
}
|
21 |
}
|
Ini agak sulit jadi saya akan memberikan penjelasan lebih rinci. Beban thumbnail satu per satu; seperti loop tetapi saya melakukan perulangan 'secara manual', hampir secara rekursif. Solusi ini diperlukan karena saya ingin thumbnail untuk muncul pada waktu yang sama. Ingat bahwa kita tetapkan visibilitas smallPicsMc ke false, sebelumnya, sehingga kami bisa mengungkapkan thumbnail bersama-sama.
[/sourcecode]
Langkah 33: smallPicComplete() Loading gambar
Pertama, kita memiliki dua variabel non-lokal lainnya untuk menyatakan:
1 |
|
2 |
var bigClicked:String;//the source of the big picture |
3 |
var cpi:uint;//it stands for CurrentPictureIndex: it's the index of the picture which is shown |
Ini adalah bagian pertama dari fungsi di mana kami memuat gambaran besar pertama, kemudian isi smallPicButtons array dan smallPicsMc.
1 |
|
2 |
protected function smallPicComplete(e: Event = null):void |
3 |
{
|
4 |
bigClicked = bigPicSource[0];//sets the source of the big picture to the first one in the array |
5 |
loadPic(bigPicSource[0]);//calls the loadPic function with the same parameter |
6 |
cpi = 0; |
7 |
|
8 |
var smallPicButtons:Array = new Array();//this will store the created smallPicButtons |
9 |
|
10 |
for(i=0; i< smallPics.length; i++) |
11 |
{
|
12 |
smallPicButtons[i] = new SmallPicButton();//we create a new instance of the SmallPicButton object in the smallPicButtons array |
13 |
smallPicButtons[i].addChild(smallPics[i]);//we put the matching element of the smallPics array to the smallPicButtons array |
14 |
|
15 |
if (i>0) |
16 |
{
|
17 |
smallPicButtons[i].x = smallPicButtons[i-1].x+smallPicButtons[i-1].width+20;//beginning with the second element, we place them in a line as we did it with the themeButtons |
18 |
}
|
19 |
smallPicButtons[i].data = bigPicSource[i]; //we set its data property to the source of the big image (so that we can use it as a link) |
20 |
|
21 |
smallPicsMc.addChild(smallPicButtons[i]); //we add the button to the smallPicsMc |
22 |
smallPicButtons[i].addEventListener(MouseEvent.CLICK, click);// we add a click eventListener to that button |
23 |
}
|
Langkah 34: smallPicComplete() mengungkapkan jempol dan panah
Ini adalah paruh kedua fungsi ini. Di sini kami mengungkapkan thumbnail dan panah navigasi (dan set mereka untuk siap untuk menavigasi).
1 |
|
2 |
smallPicsMc.visible = true; //we set the visibility back to true |
3 |
smallPicsMc.alpha = 0; //...but we set its alpha to 0%, so that we can do a little fade-in animation |
4 |
Tweener.addTween(smallPicsMc, {alpha:1, time:1}); //we fade in the smallPicsMc |
5 |
stage.addEventListener(MouseEvent.MOUSE_MOVE, slideSmallPics); //...then add the mouseMove event listener to it |
6 |
|
7 |
nextArrow.addEventListener(MouseEvent.CLICK, nextPic); //add a click event listener to the navigation arrows (nextPic, prevPic functions will be explained later) |
8 |
prevArrow.addEventListener(MouseEvent.CLICK, prevPic); |
9 |
prevArrow.visible = false; //since we're about to reveal the first picture there won't be a previous one, so we don't need the prevArrow yet |
10 |
nextArrow.visible = true; //we do need the nextArrow though |
11 |
prevArrow.buttonMode = true; |
12 |
nextArrow.buttonMode = true; |
Setelah komentar baris yang mana kita sebut fungsi kita belum ditulis belum, Anda akan dapat untuk menguji film Anda, klik pada tombol tema dan thumbnail muncul dan geser.
Langkah 35: NextPic fungsi
Fungsi ini memuat gambar berikutnya berturut-turut. Hal ini dipicu dengan mengklik nextArrow.
1 |
|
2 |
protected function nextPic(e:Event = null):void |
3 |
{
|
4 |
if (cpi < bigPicSource.length-1)//if there is one more picture in the bigPicSource array |
5 |
{
|
6 |
cpi++; |
7 |
loadPic(bigPicSource[cpi]); //load next pic |
8 |
}
|
9 |
}
|
Langkah 36: PrevPic fungsi
Fungsi ini memuat gambar sebelumnya di baris. Itu adalah reverse nextPic fungsi, dan dipicu dengan mengklik prevArrow.
1 |
|
2 |
protected function nextPic(e:Event = null):void |
3 |
{
|
4 |
if (cpi > 0)//if it's not the first picture we want to load |
5 |
{
|
6 |
cpi--; |
7 |
loadPic(bigPicSource[cpi]); //load previous pic |
8 |
}
|
9 |
}
|
Langkah 37: Klik fungsi
Variabel non-lokal lain untuk menyatakan:
1 |
|
2 |
var bigPic:String; //this will store the source path of the big image |
Ini adalah fungsi yang disebut dengan mengklik thumbnail:
1 |
|
2 |
protected function click(e:MouseEvent):void |
3 |
{
|
4 |
//if this picture is not the same as shown already
|
5 |
if (bigClicked != e.currentTarget.data) |
6 |
{
|
7 |
if(img.numChildren > 0) //if there is already something in the img MC |
8 |
{
|
9 |
img.removeChildAt(0); //we remove it |
10 |
}
|
11 |
titleTxt.text = ""; //we set the title text field to empty |
12 |
//we store the source path of the big picture of the thumbnail
|
13 |
bigClicked = e.currentTarget.data; |
14 |
bigPic = e.currentTarget.data; |
15 |
loadPic(bigPic); //pass the source path of the big pic to loadPic() |
16 |
}
|
17 |
}
|
Langkah 38: loadPic() kliring yang lama
Fungsi ini banyak gambar besar dan mengurus navigasi panah.
Pertama kita perlu mendeklarasikan sebuah Loader yang akan memuat gambar...
1 |
|
2 |
var bigLoader:Loader; //this will load the big picture |
.. .dan sekarang, sebenarnya fungsi:
1 |
|
2 |
protected function loadPic(bigPic):void |
3 |
{
|
4 |
if(img.numChildren > 0) //if this is not the first picture we load |
5 |
{
|
6 |
img.removeChildAt(0); //we clear the previous one |
7 |
}
|
8 |
titleTxt.text = ""; //and clear the title too |
Langkah 39: loadPic() Loading gambar
Kemudian kita ciptakan Loader object, mulai loading, dan menambahkan beberapa pendengar.
1 |
|
2 |
bigLoader = new Loader(); //we create the Loader object |
3 |
bigLoader.load(new URLRequest(bigPic)); //we start to load the big picture -- remember bigPic contains the source path of the big image |
4 |
bigLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progress); //call progress() whenever a bit more is loaded |
5 |
bigLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, bigLoadComplete); //and if the big picture is loaded we call the bigLoadComplete function |
Langkah 40: loadPic() angka keluar gambar indeks
Di sini kita memilih indeks kanan gambar kami dimuat. Hal ini penting karena cpi menjelaskan apa gambar yang kita berada di, dan apa judul untuk menunjukkan.
1 |
|
2 |
for (i=0; i< bigPicSource.length; i++) |
3 |
{
|
4 |
if (bigPic == bigPicSource[i]) //if the source path of the images match |
5 |
{
|
6 |
cpi = i; //we set the cpi to that index |
7 |
}
|
8 |
}
|
Langkah 41: loadPic() menampilkan atau menyembunyikan Nav Panah
Apakah navigasi panah tersembunyi atau mengungkapkan tergantung pada nilai cpi.
1 |
|
2 |
if (cpi > 0) //if this is not the first picture |
3 |
prevArrow.visible = true; //we show the prevArrow |
4 |
if (cpi == 0) //if this is the first picture |
5 |
prevArrow.visible = false; //we hide it |
6 |
|
7 |
if (cpi == bigPicSource.length-1) //if this is the last one |
8 |
nextArrow.visible = false; //we hide the nextArrow |
9 |
if (cpi < bigPicSource.length-1) //if this is not the last one |
10 |
nextArrow.visible = true; //we reveal it |
Langkah 42: Beban kemajuan Handler
Fungsi ini dijalankan setiap kali sepotong baru gambar di-load.
1 |
|
2 |
protected function progress(e: ProgressEvent): void |
3 |
{
|
4 |
//we write the percentage of the loading progress into the loadTxt dynamic textfield
|
5 |
loadTxt.text = "Loading picture: " + Math.floor((e.bytesLoaded / e.bytesTotal) * 100) + "%"; |
6 |
}
|
Langkah 43: bigLoadComplete() Setup
Hal pertama yang melakukan fungsi ini adalah mengatur isi bidang teks, dan kemudian menetapkan dan mendapatkan beberapa properti gambar dimuat.
1 |
|
2 |
protected function bigLoadComplete(e:Event):void |
3 |
{
|
4 |
//the loading has ended and we don't need this text till the next loading
|
5 |
loadTxt.text = ""; |
6 |
//we use cpi to get the title that belongs to this picture
|
7 |
titleTxt.text = picTitles[cpi]; |
8 |
|
9 |
//we create a new bitmap object, and we put the picture in it
|
10 |
var bitmap:Bitmap = e.target.content; |
11 |
bitmap.smoothing = true; |
12 |
var origWidth:Number = bitmap.width; |
13 |
var origHeight:Number = bitmap.height; |
Langkah 44: bigLoadComplete() lebih variabel
Kami membuat beberapa variabel baru sehingga kita dapat mengubah ukuran gambar dengan benar. Mereka harus pergi di dalam fungsi bigLoadComplete().
1 |
|
2 |
var maxWidth = 840; //the width |
3 |
var maxHeight = 420; //and the height of the space where we want to place the picture |
4 |
// -- it depends on your layout
|
5 |
|
6 |
//these will store the new size of the image
|
7 |
var newWidth:Number; |
8 |
var newHeight:Number; |
9 |
|
10 |
//this variable is the ratio of the sides of the image
|
11 |
//we will do the resizing with the help of this ratio
|
12 |
var ratio:Number = origWidth / origHeight; |
Langkah 45: bigLoadComplete() mengatur ukuran gambar
Bagian ini akan mengatur ukuran baru gambar.
1 |
|
2 |
//if one of the sides of the picture is longer than the space
|
3 |
if (origWidth > maxWidth || origHeight > maxHeight) |
4 |
{
|
5 |
//if the width-difference is bigger then the height-difference (portrait)
|
6 |
if (maxWidth - origWidth > maxHeight - origHeight) |
7 |
{
|
8 |
newHeight = maxHeight; //we set the height to the maximum |
9 |
newWidth = Math.round (newHeight * ratio); //then we set the length of the other side accordingly |
10 |
}
|
11 |
else //if the orientation is landscape or shaped like a square |
12 |
{
|
13 |
newWidth = maxWidth; //we set the width to the maximum |
14 |
newHeight = Math.round(newWidth / ratio); //then we set the length of the other side |
15 |
}
|
16 |
}
|
17 |
else //if the picture is smaller then the space |
18 |
{
|
19 |
//the new size will be the same as the original
|
20 |
newHeight = origHeight; |
21 |
newWidth = origWidth; |
22 |
}
|
Langkah 46: bigLoadComplete() mengungkapkan gambar
Kami menetapkan sifat bitmap, kemudian mengungkapkan hal itu di atas panggung.
1 |
|
2 |
bitmap.width = newWidth; //set the resized width |
3 |
bitmap.height = newHeight; //and height of the image |
4 |
bitmap.x = -bitmap.width/2; //we set the x |
5 |
bitmap.y = -bitmap.height/2; //and y coordinates |
6 |
//(since the img MC is in the center we should align the bitmap to the center of the MC)
|
7 |
|
8 |
img.addChild(bitmap); //we put the bitmap in the MC |
9 |
|
10 |
img.alpha = 0; //we set the alpha of the img MC to 0% so that we can fade it in |
11 |
Tweener.addTween(img, {alpha:1, time:1}); //we fade in the img MC |
Kesimpulan
Baiklah! Kami selesai sekarang! Saya harap Anda bisa memahami ide-ide ini tut (dan menikmatinya juga).
Terima kasih untuk membaca!
Akhirnya di sini datang seluruh kode:
1 |
|
2 |
package { |
3 |
|
4 |
import flash.display.*; |
5 |
import flash.events.*; |
6 |
import flash.net.*; |
7 |
import flash.text.*; |
8 |
import flash.xml.*; |
9 |
import caurina.transitions.*; |
10 |
|
11 |
public class Gallery extends MovieClip |
12 |
{
|
13 |
var urlLoader:URLLoader = new URLLoader(); |
14 |
var xml:XML; |
15 |
var list:XMLList; |
16 |
var picsList:XMLList; |
17 |
|
18 |
var themeButtons:Array = new Array(); |
19 |
var themeClicked:MovieClip = new ThemeButton(); |
20 |
|
21 |
var i:int; |
22 |
var j:int; |
23 |
|
24 |
var smallPicSource:Array; |
25 |
var bigPicSource:Array; |
26 |
var smallPics:Array; |
27 |
var picTitles:Array; |
28 |
|
29 |
var bigLoader:Loader; |
30 |
|
31 |
var bigPic:String; |
32 |
var bigClicked:String; |
33 |
|
34 |
var cpi:uint; |
35 |
|
36 |
var ratio:Number |
37 |
|
38 |
public function Gallery():void |
39 |
{
|
40 |
urlLoader.load(new URLRequest("pics.xml")); |
41 |
urlLoader.addEventListener(Event.COMPLETE, urlLoadComplete); |
42 |
|
43 |
stage.addEventListener(MouseEvent.MOUSE_MOVE, slideTheme); |
44 |
}
|
45 |
|
46 |
protected function urlLoadComplete(e:Event):void{ |
47 |
xml = new XML(urlLoader.data); |
48 |
list = new XMLList(xml.children()); |
49 |
for(i = 0; i 0) |
50 |
img.removeChildAt(0); |
51 |
titleTxt.text = ""; |
52 |
|
53 |
if (themeClicked != null){ |
54 |
themeClicked.alpha = 0.5; |
55 |
themeClicked.addEventListener(MouseEvent.ROLL_OUT, themeClicked.rollOut); |
56 |
}
|
57 |
themeClicked = MovieClip(e.currentTarget); |
58 |
themeClicked.alpha = 1; |
59 |
themeClicked.removeEventListener(MouseEvent.ROLL_OUT, themeClicked.rollOut); |
60 |
for (i=0; i0; i--) |
61 |
smallPicsMc.removeChildAt(i-1); |
62 |
}
|
63 |
|
64 |
if (picsList.length() > 0) |
65 |
{
|
66 |
smallPicSource = new Array(); |
67 |
bigPicSource = new Array(); |
68 |
smallPics = new Array(); |
69 |
picTitles = new Array(); |
70 |
|
71 |
for(i=0; i 0) |
72 |
img.removeChildAt(0); |
73 |
titleTxt.text = "Coming soon..."; |
74 |
}
|
75 |
}
|
76 |
}
|
77 |
|
78 |
protected function smallPicLoad(e:Event):void |
79 |
{
|
80 |
if (j < smallPicSource.length-1) |
81 |
{
|
82 |
j++; |
83 |
loadTxt.text = "Loading thumbs: " + (j+1).toString() +" / " + (smallPicSource.length).toString(); |
84 |
smallPics[j] = new Loader(); |
85 |
smallPics[j].load(new URLRequest(smallPicSource[j])); |
86 |
smallPics[j].contentLoaderInfo.addEventListener(Event.COMPLETE, smallPicLoad); |
87 |
}else |
88 |
{
|
89 |
smallPicComplete(); |
90 |
}
|
91 |
}
|
92 |
|
93 |
protected function smallPicComplete(e: Event = null):void{ |
94 |
bigClicked = bigPicSource[0]; |
95 |
loadPic(bigPicSource[0]); |
96 |
cpi = 0; |
97 |
|
98 |
var smallPicButtons:Array = new Array(); |
99 |
|
100 |
for(i=0; i0) |
101 |
smallPicButtons[i].x = smallPicButtons[i-1].x+smallPicButtons[i-1].width+20; |
102 |
smallPicButtons[i].data = bigPicSource[i]; |
103 |
|
104 |
smallPicsMc.addChild(smallPicButtons[i]); |
105 |
smallPicButtons[i].addEventListener(MouseEvent.CLICK, click); |
106 |
}
|
107 |
|
108 |
smallPicsMc.visible = true; |
109 |
smallPicsMc.alpha = 0; |
110 |
Tweener.addTween(smallPicsMc, {alpha:1, time:1}); |
111 |
stage.addEventListener(MouseEvent.MOUSE_MOVE, slideSmallPics); |
112 |
|
113 |
nextArrow.addEventListener(MouseEvent.CLICK, nextPic); |
114 |
prevArrow.addEventListener(MouseEvent.CLICK, prevPic); |
115 |
prevArrow.visible = false; |
116 |
nextArrow.visible = true; |
117 |
prevArrow.buttonMode = true; |
118 |
nextArrow.buttonMode = true; |
119 |
}
|
120 |
|
121 |
protected function nextPic(e:Event = null):void |
122 |
{
|
123 |
if (cpi < bigPicSource.length-1) |
124 |
{
|
125 |
cpi++; |
126 |
loadPic(bigPicSource[cpi]); |
127 |
}
|
128 |
}
|
129 |
|
130 |
protected function prevPic(e:Event = null):void |
131 |
{
|
132 |
if (cpi > 0) |
133 |
{
|
134 |
cpi--; |
135 |
loadPic(bigPicSource[cpi]); |
136 |
}
|
137 |
}
|
138 |
|
139 |
protected function loadPic(bigPic):void |
140 |
{
|
141 |
if(img.numChildren > 0) |
142 |
img.removeChildAt(0); |
143 |
titleTxt.text = ""; |
144 |
|
145 |
bigLoader = new Loader(); |
146 |
bigLoader.load(new URLRequest(bigPic)); |
147 |
bigLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progress); |
148 |
bigLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, bigLoadComplete); |
149 |
for (i=0; i 0) |
150 |
prevArrow.visible = true; |
151 |
if (cpi == 0) |
152 |
prevArrow.visible = false; |
153 |
|
154 |
if (cpi == bigPicSource.length-1) |
155 |
nextArrow.visible = false; |
156 |
if (cpi < bigPicSource.length-1) |
157 |
nextArrow.visible = true; |
158 |
}
|
159 |
|
160 |
protected function progress(e: ProgressEvent): void { |
161 |
loadTxt.text = "Loading picture: " + Math.floor((e.bytesLoaded / e.bytesTotal) * 100) + "%"; |
162 |
}
|
163 |
|
164 |
protected function bigLoadComplete(e:Event):void{ |
165 |
loadTxt.text = ""; |
166 |
titleTxt.text = picTitles[cpi]; |
167 |
|
168 |
var bitmap:Bitmap = e.target.content; |
169 |
bitmap.smoothing = true; |
170 |
var origWidth:Number = bitmap.width; |
171 |
var origHeight:Number = bitmap.height; |
172 |
|
173 |
var maxWidth = 840; |
174 |
var maxHeight = 420; |
175 |
|
176 |
var newWidth:Number; |
177 |
var newHeight:Number; |
178 |
|
179 |
var ratio:Number = origWidth / origHeight; |
180 |
|
181 |
if (origWidth > maxWidth || origHeight > maxHeight) |
182 |
{
|
183 |
if (maxWidth - origWidth > maxHeight - origHeight) |
184 |
{
|
185 |
newHeight = maxHeight; |
186 |
newWidth = Math.round(newHeight * ratio); |
187 |
}else |
188 |
{
|
189 |
newWidth = maxWidth; |
190 |
newHeight = Math.round(newWidth / ratio); |
191 |
}
|
192 |
}else |
193 |
{
|
194 |
newHeight = origHeight; |
195 |
newWidth = origWidth; |
196 |
}
|
197 |
|
198 |
bitmap.width = newWidth; |
199 |
bitmap.height = newHeight; |
200 |
bitmap.x = -bitmap.width/2; |
201 |
bitmap.y = -bitmap.height/2; |
202 |
|
203 |
img.addChild(bitmap); |
204 |
|
205 |
img.alpha = 0; |
206 |
Tweener.addTween(img, {alpha:1, time:1}); |
207 |
}
|
208 |
protected function click(e:MouseEvent):void |
209 |
{
|
210 |
if (bigClicked != e.currentTarget.data){ |
211 |
if(img.numChildren > 0) |
212 |
img.removeChildAt(0); |
213 |
titleTxt.text = ""; |
214 |
bigClicked = e.currentTarget.data; |
215 |
bigPic = e.currentTarget.data; |
216 |
loadPic(bigPic); |
217 |
}
|
218 |
}
|
219 |
|
220 |
protected function slideSmallPics(e:MouseEvent):void |
221 |
{
|
222 |
if(mouseY>470 && mouseY< 600){ |
223 |
ratio = smallPicsMc.width/850; |
224 |
if(smallPicsMc.width > 900) |
225 |
{
|
226 |
Tweener.addTween(smallPicsMc, {x: (450-smallPicsMc.width/2)-(mouseX-450)*ratio, time: 1}); |
227 |
}
|
228 |
else
|
229 |
{
|
230 |
Tweener.addTween(smallPicsMc, {x: (450-smallPicsMc.width/2), time: 1}); |
231 |
}
|
232 |
}
|
233 |
}
|
234 |
|
235 |
protected function slideTheme(e:MouseEvent):void |
236 |
{
|
237 |
if(mouseY>470 && mouseY< 600){ |
238 |
ratio = themes.width/850; |
239 |
if(themes.width > 900) |
240 |
{
|
241 |
Tweener.addTween(themes, {x: (450-themes.width/2)-(mouseX-450)*ratio, time: 1}); |
242 |
}
|
243 |
else
|
244 |
{
|
245 |
Tweener.addTween(themes, {x: (450-themes.width/2), time: 1}); |
246 |
}
|
247 |
}
|
248 |
}
|
249 |
}
|
250 |
}
|