cocos2d-x 学习日志(1)之Xcode中c++&Object-C混编,详细介绍如何在cocos2dx中访问object函数以及Apple Api

本篇继续介绍另外一个在Cocos2dx中必经之路:在Cocos2dx中调用苹果Api以实现后期iOS的GameCenter和iap的相关操作, 那么这里就跟大家简单分享探讨下;如何在Xcode中进行c++与oc混编吧~

参考王哥说的 SimpleAudioEngine 类;

首先建立了两个类,一个object-c ,一个c++,详细如下:

HSpriteOC.h
#import <Foundation/Foundation.h>
NSString *str;
@interface HSpriteOC 
+(void)testLog;
+(void)testLogWithStr:(NSString*)_str;
+(void)hMessageBox:(NSString*)pszMsg title:(NSString*) pszTitle;
@end
HSpriteOC.m
#import "HSpriteOC.h"
@implementation HSpriteOC
+(void) testLog
{
    str = @"feng->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];
}
@end

这个类比较简单,简单定义了几个静态函数,打印和显示一个提示框,不赘述,大家大概看下就可以了;

下面来看c++的类:

HSpriteCPP.h

#ifndef __QQLogin__HSpriteCPP__
#define __QQLogin__HSpriteCPP__
#include "cocos2d.h"
using namespace cocos2d;
class HSpriteCPP:public cocos2d::CCSprite {
public:
    static HSpriteCPP* hspriteWithFile(const char *spName);
    void myInit();
    virtual ~HSpriteCPP();
};
#endif /* defined(__QQLogin__HSpriteCPP__) */
HSpriteCPP.cpp

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "HSpriteOC.h"
#endif
#include "HSpriteCPP.h"
HSpriteCPP* HSpriteCPP::hspriteWithFile(const char *spName)
{
    HSpriteCPP *pobSprite = new HSpriteCPP();
    if (pobSprite && pobSprite->initWithFile(spName)) {
        pobSprite->myInit();
        pobSprite->autorelease();
        return pobSprite;
    }
    CC_SAFE_DELETE(pobSprite);
    return NULL;
}
void HSpriteCPP::myInit()
{
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
   //iOS代码
    [HSpriteOC testLog];
    [HSpriteOC testLogWithStr:@"wahaha"];
    [HSpriteOC hMessageBox:@"cocos2dx调用oc函数" title:@"feng"];
    #else
    #endif
}
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++都比较熟悉更效率)

然后在HelloWorldScene.cpp中加入以下测试代码:

 HSpriteCPP *sp = HSpriteCPP::hspriteWithFile("Icon.png");

   sp->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width*0.5,CCDirector::sharedDirector()->getWinSize().height*0.5-100));

    this->addChild(sp,0);


别忘记导入对应使用的类哦~OK,看运行效果:

cocos2d-x 学习日志(8)之Xcode中c++Object-C混编,详细介绍如何在cocos2dx中访问object函数以及Apple Api - 热血枫叶 - 热血枫叶

参考博客:http://www.himigame.com/iphone-cocos2dx/743.html



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热血枫叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值