Cocos2d-x游戏开发的一些注意事项

 

把这些天弄Cocos2d-x遇到的一些问题总结在这里,作个记录。

 

声音问题

1) I9100的时候,使用OpenSL,对于播放短小的声音,频繁地new, destroy AudioPlayer对象,在某些机器上会出现死掉。

 解决方案是使用SLDataLocator_AndroidSimpleBufferQueue。

2) 当声音播放数达到1024个时,再也无法打开声音了。原因是使用fopen没有fclose。 另外使用AAssetManager_open 进行open, 使用AAsset_close进行关闭时并没有关闭文件句柄。

 

 

字符编码的问题

1) 最好能在一个游戏的客户端和服务器端统一使用一种编码方式,不然会因为无法判断字符是什么编码而产生非常多的bug.

 

内存使用方面

1)千万不要在子线程中使用autorelease的方法,注意有些类如果使用createXXXX的方法进行创建,其内部也是使用了autoRelease的方法。

原因是,有可能在子线程中该创建该对象,在主线程中就将它kill掉了,你还没等用呢,它就死了。

 

多线程方面

1)在iOS上可以使用POSIX的线程,但在使用前必须先生成一个NSThread,然后立即结束这个线程。目的是通知cocoa(ios系统),我这个程序是多线程的程序。

不这样做的话,在一些低版本的iPhone上,出现pthread_create函数执行成功,但子线程的主函数没有被执行的情况。

可以参考:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html#//apple_ref/doc/uid/10000057i-CH15-SW2 

Using POSIX Threads in a Cocoa Application

Although the NSThread class is the main interface for creating threads in Cocoa applications, you are free to use POSIX threads instead if doing so is more convenient for you. For example, you might use POSIX threads if you already have code that uses them and you do not want to rewrite it. If you do plan to use the POSIX threads in a Cocoa application, you should still be aware of the interactions between Cocoa and threads and obey the guidelines in the following sections.

 2)在iOS上使用信号量时,要使用命名信号量。(sorry,原因不明哈)

可以参考Cocos2d-x中的如下代码: CCTextureCache.cpp

 

#if CC_ASYNC_TEXTURE_CACHE_USE_NAMED_SEMAPHORE
        s_pSem = sem_open(CC_ASYNC_TEXTURE_CACHE_SEMAPHORE, O_CREAT, 0644, 0);
        if( s_pSem == SEM_FAILED )
        {
            CCLOG( "CCTextureCache async thread semaphore init error: %s\n", strerror( errno ) );
            s_pSem = NULL;
            return;
        }
#else
        int semInitRet = sem_init(&s_sem, 0, 0);
        if( semInitRet < 0 )
        {
            CCLOG( "CCTextureCache async thread semaphore init error: %s\n", strerror( errno ) );
            return;
        }
        s_pSem = &s_sem;
#endif


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值