Game Development Reference
In-Depth Information
In other words, the scrollable area is the size of the level minus the size of the view.
Hence, dividing the viewPos by the scrollable area ( levelSize minus viewSize )
gives you the percentage of where the _physicsNode position within the scrollable
area currently is:
CGPoint viewPosPercent = CGPointMake(viewPos.x
/ (levelSize.width - viewSize.width),
viewPos.y
/ (levelSize.height - viewSize.height));
You now have the _physicsNode 's position in a range of 0.0 to 1.0, where 0,0 refers to
the lower-left scrollable area position of 284x160 and 1,1 refers to the top-right position
3716x340. Next you have to apply this percentage to every layer, taking into account the
layer's own size.
Try to do the calculation in Listing 3-13 in your head or on paper once, using 568x384 as
the layerSize width and height and 568x320 as the viewSize width and height and
tell me what values you get for layerPos when viewPosPercent is 0.5, 0.5.
Listing 3-13 . Calculating the layer's position relative to viewPosPercent
for (CCNode* layer in _backgroundNode.children)
{
CGSize layerSize = layer.contentSizeInPoints;
CGPoint layerPos = CGPointMake(viewPosPercent.x
* (layerSize.width - viewSize.width),
viewPosPercent.y
* (layerSize.height - viewSize.height));
}
If neither you nor I made a mistake, you should have noticed that where layerSize and
viewSize are the same, the result is 0. And 0 multiplied by whatever remains 0. Thus,
on the x axis you get 0 as layerPos.x , while layerPos.y is calculated to be the fol-
lowing:
0.5 * (384 - 320) = 32 points
In English: this layer can move vertically within a range of 64 points and its Y position is
currently offset by 32 points.
Search WWH ::




Custom Search