O'Reilly logo

Cocos2d Cross-Platform Game Development Cookbook - Second Edition by Siddharth Shekar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Patrol enemy behavior

In patrol enemy behavior, the enemy AI will move between two or more points. When the player comes close to the enemy, the enemy will attack the player, and if the player moves beyond the zone of the enemy or dies, then the enemy will continue to go on with their patrol duty.

Getting ready

To create an AI, we will create a class called PatrolAI, along with a bullet class. In the PatrolAI class, we will add the movement behavior, which will check the distance between the player and enemy. If the player comes close to the enemy, the enemy is required to turn around and start shooting at the player.

In the bullet class, we will add a movement function and delete the functionality for it.

Make sure to import the enemy and hero images ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required