Game Development Reference
In-Depth Information
In our case, if the player moves to the right and up, for example, what we're really going
to do is to move the _levelNode toward the left and down to keep the player centered
on the screen. That gives the impression of the player moving toward the right and up, but
really the player stays centered on the screen while the entire level's content moves to the
left and down.
Still, the player's position is going to be what you would expect: limited to the confines of
the level node and in an absolute position. The player's position will range from the level
node's origin (lower-left corner at 0,0) to the full extents of the level node, here:
4000x500 points.
To set this up, add the update: method from Listing 3-7 just above the @end line at the
bottom of the GameScene.m file.
Listing 3-7 . The update: method is automatically called once per frame
-(void) update:(CCTime)delta
{
// update scroll node position to player node, with
offset to center player in the view
[self scrollToTarget:_playerNode];
}
The update: method is called automatically by Cocos2D when implemented in a node
class. It runs every frame before rendering the nodes to the screen. We need to use that
method for scrolling because our player's position is currently updated over time by the
CCActionMoveTo . Later, the player's position will be modified by the physics engine.
In both cases, we don't know exactly when or where the player position update occurs;
therefore, the scroll position is simply recalculated every frame.
Unlike you did with previous Cocos2D versions, you no longer have to explicitly sched-
ule the update: method. You can still schedule other methods or blocks using the node
schedule and unschedule methods as documented in the CCNode class reference:
http://www.cocos2d-swift.org/docs/api/Classes/CCNode.html . For
example, to run a selector once after a delay, you would write
[self scheduleOnce:@selector(theDelayedMethod:) delay:2.5];
Then implement the corresponding selector in the same class. The selector must take one
parameter of type CCTime :
Search WWH ::




Custom Search