Back Button Support On Android for Cocos2d v3.x via Apportable

237 Views Asked by At

As described in Apportable Doc, I have created a subclass of UIResponder named AndroidButtonManager to have support for hardware button tap events (Back, Home button tap) in my current cocos2d-iPhone v3.1 project. Here are the codes of interface and implementation files

AndroidButtonManager.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface AndroidButtonManager :UIResponder<UIApplicationDelegate>{

}
+(instancetype) sharedManager;
@end

and AndroidButtonManager.m

#import "AndroidButtonManager.h"


@implementation AndroidButtonManager
+(instancetype)sharedManager
{
    static AndroidButtonManager* sharedManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[self alloc]init];
    });
    return sharedManager;
}
-(instancetype)init{
    if (self = [super init]) {
        [self canBecomeFirstResponder];
    }

    return self;
}

#ifdef ANDROID
// ----------------------------------------------------------------
//          OVER RIDING PARENT CLASS METHOD
// ----------------------------------------------------------------
-(BOOL)canBecomeFirstResponder
{
    [[AlertManager sharedManager] showAlertWithHeading:@"canBecomeFirstResponder" andMessage:@"AndroidButtonManager"];
    return YES;
}

- (void)buttonUpWithEvent:(UIEvent *)event
{
    [[AlertManager sharedManager] showAlertWithHeading:@"buttonUpWithEvent" andMessage:@""];
    switch (event.buttonCode)
    {
        case UIEventButtonCodeBack:
            [[AlertManager sharedManager] showAlertWithHeading:@"Back Button" andMessage:@"Home Button touched , going to transit to Menu Layer"];
            break;
        case UIEventButtonCodeMenu:{
            [[AlertManager sharedManager] showAlertWithHeading:@"Home Button" andMessage:@"Home Button touched , going to transit to Menu Layer"];
            break;
        }
        default:
            break;
    }
}

#endif
@end

And I am creating the shared manager in AppDelegate's -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions by

#ifdef ANDROID
    [AndroidButtonManager sharedManager];
#endif

I am getting alert from [[AlertManager sharedManager] showAlertWithHeading:@"canBecomeFirstResponder" andMessage:@"AndroidButtonManager"];, but the alert from [[AlertManager sharedManager] showAlertWithHeading:@"buttonUpWithEvent" andMessage:@""]; never appears as - (void)buttonUpWithEvent:(UIEvent *)event is never called.

I have tried implementing this in AppDelegate.m as specified in Apportable's Google Group.Both are not working. Is there any example project implementing back button support in Android in Cocos2d-v3.x?

Thanks in advance.

1

There are 1 best solutions below

0
Sauvik Dolui On

Oh finally got it. Just need to make a category of CCDirector in which the button handling code will be executed. Here is the code of my CCDirector's category name CCDirector+ButtonManager.

Interface File CCDirector+ButtonManager.h

#import "CCDirector.h"

@interface CCDirector (ButtonManager)
- (BOOL)canBecomeFirstResponder;
@end

Implementation File CCDirector+ButtonManager.m

#import "CCDirector+ButtonManager.h"

@implementation CCDirector (ButtonManager)
#ifdef APPORTABLE
- (void)buttonUpWithEvent:(UIEvent *)event {
    switch (event.buttonCode)
    {
        case UIEventButtonCodeBack:
            // handle back button here
            [self showAlertWithHeading:@"buttonUpWithEvent" andMessage:@"BACK BUTTON"];
            break;
        case UIEventButtonCodeMenu:
            // show menu if possible.
            [self showAlertWithHeading:@"buttonUpWithEvent" andMessage:@"MENU BUTTON"];
            break;
        default:
            break;
    }
}

- (BOOL)canBecomeFirstResponder {
    [[AlertManager sharedManager] showAlertWithHeading:@"canBecomeFirstResponder" andMessage:@"CCDirector"];
    return YES;
}
#endif
-(void)showAlertWithHeading:(NSString*)alertHeading andMessage:(NSString*)alertMessage
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:alertHeading
                                                       message:alertMessage
                                                      delegate:self
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles: nil];
    [alertView show];
}
@end

And initialize it in AppDelegate's -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions by

#ifdef APPORTABLE
    [[CCDirector sharedDirector] becomeFirstResponder];
#endif