O'Reilly logo

Sparrow iOS Game Framework Beginner's Guide by Johannes Stein

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

Time for action – getting the enemy ship to move around

In order for the enemy ship to move around, we need to use the following steps:

  1. Open our Xcode project if it's not already open.
  2. Open the Battlefield.h file.
  3. Define all AI states as enum, as shown in the following code:
    typedef NS_ENUM(NSInteger, AIState) {
        StateWanderAround,
        StateMoveToPlayer,
        StateAttack,
        StateRecuperate
    };
  4. Inside the Battlefield scene, add a new instance variable called _aiState, which is of the AIState type.
  5. Open the Ship.h file.
  6. Add a callback block type, as shown in the following line of code:
    typedef void(^ShipCallback)(void);
  7. Declare three new methods for the Ship class, as shown in the following code:
    -(void) moveToX:(float)x andY:(float)y withBlock:(ShipCallback) block; -(float) ...

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