Game Development Reference
In-Depth Information
@implementation StandardMoveComponent
-(id) init
{
if ((self = [super init]))
{
velocity = CGPointMake(−100, 0);
[self scheduleUpdate];
}
return self;
}
-(void) update:(ccTime)delta
{
if (self.parent.visible)
{
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (self.parent.position.x > screenSize.width * 0.5f)
{
self.parent.position = ccpAdd(self.parent.position, ←
ccpMult(velocity, delta));
}
}
}
@end
The StandardMoveComponent initializes its velocity so that whatever node is
using it, it will move at a fixed speed to the left. The update method first checks
whether the parent is visible, because if not, it's implied that the parent is currently in-
active. The parent is moved until it crosses the center position of the screen with the
given velocity multiplied by the delta time, to move the same distance regardless
of framerate.
As you can see, component code is usually very simple. That makes it easy to find and
fix flaws in components as well. Over time, you can accumulate a number of such
components for re-use in other apps, and you'll need to write fewer of the commonly
used code and more of the application-specific code—code that's new and challenging
and fun to write.
Search Nedrilad ::




Custom Search