HTML and CSS Reference
In-Depth Information
iif ( ! tankStarted ) {
currentNode = startNode ;
tankStarted = true
true ;
nextNode = result [ 0 ];
tankX = currentNode . x * 32 ;
tankY = currentNode . y * 32
If the tank is in the first startNode , tankStarted will be false . In this case, we must set
currentNode tothe startNode value. nextNode willbethefirstnodeintheresultarraynode
path. The tankX and tankY coordinates are the path x and y values (respectively) multiplied
by the tile size ( 32 ).
Now that the tank is actually moving, we check to determine when it has made it to the center
of the next node. We did this the simplest manner possible because each step that the tank
takes in any direction will be the value 1 .
If the tank were to move at a speed other than 1, it would be better to calculate the vector between
the center of each of the nodes and count the steps in between the nodes. We would then move the
tank that number of steps before it stopped to change nodes. Examples of this type of movement are
provided in Chapter 5 .
iif ( tankX == nextNode . x * 32 && tankY == nextNode . y * 32 ) {
//node change
currentNodeIndex ++ ;
iif ( currentNodeIndex == result . length ) {
finishedPath = true
true ;
currentNode = nextNode ;
nextNode = result [ currentNodeIndex ]
tankMoving = false
false ;
Next we check to see whether the tank has moved to the center of the next node. We do this
by comparing the tank x and y values with the node x and y values. First we add 1 to cur-
rentNodeIndex . Using that new value, we either stop the tank from moving by setting fin-
ishedPath to true ,orweset currentNode tobe nextNode ,set nextNode tothenewnodein
the result path using currentNodeIndex , and we set tankMoving to false .
Search WWH ::

Custom Search