Game Development Reference
In-Depth Information
turns the absolute position in points, and vice versa during assignment. The
contentS-
ize
and
scale
properties both have equivalent
contentSizeInPoints
and
scaleInPoints
methods that you should prefer whenever you calculate in points.
The
levelSize
variable is assigned from
_levelNode.contentSizeInPoints
.
It is used in the two following lines, which clamp the
viewPos
to the level's size using
the
MIN
and
MAX
macros.
The
viewPos
coordinates are first clamped to the level extents using the inner MIN
macro. Here
viewSize
is subtracted from the
levelSize
. This is an inset calculation
because the screen should never scroll closer than
viewCenter
to the level's borders,
on both sides for both axes. The insets on all four borders of the level are visualized as
two-faced arrows, which are shown in
Figure 3-3
. Both border distances added together
are conveniently equal to
viewSize
. Or, in other words, the scrollable area is two times
viewCenter
or one
viewSize
smaller than the level area.
Figure 3-3
.
Relation of Level Area to Scrollable Area. Arrows depict the Scrollable Area inset. Note the play-
er is not tied to its center position when near level borders. The diagram is not drawn to scale
The smaller of the two candidates,
viewPos
or
levelSize
minus
viewSize
, is re-
turned by the
MIN
macro to the enclosing
MAX
macro, which in turn ensures that the
viewPos
is always equal to or greater than 0.0. Together, this prevents the
_levelNode
from scrolling past its borders.
Finally, the
viewPos
is negated (multiplied by -1.0) and then assigned to the
_levelNode.positionInPoints
. Remember what I said earlier about moving the
level node in the opposite direction? That's what this line does. For instance, if the calcu-
lated
viewPos
is 850,340, the
ccpNeg
method would change the coordinates to -850,-
-340 so that movement of the
_levelNode
goes in the opposite direction of where the
player is going.
Publish the SpriteBuilder project. Then build and run the Xcode project. You are now able
to scroll over the level's area, but no further.
Search WWH ::
Custom Search