addEventListener with ruturn in AS3 - How?

时光毁灭记忆、已成空白 提交于 2021-01-29 08:26:54

问题


I'm a new Flash AS3 job but I have a big problem using some OOP concepts like return from methods, when I needs a Event Listener code. The problem is that the function from Event Listener cannot return a value. How can I work with this issue? This is a code sample that I using on my AIR app. This code I want to re-use in other situations that I want to parse a directory files.

private function initApp():void
{
    try
    {
        // Seta o diretoório de molduras
        var directory = diretorio_mestre.resolvePath("molduras/animacao");
        directory.getDirectoryListingAsync();
        directory.addEventListener(FileListEvent.DIRECTORY_LISTING, listaHandler);

    }
    catch (erro:ReferenceError)
    {
        mostraMensagem("Problemas com a listagem do diretório.", erro.errorID);

    }

    // Percorre arquivos
    function listaHandler(evento):void
    {
        // Contador
        var i:int = 0;

        // Conteúdo
        var contents = evento.files;

        for (i = 0; i < contents.length; i++) 
        {
            var nome:String = contents[i].name;
            var nome_array:Array = new Array();
            nome_array = nome.split("_");

            // Formata para ordenar
            arquivos_animacao.push ( { nome:contents[i].name, tamanho:contents[i].size, ordem:nome_array[0] } );

        }

        // Ordena para a ordem de númeração
        arquivos_animacao.sortOn("ordem", Array.NUMERIC);

        // Continua o processo
        // How can I return!?!?!

    }

}

回答1:


If I understand your question correctly, you want to pass extra arguments along with a dispatched event? This is a common issue with flash / air, and it just means you need to create a custom event and extend it with extra parameters.

I made a post not too long ago detailing how to do this right here: "how to implement custom events".

The main idea is you pass arguments at the end of the method sig when dispatching the event, and picking them up as an array on the receiving end.

Hope that helps




回答2:


Ok first off never nest one function inside of the other it is not OOP.

private function initApp():void{
  try{
    //Seta o diretoório de molduras
    var directory = diretorio_mestre.resolvePath("molduras/animacao");
    directory.getDirectoryListingAsync();
    directory.addEventListener(FileListEvent.DIRECTORY_LISTING, listaHandler);
  }catch(erro:ReferenceError){
    mostraMensagem("Problemas com a listagem do diretório.", erro.errorID);
  }
}


private function listaHandler(evento):void{
  var contents = evento.files;
  for (var i:int = 0; i < contents.length; i++) {
    var nome:String = contents[i].name;
    var nome_array:Array = new Array();
    nome_array = nome.split("_");
    arquivos_animacao.push ({nome:contents[i].name, tamanho:contents[i].size, ordem:nome_array[0]});
  }
  arquivos_animacao.sortOn("ordem", Array.NUMERIC);
  this.dispatchEvent( new Event("GOTRESULTS") );
}

Now where ever you instantiated the class you just listen for the GOTRESULTS event

var myClass:whatEverYouNamedIT = new whatEverYouNamedIT( );
myClass.addEventListener( "GOTRESULTS", gotResults )

function gotResults( e:Event ):void{
  trace(myClass.arquivos_animacao);
}


来源:https://stackoverflow.com/questions/6073727/addeventlistener-with-ruturn-in-as3-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!