我正在我的cocos2dx游戏中编写iOS应用内购买代码。我想从Obj C调用我的C ++函数。我可以通过使用* .mm实现文件Obj-C ++来调用C ++静态函数。但我想在购买进度的同时更新用户界面。我试图创建一个单例类,但是Obj-C仍然无法识别单例对象中的函数。
C ++:SceneAcc.cpp
void SceneAcc::stateChecker()
{
if(BridgeObjCpp::sharedBridge()->isPurchasing == false)
{
this->unschedule(schedule_selector(SceneAcc::stateChecker));
removeBuyCash();
}
}
// There is an update scheduler to check if the purchase phase done
C ++:BridgeObjCpp.mm
BridgeObjCpp* BridgeObjCpp::sharedBridge(){
if (! s_bridge) {
s_bridge = new BridgeObjCpp();
}
return s_bridge;
}
// Init singleton object
// And bool isPurchasing property in the header
IAPManager.m
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"Complete Transaction...");
// I want something like this
BridgeObjCpp::sharedBridge()->isPurchase = true;
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
}
答案 0 :(得分:1)
您需要修改BridgeObjCpp.mm或IAPManager.m。
<强> BridgeObjCpp.h 强>
@interface BridgeObjCpp : NSObject
+(void)setPurchasing:(BOOL)purchasing:
@end
<强> BridgeObjCpp.mm 强>
@implementation BridgeObjCpp
+(void)setPurchasing:(BOOL)purchasing {
BridgeObjCpp::sharedBridge()->isPurchase = purchasing ;
}
@end
<强> IAPManager.m 强>
[BridgeObjCpp setPurchasing:YES];
或强>
注意:在.mm文件中,编译器需要混合使用目标C和C ++代码。在.m文件中,它只期望客观的C代码。所以请分别进行编码。