Using the MBProgressHUD on Cocos2d-x (Undetermined duration progress icon) - Plunge Interactive

Using the MBProgressHUD on Cocos2d-x (Undetermined duration progress icon)

We all need to show some kind of progress bar at some point, while downloading a picture, executing a heavy task, or whatever that takes time. A great way to do this on iOS is to use the MBProgressHUD class. It can be downloaded and used completely for free, and will allow us to implement this feature in less than 5 minutes.


As you can see in the picture below, it is also very customizable:

The first step is to drop the MBProgressHud.h and .m files into our “iOS” folder in XCODE. Then I’ll use this class from the AppController class. Why? because MBProgressHud needs information about the current window, and that information is contained in the controller. More sophisticated implementations are up to you :-)

You just need to do the following in AppController.h:

// Define this before the @interface
#import "MBProgressHUD.h"

// Put this inside the @interface section
MBProgressHUD *_hud;

// Put this after the @interface section
@property (retain) MBProgressHUD *hud;

// "Loading" UI element public methods
- (void)showHud;
- (void)hideHud;

+(AppController*)getSharedInstance;

Then, again, the implementation of the AppController.mm is very easy:

// Static accessor to the classs
static AppController *sharedInstance;

// This method will allow us to get access to the app controller from anywhere
+(AppController*)getSharedInstance {
    return sharedInstance;
}

// Make sure to add this line at the didFinishLaunchingWithOptions method:
sharedInstance = self;

- (void)showHud {
    self.hud = [MBProgressHUD showHUDAddedTo:viewController.view animated:YES];
    _hud.labelText = @"Loading...";
};

- (void)hideHud {
    [MBProgressHUD hideHUDForView:viewController.view animated:YES];
    self.hud = nil;
};

That was so easy right? Ok, now we need to call this code from C++ in order to be able to show / hide the HUD from our game.

We could do this creating an Objective C++ class that makes use of these hideHud and showHud methods. An small example:

[[AppController getSharedInstance] showHud];

Please feel free to follow me on twitter at @jboschaiguade and @plungeint

This blog is being maintained by Plunge Interactive professionals, a game development company.

This site uses cookies to store information on your computer. Some cookies on this site are essential, and the site won't work as expected without them. Read more about them

ACCEPT