PDA

View Full Version : delete an elemet from an array by name


Samantha Gram
08-10-2004, 02:10 AM
Hello folks.. quick question, I was just wondering if this is possible, if so how would l do it?

For instance:


myArray = new Array();
myArray.push("MONKEY");
myArray.push("DOG");
myArray.push("CAT");


now is it possible to remove an item from the array, by refering to it by name, as aposed to by it's index number?
For instance, something like:


myArray.delete("CAT");


Any help would be greatly appriciated!!

Samantha Gram

CyanBlue
08-10-2004, 02:49 AM
Uh... Here is a quick one I could think of... (Maybe there are lots of other ways of doing it... Not sure...)

myArray = new Array();
myArray.push("MONKEY");
myArray.push("DOG");
myArray.push("CAT");

trace(myArray);

deleteItem(myArray, "DOG");

trace(myArray);

function deleteItem(array, item)
{
for (var i = 0 ; i < array.length ; i++)
{
if (array[i] == item)
{
array.splice(i, 1);
}
}
}

Samantha Gram
08-10-2004, 03:24 AM
That seems to do the trick, thank you very much!
Now l've got a little bit of a better question, if you wouldn't mind helping me out.

I've got an array in a keyframe of my main timeline, for example:

myArray = new Array();
myArray.push("MONKEY");
myArray.push("DOG");
myArray.push("CAT");

Now, I've got a MC on the stage, that l am trying to get to do hit test's on the entire array, but it doesn't seem to be working at all :/

here's what l've got:

onClipEvent (enterFrame) {
num = _root.myArray.length;
for (var i = 0; i<num; i++) {
if (this.hitTest(_root[myArray[i]])) {
trace("you've hit "+i);
}
}
}

I just can't seem to figure this out...
furthermore, l can't even seem to get this to work from my movie clip:

onClipEvent(load){
//returns undefined :(
trace(_root[myArray[1]]);

//returns correctly/DOG :?
trace(_root.myArray[1]);
}

Samantha G.

Samantha Gram
08-10-2004, 03:40 AM
n/m jackpot



onClipEvent (enterFrame) {
for (var i = 0; i<_root.myArray.length; i++) {
if (this.hitTest(_root[_root.myArray[i]])) {
trace("collision detected!!");
}
}
}
Thanks for your time, in any event! =)

Samantha Gram

CyanBlue
08-10-2004, 12:45 PM
Glad you got it working and thank you for providing the answer... :)

OAN, please seperate the threads so that the subject can best describe your problem... That way, people who are looking for similar answer can get benefited by your thread... :)