我在iOS上編寫代碼以在我的cocos2dx遊戲中進行應用購買。我想從Obj C調用我的C++函數。我可以使用* .mm實現文件Obj-C++調用C++靜態函數。但是我想在購買過程中更新用戶界面。我試圖創建一個單例類,但Obj-C仍然不能識別來自單例對象的函數。Obj C調用Cocos2dx 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];
}
它是如何失敗?在'IAPManager.m'上調用'BridgeObjCpp :: sharedBridge() - > isPurchase = true;'應該可以工作(儘管您必須將其重命名爲.mm或將其類型更改爲Objective-C++ Source)。嘗試這樣做,並告訴我們編譯器錯誤,如果這不起作用 – Olotiar
對不起,我不能給失敗報告。我在調整代碼時遇到了另一個錯誤。現在我得到了「詞法或預處理器錯誤 - CCDataVisitor.h:」字符串「文件沒有找到」:( –
是的,在這一點''CCDataVisitor.h''可能包含'#include'包含C++代碼塊作爲'#include')。你可能會更好地編譯你的Objective-C++項目,在你的編譯器標誌中加入'-x objective-C++'。 –
Olotiar