for gravity you should just do...

if(this._y>400){
this.__y = 400
}

then for walls, you should do something like...

if (this.hittest(_root.wall)){
this._x -= 2
}