Game Development Reference
In-Depth Information
How to do it
Perform the following steps to implement the evade behavior:
1. Open the Player.m file and add the following line of code in the end of the file:
- (void) evade:(CGPoint )target
deltaTime:(float)deltaTime {
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);
// Note the minus sign - we're multiplying by the
inverse of our movement speed,
// which means we're moving away from it
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, add the following code in the update method:
if (self.behaviourType == Evade) {
int boxWidth = 100;
Search WWH ::




Custom Search