"Quadrant", analyze area around the square

Asked

Viewed 148 times

0

I am with a project and I came up with a problem. The project would be a simple simcity 1 made in pure javascript. The problem is that when it comes to making intersections and curves I can’t detect the square next to it on my grid. To learn how to solve the problem wanted you to do the following: If I put a square the square on the left turns green, the right turns yellow, the above turns blue, the below brown and explain how you did if possible. My idea was to do when clicking to draw my square I draw another 8 around it to check if there is square next. Exemplo:

Code link: http://jsfiddle.net/4Me57/15/

1 answer

1


If you already know the position of the row and column, it is basic to think that: (row - 1) is in the top row and (column + 1) is in the back column, and so on...

    dR(grid_x[i],grid_y[n-1],grid_w,grid_h,'#0018FF');
    dR(grid_x[i-1],grid_y[n],grid_w,grid_h,'#218126');
    dR(grid_x[i],grid_y[n+1],grid_w,grid_h,'#815E21');
    dR(grid_x[i+1],grid_y[n],grid_w,grid_h,'#FFC600');
  • However I do not have a square or position set I create it randomly anywhere I click on my grid. That way I’d have to have a specific position.

  • Guy puts these lines of code along with the line that makes the black square... Sorry but you don’t understand your own code...

  • No friend I don’t think I formulated the right doubt. I want me to draw the black square, when I click on the black square to draw another square he will draw a blue square but in case I click on a place away from those squares he will draw the black square.

  • And I’m sorry if I’m annoying is that I’m new to the area.

  • Use the variable grid_type to see if the neighbors of the new click have already been clicked using ifs... (obviously in the future you will have to register in this variable the type of object that the position has and not just true / false)

  • I thought about it too but when I went to try I did not get a good logic would have how to explain a little better to me to understand?

  • I’m on the phone now, but check there in the code n+1, i-1... Use this in gridtype to see the neighbors

  • I understood why it didn’t work out before now! I instead of grid_type[i][n + 1] for example, I just placed grid_type[n + 1] to make it go wrong!

  • @user3792106 because you do not part to pure html, with the use of classes will greatly facilitate the implementation of sprites with the drawings and even animations, see the model I made in jquery of your script and compare the simplicity that was left: http://jsfiddle.net/jaderw/6q6gq760/

  • really turned out pretty good. The reason I’m doing it in Javascript is that as I’m learning in school I prefer to do it for the subject they’re giving me despite being more advanced. I’m about to learn object orientation and I’ve seen some sites to learn a little Jquery now just finish this way and go to the next.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.