【问题标题】:Back Button Support On Android for Cocos2d v3.x via ApportableCocos2d v3.x 在 Android 上通过 Apportable 支持后退按钮
【发布时间】:2014-11-02 17:44:25
【问题描述】:

Apportable Doc 中所述,我创建了一个名为AndroidButtonManagerUIResponder 子类,以在我当前的cocos2d-iPhone v3.1 项目中支持硬件按钮点击事件(返回、主页按钮点击)。下面是接口和实现文件的代码

AndroidButtonManager.h

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

@interface AndroidButtonManager :UIResponder<UIApplicationDelegate>{

}
+(instancetype) sharedManager;
@end

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

我正在 AppDelegate 的 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions by

中创建共享管理器
#ifdef ANDROID
    [AndroidButtonManager sharedManager];
#endif

我收到来自[[AlertManager sharedManager] showAlertWithHeading:@"canBecomeFirstResponder" andMessage:@"AndroidButtonManager"]; 的警报,但来自[[AlertManager sharedManager] showAlertWithHeading:@"buttonUpWithEvent" andMessage:@""]; 的警报从未显示为- (void)buttonUpWithEvent:(UIEvent *)event 从未被调用。

我已尝试按照Apportable's Google Group 中的说明在AppDelegate.m 中实现此功能。两者都不起作用。 Cocos2d-v3.x中是否有实现Android中后退按钮支持的示例项目?

提前致谢。

【问题讨论】:

    标签: ios objective-c iphone cocos2d-iphone apportable


    【解决方案1】:

    哦,终于明白了。只需要创建一个CCDirector 类别,按钮处理代码将在其中执行。这是我CCDirector的分类名称CCDirector+ButtonManager的代码。

    接口文件CCDirector+ButtonManager.h

    #import "CCDirector.h"
    
    @interface CCDirector (ButtonManager)
    - (BOOL)canBecomeFirstResponder;
    @end
    

    实现文件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
    

    并在AppDelegate-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions by

    中初始化它
    #ifdef APPORTABLE
        [[CCDirector sharedDirector] becomeFirstResponder];
    #endif
    

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多