0

在我使用 cocos2d-iphone 2.0.0 的 iOS 游戏中,我有一个图层会弹出一个精灵,要求用户购买应用内购买,并带有一个供用户单击购买的按钮 ( menuItemBuyButton)。当用户点击这个购买按钮时,会完成三件事:

  • 活动指示器已启动
  • 图层上的所有菜单项都被禁用 - 特别是主菜单(此代码在主菜单场景中)、购买按钮本身和弹出菜单
  • 触发通常的购买调用和回调序列。

购买完成后,回调(在另一个线程上)需要:

  • 停止活动指示器
  • 重新启用禁用的菜单元素
  • 用另一个导演替换场景

现在,当我运行此序列并通过反复单击购买按钮等进行测试时,仅一次(并且无法再次重现)我在代码中遇到了崩溃 - 代码和崩溃日志如下所示。我怀疑(并且可能是错误的)这是由于 cocos2d 的非线程安全性质。我该如何避免这种崩溃?我确实需要在开始购买交易之前禁用 UI 元素,并且必须在交易完成后重新启用它们,这将在另一个线程中发生。

代码如下:

-(void) startActivityIndicator {
    mainMenu.enabled = NO;
    scorePopupMenu.enabled = NO;
    menuItemBuyButton.isEnabled = NO;
    [activityIndicatorView startAnimating];
}

-(void) stopActivityIndicator {
    mainMenu.enabled = YES;
    scorePopupMenu.enabled = YES;//this is line 744 that crashed
    menuItemBuyButton.isEnabled = YES;
    if (activityIndicatorView.isAnimating)
        [activityIndicatorView stopAnimating];
}

崩溃日志:

5   SmartRun        0x00126c4c -[MainMenuLayer stopActivityIndicator] (MainMenuLayer.m:744)
4

1 回答 1

0

Then disable repeated tap. Just disable touch once it is pressed and reenable once it is processed.

    node.touchEnabled = NO; //do this for all menu and layer

     (In Cocos2d 1.0 isTouchEnabled to NO) 

We also used inAp purchase in cocos2d game with activity indicator from UIKit. What we did is used activity indicator inside Alert view. That blocks user to do other UI switch.

-(void)ShowActivityIndicator
{
    if(!mLoadingView) //    UIAlertView     *mLoadingView;
    {
        mLoadingView = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];

        UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        actInd.frame = CGRectMake(128.0f, 45.0f, 25.0f, 25.0f);
        [mLoadingView addSubview:actInd];
        [actInd startAnimating];
        [actInd release];


        UILabel *l = [[UILabel alloc]init];
        l.frame = CGRectMake(100, -25, 210, 100);
        l.text = @"Please wait...";
        l.font = [UIFont fontWithName:@"Helvetica" size:16];
        l.textColor = [UIColor whiteColor];
        l.shadowColor = [UIColor blackColor];
        l.shadowOffset = CGSizeMake(1.0, 1.0);
        l.backgroundColor = [UIColor clearColor];
        [mLoadingView addSubview:l];
        [l release];
    }

    [mLoadingView show];
}

-(void)StopIndicator
{
    [mLoadingView dismissWithClickedButtonIndex:0 animated:NO];
}

enter image description here

于 2013-03-18T14:09:43.987 回答