天天看點

【IOS-COCOS2D-X 遊戲開發之十四】XCODE中C++&OBJECT-C混編,詳細介紹如何在COCOS2DX中通路OBJECT函數以及APPLE API

cocos2dx系列博文的上一篇詳細介紹了如何在xcode中利用jni調用android的java層代碼,還沒有看過的童鞋,請移步到如下博文:

<a href="http://www.himigame.com/iphone-cocos2dx/www.himigame.com/android-game/725.html">【ios-cocos2d-x 遊戲開發之十三】詳細講解在xcode中利用預編譯并通過jni調用android的java層代碼(cocos2dx裡通路調用android函數)! </a>

本篇繼續介紹另外一個在cocos2dx中必經之路:在cocos2dx中調用蘋果api以實作後期ios的gamecenter和iap的相關操作, 那麼himi這裡就跟大家簡單分享探讨下;如何在xcode中進行c++與oc混編吧~

參考王哥說的 simpleaudioengine 類;

【IOS-COCOS2D-X 遊戲開發之十四】XCODE中C++&amp;OBJECT-C混編,詳細介紹如何在COCOS2DX中通路OBJECT函數以及APPLE API

首先himi建立了兩個類,一個object-c ,一個c++,詳細如下:

hspriteoc.h

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

//

//  hspriteoc.h

//  oc_cpp

//  created by himi  on 12-4-10.

//  copyright (c) 2012年 himi. all rights reserved.

#import &amp;lt;foundation/foundation.h&amp;gt;

nsstring * str;

@interface hspriteoc

+(void) testlog;

+(void) testlogwithstr:(nsstring*)_str;

+(void) hmessagebox:(nsstring*)pszmsg title:(nsstring*)psztitle;

@end

hspriteoc.m

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

//  hsprite.m

//  created by himi on 12-4-10.

#import "hspriteoc.h"

@implementation hspriteoc

+(void) testlog{

    str = @"himi-&amp;gt;string is: ";

    nslog(@"hsprite: testlog");

}

+(void) testlogwithstr:(nsstring*)_str{

    str = [nsstring stringwithformat:@"%@%@",str,_str];

    nslog(@"%@",str);

+(void) hmessagebox:(nsstring*)pszmsg title:(nsstring*)psztitle{

    uialertview * messagebox = [[uialertview alloc] initwithtitle: psztitle

                                                          message: pszmsg

                                                         delegate: nil

                                                cancelbuttontitle: @"ok"

                                                otherbuttontitles: nil];

    [messagebox autorelease];

    [messagebox show];

這個類比較簡單,簡單定義了幾個靜态函數,列印和顯示一個提示框,不贅述,大家大概看下就可以了;

下面來看c++的類:

hspritecpp.h

//  hspritecpp.h

#ifndef oc_cpp_hsprite_h

#define oc_cpp_hsprite_h

#include "cocos2d.h"

using namespace cocos2d;

class hspritecpp:public cocos2d::ccsprite {

public:

    static hspritecpp* hspritewithfile(const char *spname);

    void myinit();

    virtual ~hspritecpp();

};

#endif

hspritecpp.cpp

36

37

38

39

40

41

42

//  hspritecpp.mm

#if (cc_target_platform == cc_platform_ios)

#include "hspriteoc.h"

#include "hspritecpp.h"

hspritecpp* hspritecpp::hspritewithfile(const char *spname)

{

    hspritecpp *pobsprite = new hspritecpp();

    if (pobsprite &amp;amp;&amp;amp; pobsprite-&amp;gt;initwithfile(spname))

    {

        pobsprite-&amp;gt;myinit();

        pobsprite-&amp;gt;autorelease();

        return pobsprite;

    }

    cc_safe_delete(pobsprite);

return null;

void hspritecpp::myinit(){

    //ios代碼

[hspriteoc testlog];

    [hspriteoc testlogwithstr:@"wahaha"];

    [hspriteoc hmessagebox:@"cocos2dx調用oc函數" title:@"himi"];

#else

    //android代碼

hspritecpp::~hspritecpp(){

此類是個自定義精靈類,都是簡單的建立等函數,其hsprite.cpp類的導入和在 myinit() 自定義初始化函數中都加入了預編譯(#if #else #endif 對預編譯不太了解的自定百度下吧),主要為了差別目前手機裝置的平台區分做處理;而且在myinit()中我使用了object-c文法進行調用之前oc寫的hsprite類函數;

其實通過觀察以上兩個類童鞋們估計很容易看出在xcode中cpp和oc如何混編;其實就是兩點:

1. 混編的類需要将其實作類(.cpp)改成(.mm)類,那麼xcode就會智能知道這個類混編類了,不用複雜的操作;

2. 混編中cpp調用oc,其實就是各自使用各自文法即可,沒差異!(最好對oc和c++都比較熟悉更效率)

然後himi在helloworldscene.cpp中加入以下測試代碼:

hspritecpp * sp =hspritecpp::hspritewithfile("icon.png");

    sp-&amp;gt;setposition(ccp(ccdirector::shareddirector()-&amp;gt;getwinsize().width*0.5,ccdirector::shareddirector()-&amp;gt;getwinsize().height*0.5-100));

this-&amp;gt;addchild(sp);

别忘記導入對應使用的類哦~ok,看運作效果:

【IOS-COCOS2D-X 遊戲開發之十四】XCODE中C++&amp;OBJECT-C混編,詳細介紹如何在COCOS2DX中通路OBJECT函數以及APPLE API

繼續閱讀