Below is my code:
This code is used to find the surrounding nodes when supplied with a given node.
i.e

for(var i:uint = 0;i < nodes.length; i++){
test_node = this.nodes[i];
if (test_node.row < node.row - 1 || test_node.row > node.row + 1) continue;
if (test_node.column < node.column - 1 || test_node.column > node.column + 1) continue;
surrounding_nodes.push(test_node)
}
/*
nodes contains an array of objects.
node is an object I want to use as my test variable.
property row contains the row in which the object is located
proerty column contains the cloumn in which the object is located
*/
I’m getting correct results (as I am following a tutorial), although I am not sure why?
this is what I’m thinking.
-
if the test_node row is below node row location, or if the test_node
row is above the node row – continue -
if the test_node column is to the left of the node column location continue or if the test_node column is to the right of the
node column – continue.
Assume the above image case.
Therefore, shouldn’t surrounding_nodes contain all the objects in nodes (bar the actual node), as every object is going to satisfy the above statement, because and object is either going to be above or below node or to the left or right of it.
what this code actually does is only find the nodes surrounding the node (red square).
could someone actually help me understand these if statement.
thanks
Just substitute the conditionals with numbers and you see how it works.
In the image above node is on row 4 and column 4 (0-based) and let’s take a test_node on row 2
then
translates to
continue means “skip the rest of this iteration and start the next one”
so now take test_node on row 3 and column 3
all 4 conditions are false so it adds it to surrounding_nodes
— EDIT —
BTW if you label your loop it’s more legible what happens