我怎样才能删除大量重复代码?

时间:2014-04-19 11:33:29

标签: actionscript-3 flash

我发布了几次关于我在AS3(罗马时报和现在之间的欧洲动态地图)的项目,并且我还在学习。

我正在制作这种名单(按钮)列表,点击后会打开传记。 在我链接的example.fla中,你会发现我遇到麻烦的代码部分。它的工作原理是这样,但我有超过500个BIOS,我想知道是否有其他方式,重复性较低。

以下是代码:

var char_array_rom: Array = [auguste, tibere];
var textes_array: Array = [hero, life, short, comment, pop, photo];

close_bio_btn.visible = false;

for each(var char: MovieClip in char_array_rom) {
char.visible = false;
};

//___________________________________________________________________//

/* Close Kings Bio */

close_bio_btn.addEventListener(MouseEvent.CLICK, fl_close_bio);
function fl_close_bio(MouseEvent: Event): void {
close_bio_btn.visible = false;
for each(var texte: TextField in textes_array) {
    texte.visible = false;
};
for each(var char: MovieClip in char_array_rom) {
    char.visible = false;
};
};

//____________________________________________________________________//


/*Rome*/

/* A */

/*Auguste*/
auguste_list.addEventListener(MouseEvent.CLICK, fl_auguste);
function fl_auguste(MouseEvent: Event): void {
close_bio_btn.visible = true;
auguste.visible = true;
for each(var texte: TextField in textes_array) {
    texte.visible = true;
}
};

/* T */

/*Tibère*/
tibere_list.addEventListener(MouseEvent.CLICK, fl_tibere);
function fl_tibere(MouseEvent: Event): void {
close_bio_btn.visible = true;
tibere.visible = true;
for each(var texte: TextField in textes_array) {
    texte.visible = true;
}
};

我是否必须一遍又一遍地复制/粘贴/调整有关角色的标志,还是有另一种方法可以做到这一点?谢谢。

如果你想看到代码在运行中我已经为这个场合做了这个example.fla;)

https://www.dropbox.com/s/ndpa920u0mg2pbs/example.fla

1 个答案:

答案 0 :(得分:0)

您可以使用Event::target属性来确定单击了哪个英雄列表元素,然后您应该有Dictionary或两个数组或任何类型的关系结构,它们将保存列表元素之间的关联数据和英雄。然后你得到基于e.target的关系,检索相应的英雄,并获得相应的文本数据。由于你已经有一个英雄列表,你可能决定制作那些someHero_list影片剪辑的相应列表,以便索引0处的元素对应于索引0的英雄等等。像这样:

var char_array: Array = [auguste, tibere, louis_xiv, angela_merkel, henry_viii];
// etc, you can use one array for such a thing
var list_array:Array = [auguste_list, tibere_list, louis_xiv_list, angela_merkel_list, henry_viii_list];
// etc, make sure each list element stands at the same index as the referring hero
var visibleHero:MovieClip=auguste; // initialize with non-null just in case
for each (var aList:MovieClip in list_array) 
    aList.addEventListener(MouseEvent.CLICK,showHero);
closeButton.addEventListener(MouseEvent.CLICK, hideHero);
function showHero(e:Event):void {
    var listElement:DisplayObject = e.target as DisplayObject; // get which list was clicked
    var i:int = list_array.indexOf(listElement); 
    if (i<0) {
        trace('ListElement not found:',listElement.name,'using current target!');
        listElement = e.currentTarget as DisplayObject;
        if (listElement) i = list_array.indexOf(listElement); 
    }
    trace(listElement.name,i); // debug only
    if (i<0) return; // oops, not found!
    visibleHero = char_array[i]; // the hero from the other array is the one we need to show!
    visibleHero.visible=true; // now show the hero...
    for each(var texte: TextField in textes_array) {
        texte.visible = true;
    } // ... and the text fields...
    closeButton.visible=true; // ... and the button...
    ... // [TODO] ... and fill the text fields with data taken from hero.
}
function hideHero(e:Event):void {
    visibleHero.visible=false; // hide the hero...
    for each(var texte: TextField in textes_array) {
        texte.visible = false;
    } // ... and the text fields...
    closeButton.visible=false; // ... and the button
}