Get the index in an array of a clicked element in actionscript
I'm trying to create a virtual keyboard.
I have a class VirtualKeyboard
which contains an array called keyboard of 26 movieclips开发者_如何学编程, each one representing a letter.
In the main class, I create an object of the class VirtualKeyboard
and added an eventlistener
on it when a letter is clicked.
I tried this in the event handler:
var objectClicked:Object = event.currentTarget;
var index:uint = virtualKeyboard.keyboard.indexOf(objectClicked);
But the index
always returns the value 4294967295.
How can I know which letter was clicked, I mean the index in the keyboard array corresponding to that letter?
The same code is working correctly if I put it in the VirtualKeyboard
class, but not from the main class.
4294967295
is 2^32 - 1. That is what happens when you assign -1 to a uint
. It means the value was not found.
Recommendations:
- As indexOf returns an int, use int. It's faster anyway.
- Use Vectors if you can, that way you can be sure that everything is of the same class.
- Is
virtualKeyboard.keyboard
an array of DisplayObjects, or is it an array of strings or numbers? You have to make sure you are comparing apples to apples, not apples to goats (Also why I recommend Vector). - Don't cast to Object. AS3 has a solid type system -- use it.
精彩评论