您的位置:首页 > 其它

coco2dx开发的小总结篇章 不断更新

2013-12-26 15:51 253 查看
1:didAccelerate(CCAcceleration *pAccelerationValue) 得到重力改变的函数 就是你想做一个摇晃类游戏得到z的变换就是这个接口。setIsAccelerometerEnabled(true);

2:voidvisit()

{

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    CCSize winSizeInPixels = CCDirector::sharedDirector()->getWinSizeInPixels();

    float scaleX = winSizeInPixels.width / ORIGIN_WIDTH;

    float scaleY = winSizeInPixels.height / ORIGIN_HEIGHT;

    

    glEnable(GL_SCISSOR_TEST);

    glScissor(0 , 100, winSize.width,300);

    

    CCLayer::visit();

    

    glDisable(GL_SCISSOR_TEST);

    

} //这个就是j2me里面的setclip啦。设置当前层的c裁剪区域。

3:随机数  arc4random(); 或者 srand((unsigned)time(NULL));//必须要 不然种子不会变得到的随机数永远都是一样的 rand()%100;

4:删除文件 c语言自带:int r = remove(“path”); //删除文件

5:文件以及数据库操作需要做线程安全处理;

6:图层layer出错 可以使用函数设置成2d模式再试试:CCDirector::sharedDirector()->setProjection(CCDirectorProjection2D);

7: 二进制png生成sprite:

CCImage image;

        unsigned long nSize  = pngLength;

        unsigned char* pBuffer = pngDatas;

        image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtPng);        CCTexture2D* texture = new CCTexture2D();

        texture->initWithImage(&image);        

        if (!texture)

        {

            return false;

        }        

        CCSprite* sprite = CCSprite::spriteWithTexture(texture);

        texture->release();

8:c语言下判断文件时候存在

用函数access,头文件是stdio.h,原型: 
   
int   access(const   char   *filename,   int   amode); 

amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1。 

这个函数还可以检查其它文件属性: 

06     检查读写权限 
04     检查读权限 
02     检查写权限 
01     检查执行权限 
00     检查文件的存在性
在UNIX和VC下实验成功。
好处是 fopen(..,"r")不好,当无读权限时一不行了。
而这个就算这个文件没有读权限,也可以判断这个文件存在于否
存在返回0,不存在返回-1
#include <stdio.h>
int main()
{       
printf ("%d",access("111",0));
}

9:char c[10]; sprintf(c,"123456789012");

printf("%s",c);

居然能全部打印出来,这个隐形错误让我郁闷了2个小时,哎呀呀呀。bt

10:闪屏问题 看中文网站http://cn.cocos2d-x.org/resource/show?nid=80 第二个方法

11:日期相关 得到日期 并计算相差的天数 以日为单位

structcc_timeval now;

if( CCTime::gettimeofdayCocos2d(&now,NULL) == 0)

    {

        structtm *t;

        t = localtime(&now.tv_sec);

        int nowYear = t->tm_year+1900 ,nowMonth = t->tm_mon+1,nowDay = t->tm_mday;

        char time[100];

        sprintf(time,"%d-%d-%d",nowYear,nowMonth,nowDay);

        

        if (time.length() > 0)

        {

            char* token = strtok((char*)timeStr.c_str(),"-");

            int lastYear,lastMonth,lastDay;            

            int j = 0;

            while( token != NULL )                

            {                

                if (j == 0)

                {

                    lastYear = atoi(token);

                }

                elseif (j == 1)

                {

                    lastMonth = atoi(token);

                }

                elseif ( j == 2)

                {

                    lastDay = atoi(token);

                }

                j++;                

                token = strtok( NULL,"-");                 

            }

            CCLog("lastYear:%d lastMonth:%d lastDay:%d",lastYear,lastMonth,lastDay);

            int disDays = getDays(nowYear, nowMonth, nowDay, lastYear, lastMonth, lastDay);

            CCLog("disDay:%d",disDays);          

        } 

int getDays(int year,int month,int day,int lastYear,int lastMonth,int lastDay)

{

    int  iTotalDays = 0;

    int  iMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; // Initiate month

    if (isLeapYear(lastYear))

    {

        iMonth[2] = 29;

    }

    else

    {

        iMonth[2] = 28;

    }

    

    while (!(year == lastYear && month == lastMonth && day == lastDay)) {

        lastDay++;

        if ( lastDay == iMonth[lastMonth]) {

            lastDay = 0;

            lastMonth++;

        }

        if (lastMonth == 13) {

            lastMonth = 1;

            lastYear ++;

            if (isLeapYear(lastYear)) 

            {

                iMonth[2] = 29;   // Leap year February is 29 days.

            }

            else

            {

                iMonth[2] = 28;

            }

        }

        iTotalDays++;

    }

    return iTotalDays;

    

}

bool isLeapYear(int year)

{

    bool isLeapYear = false;

    if ((year%4==0 && year%100) || year%400 ==0 )

    {

        isLeapYear =  true;

    }

   

    return isLeapYear;

}

 12:uiview 旋转这只位置

//            CGAffineTransform transform = adView.transform; //旋转

//            transform = CGAffineTransformRotate(transform, 3.14F/2);

//            adView.transform = transform;            

            

//            adView.center = CGPointMake(200, 500); //中心点设置坐标

            // 设置位置

            CGRect frame = adView.frame;

13:c/c++ 随机数使用前只需要初始化一次随机数种子 srand((unsigned)time(NULL)); //初始化随机数种子

不然多次初始化太消费cpu了。

            frame.origin.x = 0;

            frame.origin.y = 0;

            adView.frame = frame;

13:os下的定时器

 

    //    [NSTimer scheduledTimerWithTimeInterval: 90

    //                                     target: self

    //                                   selector: @selector(handleTimer)

    //                                   userInfo: nil

    //                                    repeats: NO];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: