I just posted a code to explain what can do a "latch key" . I understood the initial walk. ;)
But, i really think, there is missing something in an other part of your code. Here, noone of the two codes you posted could not work. So it comes from an other part.
Maybe you try using this in a function, and you forget a global var ...
an other way is : maybe you have a "animate" in other part of your code ... don't know, but it's sure => the code should work. It's real as 1+1=2 :)
But, i really think, there is missing something in an other part of your code. Here, noone of the two codes you posted could not work. So it comes from an other part.
Maybe you try using this in a function, and you forget a global var ...
an other way is : maybe you have a "animate" in other part of your code ... don't know, but it's sure => the code should work. It's real as 1+1=2 :)