Fairly Simple - Not Sure If It Would Work In Your Situation...
I've Only Read The First Page - Too Lazy
Of Course Add Timeout Etc Blah Blah:
PHP Code:
this.width = (this.x - player.x);
this.height = 2;
if (onwall2(this.x,this.y,this,width,this.height)){
}else{
this.mode = "idle"; //Whatever Your Go Into Idle Is
}