// This method requests the tapjoy server for current virtual currency of the user.//Get currency[Tapjoy getCurrencyBalanceWithCompletion:^(NSDictionary *parameters, NSError *error) { if (error) { //Show error message NSLog(@"getCurrencyBalance error: %@", [error localizedDescription]); } else { //Update currency value of your app NSLog(@"getCurrencyBalance returned %@: %d", parameters[@"currencyName"], [parameters[@"amount"] intValue]); }}];
completion 代码块会返回余额,其中
currencyName
表示货币名称,
amount
表示用户的总金额。如需了解实现细节,请参考 SDK 包中的示例应用程序。
要在 Android 设备上获取当前的虚拟货币余额,请使用以下方法:
Tapjoy.getCurrencyBalance(new TJGetCurrencyBalanceListener(){ @Override public void onGetCurrencyBalanceResponse(String currencyName, int balance) { Log.i(TAG, "getCurrencyBalance returned " + currencyName + ":" + balance); } @Override public void onGetCurrencyBalanceResponseFailure(String error) { Log.i("Tapjoy", "getCurrencyBalance error: " + error); }});
try { let result = await Tapjoy.getCurrencyBalance(); let currencyName = result['currencyName']; let amount = result['amount'];} catch (error: any) { //Handle error}
在 React Native 中,我们对没有参数的
getCurrencyBalance()
使用了一个 Promise。这个 Promise 解析后会返回一个包含
currencyName
和
amount
的字典,若失败则会返回错误。
// Get currencyTapjoyAIR.getCurrencyBalance();// Setup handlersTapjoyAIR.addEventListener(TJCurrencyEvent.GET_CURRENCY_BALANCE_SUCCESS, tapjoyCurrencyEventHandler);TapjoyAIR.addEventListener(TJCurrencyEvent.GET_CURRENCY_BALANCE_FAILURE, tapjoyCurrencyEventHandler);private function tapjoyCurrencyEvents(event:TJCurrencyEvent):void { trace("Tapjoy sample event listener for " + event.type + ", " + event.balance + ", " + event.currencyName);}
您将在指定的
TJCurrencyEvent.GET_CURRENCY_BALANCE_SUCCESS
处理程序中收到货币余额通知,该处理程序将传递
TJCurrencyEvent
对象。此对象包含
currencyName
和
balance
属性。您将在
TJCurrencyEvent.GET_CURRENCY_BALANCE_FAILURE
处理程序中收到错误通知。
检查用户是否已获得货币
当用户自上次检查余额以来获得货币时,通知用户。请参阅以下各节中每个平台的说明以完成通知设置。
为 iOS 设备添加通知观察器以检测获得的货币:
// Set the notification observer for earned-currency-notification.It's recommended that this be placed within the applicationDidBecomeActive method.[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showEarnedCurrencyAlert:) name:TJC_CURRENCY_EARNED_NOTIFICATION object:nil];// In the following method, you can set a custom message or use the default UIAlert to inform the user that they just earned some currency.- (void)showEarnedCurrencyAlert:(NSNotification*)notifyObj{ NSNumber *currencyEarned = notifyObj.object; int earnedNum = [currencyEarned intValue]; NSLog(@"Currency earned: %d", earnedNum); // Pops up a UIAlert notifying the user that they have successfully earned some currency. // This is the default alert, so you may place a custom alert here if you choose to do so. [Tapjoy showDefaultEarnedCurrencyAlert]; // This is a good place to remove this notification since it is undesirable to have a pop-up alert more than once per app run. [[NSNotificationCenter defaultCenter] removeObserver:self name:TJC_CURRENCY_EARNED_NOTIFICATION object:nil];}
为了在用户获得虚拟货币(例如通过完成任务)时收到通知,请使用以下方法来设置货币收入监听器:
// Get notifications whenever Tapjoy currency is earned.Tapjoy.setEarnedCurrencyListener(new TJEarnedCurrencyListener() { @Override public void onEarnedCurrency(String currencyName, int amount) { Log.i("Tapjoy", "You've just earned " + amount + " " + currencyName); }});
TapjoyAIR.addEventListener(TJEarnedCurrencyEvent.EARNED_CURRENCY, tapjoyEarnedCurrencyEventHandler);private function tapjoyEarnedCurrencyEventHandler(event:TJEarnedCurrencyEvent):void{ trace("You can notify user's here that they've just earned " + event.amount + " " + event.currencyName);}
您将在指定的
earned in the TJEarnedCurrencyEvent.EARNED_CURRENCY
处理程序中收到货币收入通知,该处理程序将传递
TJEarnedCurrencyEvent object
。此对象包含
currencyName
和
amount
属性。
花费 Tapjoy 托管货币
要花费用户的一些虚拟货币,请调用以下各节所述特定于平台的方法。
// This method call will deduct 10 virtual currencies from the user's total.[Tapjoy spendCurrency:10 completion:^(NSDictionary *parameters, NSError *error) { if (error) { NSLog(@"spendCurrency error: %@", [error localizedDescription]); } else { NSLog(@"spendCurrency returned %@: %d", parameters[@"currencyName"], [parameters[@"amount"] intValue]); }}];
您将在 completion 代码块中获知货币余额,其中的参数
currencyName
提供货币名称,
amount
提供用户的余额。
Tapjoy.spendCurrency(10, new TJSpendCurrencyListener() { @Override public void onSpendCurrencyResponse(String currencyName, int balance) { Log.i("Tapjoy", currencyName + ": " + balance); } @Override public void onSpendCurrencyResponseFailure(String error) { Log.i("Tapjoy", "spendCurrency error: " + error); }});
您将在指定的
TJSpendCurrencyListener
中的
onSpendCurrencyResponse(String currencyName, int balance)