Help! My tile system is buggy!

Hey all, I’m trying to make my own code to handle one square moving around a 500x300 stage at 10px and be able to stop when it hits another square, but it’s sort of buggy. Both the character and the block that I want him to stop at are 10x10 squares.

Code on the frame:


/*Plot is a function I use so that instead of using the actual 500x300 to
plug in x and y values I can use a 50x30 based on where the square    
can move */
function PLOT(xory) {
 newxory = xory/10;
 return (newxory);
}
/* Here I declare two arrays that hold whatever spots are occupied by non-passible objects (ie. my wall) */
takenXslots = new Array();
takenYslots = new Array();
/* This is Kirupa's wonderfully useful Array searching function */
arraySearch = function (input, array) {
 for (i=0; i<array.length; i++) {
  if (array* == input) {
   return 1;
  }
 }
 return -1;
};
 

The code on my wall :


onClipEvent (load) {
 with (this) {
  _x = 40;
  _y = 70;
  xslot = _root.PLOT(this._x);
  yslot = _root.PLOT(this._y);
 }
 _root.takenXslots.push(this.xslot);
 _root.takenYslots.push(this.yslot);
}

And the code on my character:


onClipEvent (load) {
 with (this) {
  _x = 100;
  _y = 200;
 }
}
onClipEvent (enterFrame) {
/* These determine the x and y values of the next move using the plot function */
 this.nextLEFT = new Array(_root.PLOT(this._x -= 10), _root.PLOT(this._y));
 this.nextRIGHT = new Array(_root.PLOT(this._x += 10), _root.PLOT(this._y));
 this.nextUP = new Array(_root.PLOT(this._x), _root.PLOT(this._y += 10));
 this.nextDOWN = new Array(_root.PLOT(this._x), _root.PLOT(this._y -= 10));
 //MOVEMENT LEFT
 if (Key.isDown(Key.LEFT)) {
  if (_root.arraySearch(this.nextLEFT[1], _root.takenYslots) == -1) {
   if (_root.arraySearch(this.nextLEFT[0], _root.takenXslots) == -1) {
    this._x -= 10;
   }
  }
  if (_root.arraySearch(this.nextLEFT[1], _root.takenYslots) == 1) {
   if (_root.arraySearch(this.nextLEFT[0], _root.takenXslots) == -1) {
    this._x -= 10;
   }
  }
  if (_root.arraySearch(this.nextLEFT[1], _root.takenYslots) == -1) {
   if (_root.arraySearch(this.nextLEFT[0], _root.takenXslots) == 1) {
    this._x -= 10;
   }
  }
 }
 //MOVEMENT RIGHT                                        
 if (Key.isDown(Key.RIGHT)) {
  if (_root.arraySearch(this.nextRIGHT[1], _root.takenYslots) == -1) {
   if (_root.arraySearch(this.nextRIGHT[0], _root.takenXslots) == -1) {
    this._x += 10;
   }
  }
  if (_root.arraySearch(this.nextRIGHT[1], _root.takenYslots) == 1) {
   if (_root.arraySearch(this.nextRIGHT[0], _root.takenXslots) == -1) {
    this._x += 10;
   }
  }
  if (_root.arraySearch(this.nextRIGHT[1], _root.takenYslots) == -1) {
   if (_root.arraySearch(this.nextRIGHT[0], _root.takenXslots) == 1) {
    this._x += 10;
   }
  }
 }
 //MOVEMENT UP                                        
 if (Key.isDown(Key.UP)) {
  if (_root.arraySearch(this.nextUP[1], _root.takenYslots) == -1) {
   if (_root.arraySearch(this.nextUP[0], _root.takenXslots) == -1) {
    this._y -= 10;
   }
  }
  if (_root.arraySearch(this.nextUP[1], _root.takenYslots) == 1) {
   if (_root.arraySearch(this.nextUP[0], _root.takenXslots) == -1) {
    this._y -= 10;
   }
  }
  if (_root.arraySearch(this.nextUP[1], _root.takenYslots) == -1) {
   if (_root.arraySearch(this.nextUP[0], _root.takenXslots) == 1) {
    this._y -= 10;
   }
  }
 }
 //MOVEMENT DOWN                                        
 if (Key.isDown(Key.DOWN)) {
  if (_root.arraySearch(this.nextDOWN[1], _root.takenYslots) == -1) {
   if (_root.arraySearch(this.nextDOWN[0], _root.takenXslots) == -1) {
    this._y += 10;
   }
  }
  if (_root.arraySearch(this.nextDOWN[1], _root.takenYslots) == 1) {
   if (_root.arraySearch(this.nextDOWN[0], _root.takenXslots) == -1) {
    this._y += 10;
   }
  }
  if (_root.arraySearch(this.nextDOWN[1], _root.takenYslots) == -1) {
   if (_root.arraySearch(this.nextDOWN[0], _root.takenXslots) == 1) {
    this._y += 10;
   }
  }
 }
}
/* Originally I was messing around with the Left movement and it seemed to work if i presented all three possible cases as shown (X and Y clear / X clear and Y is not, Y clear and X is not )*/

When I test the movie, the following happens :

Left movement works perfectly

Right movement stops when INSIDE the wall rather than one space to the left of it

When moving down it has a similar effect to the right (stopping inside instead of 1 space up)

When moving up it stops a space AHEAD of the square instead of two below it.

Whats wrong with my code?!?! :puzzle:

Any help would be great, thanks in advance.