Game Development Reference
In-Depth Information
How to do it
Perform the following steps to implement the arrive behavior:
1. Open the Player.m file, and add the following line of code in the end of the file:
- (void) arrive:(CGPoint )target
deltaTime:(float)deltaTime {
// Work out the direction to this position
GLKVector2 myPosition =
GLKVector2Make(self.position.x, self.position.y);
GLKVector2 targetPosition =
GLKVector2Make(target.x, target.y);
GLKVector2 offset =
GLKVector2Subtract(targetPosition, myPosition);
// Reduce this vector to be the same length as our
movement speed
offset = GLKVector2Normalize(offset);
offset = GLKVector2MultiplyScalar(offset, 5);
// Add this to our current position
CGPoint newPosition = self.position;
newPosition.x += offset.x;
newPosition.y += offset.y;
self.position = newPosition;
}
2. Now, we have to call this function till our player is not in the stopping radius. So,
we will create a box around our target point, and as soon as the player is inside this
box, we will stop calling the arrive function. To implement this, add the following
line of code in the update method:
if (self.behaviourType == Arrive) {
int boxWidth = 20;
Search WWH ::




Custom Search