2013-03-09 29 views
0

在我使用cocos2d-iphone 2.0.0的iOS遊戲中,我彈出一個圖層,彈出一個要求用戶購買應用內購買的按鈕,供用戶點擊購買(menuItemBuyButton)。當用戶點擊此購買按鈕,三件事情要做到:如何避免引用cocos2d-iphone(2.0.0)UI類的異步調用中的崩潰?

  • 活動指示器啓動
  • 層上的所有菜單項將被禁用 - 具體地說是主菜單(此代碼是在主菜單中的場景),購買按鈕本身和彈出菜單
  • 購買調用和回調的通常順序被觸發。

當購買完成後,回調(這是另一個線程),則需要:

  • 停止活動的指標
  • 重新啓用已禁用菜單元素
  • 更換場景另一個董事

現在,當我運行這個序列,並通過反覆點擊購買按鈕等等,只測試一次(和你再次重新報道)我在代碼崩潰 - 代碼和崩潰日誌如下。我懷疑(這可能是錯誤的),這是由於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) 
+1

在哪個線程'stopActivityIndi​​cator'叫什麼名字?背景還是主要? – Sebastian 2013-03-13 10:34:11

回答

0
然後

禁用重複抽頭。只要按下觸摸按鈕即可禁用,並在處理後重新啓用。

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

    (In Cocos2d 1.0 isTouchEnabled to NO) 

我們還使用了UIKit的活動指示器在cocos2d遊戲中的購買活動。我們所做的是在警報視圖內使用活動指示器。阻止用戶進行其他UI切換。

-(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

+0

謝謝,但在我的情況下添加警報是不可接受的 - 我希望能夠在不更改UI的情況下執行此操作。 – Anand 2013-03-21 03:58:41

+0

然後將cocos2d菜單和圖層的touchEnabled設置爲NO(在Cocos2d 1.0 isTouchEnabled中爲NO)。這工作肯定:) – Guru 2013-03-21 04:01:17

+0

更新了我的答案。 – Guru 2013-03-21 04:04:35