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.
Oh finally got it. Just need to make a category of
CCDirectorin which the button handling code will be executed. Here is the code of myCCDirector's category nameCCDirector+ButtonManager.Interface File
CCDirector+ButtonManager.hImplementation File
CCDirector+ButtonManager.mAnd initialize it in
AppDelegate's-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsby