Game Development Reference
In-Depth Information
After the layerPos has been calculated, it is negated just like viewPos and assigned
to the layer:
layer.positionInPoints = ccpNeg(layerPos);
Be sure to use positionInPoints so that the position is properly converted from
points to other position types, if needed. To put everything in context, Listing 3-14 shows
the updated scrollToTarget: method again in full.
Listing 3-14 . Scrolling with parallaxing layers
-(void) scrollToTarget:(CCNode*)target
{
CGSize viewSize = [CCDirector sharedDirector].viewSize;
CGPoint viewCenter = CGPointMake(viewSize.width / 2.0,
viewSize.height / 2.0);
CGPoint viewPos = ccpSub(target.positionInPoints,
viewCenter);
CGSize levelSize = _levelNode.contentSizeInPoints;
viewPos.x = MAX(0.0, MIN(viewPos.x, levelSize.width
- viewSize.width));
viewPos.y = MAX(0.0, MIN(viewPos.y, levelSize.height
- viewSize.height));
_physicsNode.positionInPoints = ccpNeg(viewPos);
CGPoint viewPosPercent = CGPointMake(viewPos.x
/ (levelSize.width - viewSize.width),
viewPos.y
/ (levelSize.height - viewSize.height));
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));
layer.positionInPoints = ccpNeg(layerPos);
}
}
Search WWH ::




Custom Search