walking through walls !?!

BlitzPlus Forums/BlitzPlus Beginners Area/walking through walls !?!

Chrisbris(Posted 2016) [#1]
I have a brick wall in my platform game
how do you stop your sprite walking through it

map is an array, walls depicted by the number 3... not that that matters....
I can make him stop but when he gets to the edge of the screen, the code that checks for the obstruction in one way or the other my code errors as it try's to look past the defined array configuration
I can counter this by saying something like if his x cord is less than the max amount allowed before the screen runs out then don't let him move any more in that direction,,, but that makes him stop 16 pixels from the edge of the play area and looks stupid ....

Any thoughts would be appreciated :)

peace


Midimaster(Posted 2016) [#2]
this is a well known problem... This happens, because your player can stand on GRID(0/0) and when ask for new directions he will check GRID(-1/0) which is outside the array

The solution is to create a complete closed wall around your playground
f.e. for a grid of 20x 10 you now need a bigger grid of 22x12 fields:
Global Grid%[22,12]
Const WALL=3

For local i%=0 to 21
     Grid[i,0]=WALL
    Grid[i,11]=WALL
Next 

For local i%=0 to 11
     Grid0,i]=WALL
    Grid[21,i]=WALL
Next 

This creates a closed wall around the whole grid. Now the player can only use the grids 1/1 to 21/11.

If the new walls should stay out of screen you need to adjust the drawing:
Const GRID_SIZE=32
For local y%=1 to 11
     For local x%=1 to 21
          DrawImage Elements, (x-1)*GRID_SIZE, (y-1)*GRID_SIZE, Grid[x,y]
     Next
Next


if you need an "EXIT" in the wall and want to prevent from array errors here, create a double wall and allow one exit in the inner wall: