2013-01-02 35 views
0

我與cocos2d新,所以我有我的應用程序內購買類助手的問題。我在Cocoa Touch中編寫遊戲,並且這個類完美地工作,但是我現在在Cocos2d中編寫了相同的遊戲,問題與NSString有關。如何正確使用cocos2d來處理這個NSString?

這是一些片段。

當我點擊某個按鈕時,此方法被稱爲第一個。正如你所看到的completeIdentifier是簡單的字符串bundleIdentifier和一些字符串參數。沒關係,在這個地方我可以登錄這個completeIdentifier

- (void)prepareToPurchaseItemWithIdentifier:(NSString *)aIdentifier showAlertWithTitle:(NSString *)title description:(NSString *)description delegate:(id)aDelegate{ 

    self.delegate = aDelegate; 

    identifier = aIdentifier; 
    completeIdentifier = [NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] bundleIdentifier], aIdentifier]; 

    askToPurchase = [[UIAlertView alloc] initWithTitle:title message:description delegate:self cancelButtonTitle:nil otherButtonTitles:@"Later", @"Yes", nil]; 
    askToPurchase.delegate = self; 
    [askToPurchase show]; 
} 

下一個方法是UIAlertViewDelegate方法。當我從第一種方法點擊YESalertView時被調用。

#pragma mark - UIAlertViewDelegate Methods 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    NSLog(@"%@", completeIdentifier); 
    if (alertView == askToPurchase) { 

     NSLog(@"[TSIAPHelper] Clicked YES. Prepare..."); 
     if ([SKPaymentQueue canMakePayments]) { 

      NSLog(@"[TSIAPHelper] Prepare to purchase [%@].",completeIdentifier); 
      SKProductsRequest *request =[[SKProductsRequest alloc] initWithProductIdentifiers: 
            [NSSet setWithObject:completeIdentifier]]; 
      request.delegate = self; 
      [request start]; 

      pleaseWait = [[UIAlertView alloc] initWithTitle:@"Please wait..." message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil]; 
      UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 
      [activity startAnimating]; 
      [pleaseWait addSubview:activity]; 
      activity.frame = CGRectMake(125, 50, 36, 36); 
      [pleaseWait show]; 
     } 
     else { 

      NSLog(@"[TSIAPHelper] Purchase [FAILURE]. Prohibited by Parentar Control or something like that."); 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Prohibited." message:@"Parental Control is enabled, cannot make a purchase. Turn off and try again." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil]; 
      [alert show]; 
     } 
    } 
} 

的問題是:無論何時無論我想記錄變量completeIdentifier我已經得到了與選定的線路崩潰:0x39e965d0: ldr r3, [r4, #8]但我不知道那是什麼意思。並選擇此行:

NSLog(@"%@", completeIdentifier); 

我該如何解決?在可可觸摸工作完美。當我使用cocos2d不是。

+0

當我除去所有'completeIdentifier'或其他變量存儲此:'[的NSString stringWithFormat:@ 「%@%@。」,[[一個NSBundle mainBundle] bundleIdentifier],aIdentifier]'和粘貼'[NSString的stringWithFormat:@「%@。%@」,[[NSBundle mainBundle] bundleIdentifier],aIdentifier]'直接到NSLog或其他工作正常的地方。這對我來說很奇怪......任何人都知道問題在哪裏? –

回答

1

我想你沒有使用ARC。在這種情況下,completeIdentifier將被自動發佈。

Cocos2d清除autoreleasepool的每一幀,而在Cocoa這並不嚴格定義,但可能仍會崩潰。您可以通過保留或複製字符串來解決此問題。

completeIdentifier = [NSString stringWithFormat:@"%@.%@", [[NSBundle mainBundle] bundleIdentifier], aIdentifier]; 
completeIdenfitier = [completeIdentifier retain]; 
+0

謝謝!這是一個問題。 –