Форум Flasher.ru

Форум Flasher.ru (http://www.flasher.ru/forum/index.php)
-   ActionScript 1.0/2.0 (http://www.flasher.ru/forum/forumdisplay.php?f=93)
-   -   помогите упростить код (http://www.flasher.ru/forum/showthread.php?t=136776)

smailik 25.02.2010 14:31

помогите упростить код
 
Вложений: 1
Сделал xml галерею вот по этим 2м урокам
http://demiart.ru/forum/index.php?showtopic=20739
http://demiart.ru/forum/index.php?showtopic=19720

Т.к. это мой первый опыт работы с xml и массивами, код на мой взгляд кажется "индийским" =). Коменты мои, и в некоторых местах я мог ошибится.

Код AS1/AS2:

var thumbBtn = 1;// определение что было нажато последним, Prev и Next или же иконка в thumbnails. 1 - thumbnails
 
function loadXML(loaded) {//после загрузки xml файла вызывается данная функция
        if (loaded) {// если файл найден
                spacing = 110;// растояние между иконками в thumbnails. Учитывается растояние самой иконки
                image = [];//массив с рисунками
                description = [];// массив с текстом
                thumbi = [];// массив с иконками
                _root.nameImage = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;//содержит строку, с именем картинки. Используется для определения строки массива.
                total = this.firstChild.childNodes.length;//количесво картинок в xml файле
 
                for (i=0; i<total; i++) {// заполнение всех массивов и присваивание иконкам в thumbinails, имена
                        this.picHolder = this.firstChild.childNodes[i];// чтобы вечно не писать this.firstChild.childNodes[i]
 
                        image[i] = this.picHolder.childNodes[0].firstChild.nodeValue;
                        description[i] = this.picHolder.childNodes[1].firstChild.nodeValue;
                        thumbi[i] = [this.picHolder.childNodes[2].firstChild.nodeValue];
 
                        this.thumbHolder = thumbnails.createEmptyMovieClip("thumbnail"+i, i);//создаются пустые клипы равные количеству иконок
                        this.thumbHolder._x = i*spacing;
                        this.thumbHolder.createEmptyMovieClip("thumbnail_image", 0).loadMovie(this.picHolder.childNodes[2].firstChild.nodeValue);// наполняются созданные клипы
                        this.thumbHolder.picture2 = this.picHolder.childNodes[0].firstChild.nodeValue;// создаются картинки
                        this.thumbHolder.title = this.picHolder.childNodes[1].firstChild.nodeValue;// создается текст к картинкам
 
                        this.thumbHolder.onRelease = function() {// код для каждой из иконок, делается из иконок кнопки.
                                picture.loadMovie(this.picture2);
                                nameImage = (this.picture2);
                                desc_txt.text = this.title;
                                thumbBtn = 1;
                                picture._alpha = 0;
                                picture_num();
                        };
                }
                firstImage();// вставляется первое изображение, чтобы не было пусто.
        } else {
                content = "file not loaded!";
        }
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("images.xml");
 
/////////////////////////////////////
listen = new Object();
listen.onKeyDown = function() {
        if (Key.getCode() == Key.LEFT) {
                prevImage();
        } else if (Key.getCode() == Key.RIGHT) {
                nextImage();
        }
};
Key.addListener(listen);
previous_btn.onRelease = function() {
        prevImage();
 
};
next_btn.onRelease = function() {
        nextImage();
};
/////////////////////////////////////
this.onEnterFrame = function() {
        filesize = picture.getBytesTotal();
        loaded = picture.getBytesLoaded();
        preloader._visible = true;
        if (loaded != filesize) {
                preloader.preload_bar._xscale = 100*loaded/filesize;
        } else {
                preloader._visible = false;
                if (picture._alpha<100) {
                        picture._alpha += 10;
                }
        }
};
function nextImage() {
        if (thumbBtn == 1) {//если последнее нажатие было на иконку, то ищется строка в массиве, сооствествующая имени файла.
                for (i in image) {
                        if (nameImage == image[i]) {
                                itsImage = i;
                                thumbBtn = 0;
                        }
                }
        }
        if (itsImage<(total-1)) {
                itsImage++;
                if (loaded == filesize) {
                        picture._alpha = 0;
                        picture.loadMovie(image[itsImage],1);
                        desc_txt.text = description[itsImage];
                        picture_num();
                }
        }
}
function prevImage() {
        if (thumbBtn == 1) {//если последнее нажатие было на иконку, то ищется строка в массиве, сооствествующая имени файла.
                for (i in image) {
                        if (nameImage == image[i]) {
                                itsImage = i;
                                thumbBtn = 0;
                        }
                }
        }
        if (itsImage>0) {
                itsImage--;
                if (loaded == filesize) {
                        picture._alpha = 0;
                        picture.loadMovie(image[itsImage],1);
                        desc_txt.text = description[itsImage];
                        picture_num();
                }
        }
}
function firstImage() {
        if (loaded == filesize) {
                picture._alpha = 0;
                picture.loadMovie(image[0],1);
                desc_txt.text = description[0];
                picture_num();
        }
}
function picture_num() {
        if (thumbBtn == 1) {//если последнее нажатие было на иконку, то ищется строка в массиве, сооствествующая имени файла.
                for (i in image) {
                        if (nameImage == image[i]) {
                                itsImage = i;
                                thumbBtn = 0;
                        }
                }
        }
        itsImage++;
        current_pos = itsImage;
        itsImage--;
        pos_txt.text = current_pos+" / "+total;
}

Прилагаю исходник с файлами, того что у меня получилось =).
Простите, забыл. Перезалил исходник.

samana 25.02.2010 15:04

В исходнике отсутствует папка thumbnails, и как я понимаю по этому маленьких превьюшек нет. Исправляйте коль хотите закончить дело!


Часовой пояс GMT +4, время: 04:43.

Copyright © 1999-2008 Flasher.ru. All rights reserved.
Работает на vBulletin®. Copyright ©2000 - 2026, Jelsoft Enterprises Ltd. Перевод: zCarot
Администрация сайта не несёт ответственности за любую предоставленную посетителями информацию. Подробнее см. Правила.