Action Script 3.0 Function does not receive parameter

2

I'm having a problem with parameters for my function. She is not getting the parameter I step into it. Here is the code below:

function gerarescala(e:MouseEvent):void
{

var tom:String = texto.text;
var escala:Array = new Array('C','C#','Db', ...);
var container:Array = new Array([vazio]); //Este array vai receber elementos de acordo com os elementos do array escala.
var pesquisa,i:Number;

[Aqui vai o Código preenchendo o array container] ...

[A função "imp", é muito extensa, então esse é só um trecho, mas é a mesma coisa em  toda a função]

 function imp(pos:String):String {

  var retorno:String;

if (pesquisa == 1)
{
    if (pos == "D#")
    {
        retorno = container[3];
    }
    else if (pos == "F")
    {
        retorno = container[6];
    }

return retorno;

  }//fim func imp

}//Fim func grarescala

In the course of the code I want to do, I'll call the function as follows: imp (container [3]); passing an element of the container array to my function, however I tested it and when I execute it I see that it returns "null" to me. I already checked with direct string entries and it worked, it just does not work with that array parameter that I pass to it. The rest of the code is all working and everything is fine, the only thing that is giving problem is with the parameter.

I noticed that the imp function is not seeing the array container, and I already put the imp function outside of the main function, but if I do this the imp function will not see any variables, and if I take the variables out of the main function it it will not work the way I want.

Any suggestions ??

    
asked by anonymous 15.08.2014 / 22:47

1 answer

0

According to this thread , the indexOf is not for searching in an array, only in a string.

var m: Array = ["CYCLE", "FREE", "EASY"];
//
var index:Number;
index= m.indexOf("FREE");
trace("INDEX "+index); // traces undefined
index= m[1].indexOf("FREE");
trace("INDEX "+index); // traces 0 

This does not explain the error you are encountering (by my understanding of your code, container should still contain valid elements despite the error - only the order is wrong), but it should be contributing to it. Even though the thread is linked, the problem may be in the version differences of ActionScript.

However, this other thread suggests using a more direct way of searching for an array by an element:

for ( i = 0 ; i < escala.length ; i++ )
    if ( escala[i] == tom )
        pesquisa = i;

Ensuring that pesquisa is correct, the problem with container must also resolve. But anyway, notice that you do not need this auxiliary array: all you're doing is moving the array escala X positions to the left, based on the tom sought. In this way, every expression of the form:

container[i]

Can be replaced by:

escala[(i + pesquisa) % 17]

Using the calculation with the module. You can also use escala.length instead of 17 to eliminate this "magic number". However, I think it's convenient for you to use an even auxiliary array - or else a function that does this calculation for you (so you do not have to keep repeating this complicated expression every time).

    
21.08.2014 / 23:19