Skip to main content
Inspiring
October 9, 2015
Answered

Array Has -1 index

  • October 9, 2015
  • 3 replies
  • 829 views

I have 3 arrays which are created from tags in an XML file, this part works fine.

Each index corresponds to each other, i.e. [0] of each is dashBurst (the animation), 1000 (the low damage range), and 2500 (the high damage range), respectively.

But when a function chooses dashBurst, and I use a trace statement to find it's index, it puts out -1.

This means that I can't get the damage values (specialList.indexOf('dashBurst')).

What is going on?

This topic has been closed for replies.
Correct answer mcdermitt

Ok, it was that I hadn't put the item in the array as a string.

I'll remember that in the future, and thanks a lot!

3 replies

Inspiring
October 12, 2015

Convert the tags of the xml file to strings, however you're pushing the labels from the xml data, add ".toString();" after the node value.. example:

frameLabel[2] = xmlData.frame2.@label.toString();

mcdermittAuthorCorrect answer
Inspiring
October 12, 2015

Ok, it was that I hadn't put the item in the array as a string.

I'll remember that in the future, and thanks a lot!

October 10, 2015

as you say, [0] is a animation instance, but specialList.indexOf('dashBurst') means looking for a String instance, right?

so you won't find 'dashBurst' in the array, but specialList[0].name is 'dashBurst'.

Inspiring
October 10, 2015

trace(specialList[0]);

then use the result here specialList.indexOf('result');

Ned Murphy
Legend
October 9, 2015

If you get a -1 as a result of using the indexOf method it means the item sought does not exist in the array.

mcdermittAuthor
Inspiring
October 10, 2015

post.user_wrote.label:

If you get a -1 as a result of using the indexOf method it means the item sought does not exist in the array.

I found a workaround where the damage index is +1 of the frame label index and it works fine.

I traced all the array's objects and it has everything in it's proper place.

So it exists, but it doesn't see it in the array at the time of this code running.

Ned Murphy
Legend
October 10, 2015

You might have traced the array's objects but whatever you are using quotes for in that code line is not (cannot be) an object.  Try removing the quotes for a start if that is supposed to be an instance name.