HTML and CSS Reference
In-Depth Information
world.map
array. In essence, we are subtracting the
dy
or
scrollRate
value from the current
camera position.
Wehaveseenthiscodepreviously,buthereitisonemoretime.Thisedgecaseisthefirstelse:
(bold and larger type):
iif
(
camera
.
y
<=
0
) {
camera
.
y
=
0
;
rowBuffer
=
0
;
}
else if
(
camera
.
y
>
(
world
.
height
-
camera
.
height
)
-
scrollRate
) {
camera
.
y
=
world
.
height
-
camera
.
height
;
rowBuffer
=
0
;
}
else
else
{
rowBuffer
=
1
;
}
Figure 9-18
shows an example of the bottom edge case.
Figure 9-18. The fine scrolling camera at position bottom edge
The data for the algorithm would look like this:
scrollRate
=
4
camera
.
x
=
40
camera
.
y
=
320
This will return buffer values as follows data:
colBuffer
=
1
rowBuffer
=
0
Because we are scrolled all the way down, the
camera.y
value is
320
. When we add
cam-
era.height
of
160
to this value
320
, we get
480
. This just so happens to be the height of
the world (
world.height
). Notice that we have to subtract
scrollRate
to ensure that we are
always comparing a value that is less than where the player wants to go; if we didn't do this,
we could actually throw a null pointer error in the row array lookup.