Action Script 3.0 Function does not receive parameter

Asked

Viewed 227 times

2

I’m having a problem with parameters for my function. It’s not getting the parameter I pass to it. Follow 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 will call the function as follows: imp(container[3]); passing an element of the container array to my function, but I tested it and when I run it I see that it turns me "null". I have already checked with direct "string" entries and it worked, it just doesn’t work with this 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 container array, and I’ve put the imp function outside the main function, but if I do this the imp function will not see any variables, and if I take the main function variables it will not work the way I want it to.

Any suggestions ??

  • Is there a way to put the complete code somewhere? (e.g., Pastebin) Just looking at the piece you posted can’t identify where the problem might be...

  • @mgibsonbr As I said the function "imp()" is very extensive. The following code would be:

  • just calling the function seven times, printing on the screen. imp(container[3] ... imp(container[6] ... imp(container[7] ... container[10] on. As I said the real problem is the parameter I pass to function, and it happens with the array element parameter[];

  • I’m not talking about putting the full code here, but to post it somewhere (Pastebin, ideone, etc) and put the link here. You states that the problem is in passing parameter, but how can we be sure (and help you) if we do not know your code?

  • I’d like to help you @Fenixdk, but I don’t understand any of your code, post the code somewhere or edit your question to make it more understandable.

  • @mgibsonbr, Good sorry for the confusion, the entire code is here: http://pastebin.com/R4Uj1Ra8

  • Blz, now it’s clearer, +1. As for the call from imp, Do you have an example of what works and what doesn’t? For example, if tom (texto.text) for "C#", imp("D#") returns "D#" and imp(container[3]) returns null. That’s it?

  • What happens in this code is: tone is a user input, and from there the container array will be filled according to this entry. the check pattern, which will go through the function and be analyzed is some elements of the container, eg: imp(container[3]); according to what is in the container[3] the imp() function will return me according to what I want. Only that the function imp() works according to the variable "search" but it is not accessing this variable as well as the container array.

  • @mgibsonbr while your question of what works, the only thing that is not working is that part of the parameter as array[]. How direct string works ex: imp("D"). I have tested with some "trace". but imp() returns me null when I pass it as array[].

  • I find it unlikely, because as far as I know Actionscript deals correctly with closures. Anyway, a suggestion: how about defining imp so that she receives pesquisa and container as additional parameters, instead of accessing these variables from the external scope? See if this helps at all. Personally, I’m "betting" on a logic error instead, but I’m not sure. Note for example that not every input produces a non-zero answer, and that there are certain repeated indices - such as C and C#, both 1 - and others that seem incorrect, like D#, 5.

  • In this part, I think there are no errors because this code is the same as PHP. As for the indexes I will check an optimization.

  • I was testing something here in the code, and I found out that the container has a problem even though it’s filled in. var var1:String = container[3]; I declared this variable to test and when I print it, it returns "null". can tell me if the error can be array manipulation ?

  • @Fenixdk From what I saw in your code, the creation of container seems to be correct. escala is working? I noticed the array container is unnecessary (although useful) - you can replace container[i] for escala[(i + pesquisa) % 17] for any i. Since of course pesquisa has a valid value. (By the way, have you checked this? I don’t know Actionscript very well, the only indexOf I know is to look for a substring inside a string. It works with arrays too?)

  • Yes, "indexof" works like a PHP "array_search"

  • It is not necessary to take into account the checking of entries now. I will have no problem with that.

Show 10 more comments

1 answer

0


According to that thread, the method indexOf does not serve to search 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 found (by my understanding of your code, container should still contain valid elements despite the error - only the order would be wrong), but must be contributing to it. Still according to the thread linked, the problem may be in the version differences of Actionscript.

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

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

Ensuring that pesquisa is correct, the problem with container should resolve itself as well. But anyway, note that you don’t need of this auxiliary array: all you’re doing is moving the array escala X left positions, based on tom sought. Thus, every expression of the form:

container[i]

May 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 believe it is more convenient for you to use an auxiliary array even - or a function that does this calculation for you (so you don’t have to keep repeating this complicated expression every time).

  • I will do the tests here thank you very much for the answers!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.