Game Development Reference
In-Depth Information
}
[physicsBody
applyImpulse:CGPointMake(_playerNudgeRightVelocity,
_playerNudgeUpVelocity)];
if (ccpLength(physicsBody.velocity) >
_playerMaxVelocity)
{
CGPoint direction
= ccpNormalize(physicsBody.velocity);
physicsBody.velocity = ccpMult(direction,
_playerMaxVelocity);
}
}
The variables prefixed with _player are assigned some values at the top. You will soon
replace them with custom properties to make them editable right within SpriteBuilder. For
now, these assignments just exist to make the code functional.
Let's dissect the core of the method:
CCPhysicsBody* physicsBody = target.physicsBody;
The target's CCPhysicsBody instance is stored in a local variable. This makes the code
easier to read, shorter, and minimally faster compared to using tar-
get.physicsBody.velocity every time you need to read from or assign to velo-
city, for instance. The main benefit here is clearly readability; improved performance is
only a very, very minor but nevertheless positive side-effect.
Since the game is all about moving from left to right, any already inherent “leftward”
movement in a negative x axis direction is canceled when in movement mode by setting
the x component of the physicsBody.velocity to 0:
if (physicsBody.velocity.x < 0.0)
{
physicsBody.velocity = CGPointMake(0.0,
physicsBody.velocity.y);
}
The node may very well be moving to the left—for instance, because external forces
pushed it to the left or simply because it is rolling down a slope that's slanted toward the
Search WWH ::




Custom Search