zoukankan      html  css  js  c++  java
  • cocos2d基础篇笔记四

    1.//有两种集合

    //第一种是array 特点:插入,删除效率低,可是查找效率高

    //另外一种是list  特点:插入,删除效率高,可是查找效率低

    //分析这个游戏: 插入的时候:怪物,射弹出现时,删除的时候:碰撞时,怪物、射弹出界时。
    //遍历:fps(每秒中填充图像的帧数(帧/秒)相应的时间,怪物是2秒出现一次,而遍历是60次每秒,可见遍历用的较多,所以我们选择array。
    CCArray*_targets;//定义怪物集合,3.0一般用vector定义集合
    CCArray*_projs;//定义射弹集合

    2.集合的初始化和释放

    _targets=new CCArray;

    _projs=new CCArray;
    //cocos2d中Class:create不须要手动释放

    //new须要手动释放,我们把它放在析构函数释放。

    HelloWorld::~HelloWorld(){
    if(_targets!=NULL)
    _targets->release();
    if(_projs!=NULL)
    _projs->release();
    }

    3.开启update函数(默认是没激活的)

    this->schedule(schedule_selector(HelloWorld::update));//开启update函数

    4.集合的遍历:

    void HelloWorld::update(float dt){                                               //dt为刷新周期=1/fps
    CCObject*itarget;
    CCObject*iproj;
    CCArray*targetToDelect=new CCArray;                         //假设当有交集时就直接从容器移除而且清楚靶子或者射弹会导致下次遍历越域,因此我们又一次定义两个集合来保                                                                                                               存,发生碰撞的靶子和射弹,然后在遍历这两个集合在进行移除和清理,就不会发生越域的情况。
    CCArray*projToDelect=new CCArray;                            
    CCARRAY_FOREACH(_targets,itarget){                           //为了方便遍历容器里面的元素,cocos2dx提供了CCARRAY_FOREACH这种宏
    CCSprite*target=(CCSprite*)itarget;
    CCRect targetZone=CCRectMake(target->getPositionX(),
    target->getPositionY(),
    target->getContentSize().width,
    target->getContentSize().height);


    CCARRAY_FOREACH(_projs,iproj){
    CCSprite*proj=(CCSprite*)iproj;
    CCRect projZone=CCRectMake(proj->getPositionX(),
    proj->getPositionY(),
    proj->getContentSize().width,
    proj->getContentSize().height);


    if(projZone.intersectsRect(targetZone)){
    targetToDelect->addObject(itarget);
    projToDelect->addObject(iproj);
    }
    }                                                              //遍历怪物
    }                                                                     / /遍历靶子
    CCARRAY_FOREACH(targetToDelect,itarget){
    _targets->removeObject(itarget);
    CCSprite*target=(CCSprite*)itarget;
    target->removeFromParentAndCleanup(true);
    }
    CCARRAY_FOREACH(projToDelect,iproj){
             _projs->removeObject(iproj);
    CCSprite*proj=(CCSprite*)iproj;
    proj->removeFromParentAndCleanup(true);
    }


    }

  • 相关阅读:
    通过PROFINET网络实现SINAMICS 120的PN IO OPC通讯,起动及调速控制
    Python datetime获取当前年月日时分秒
    计算机网络:套接字(Socket)| Python socket实现服务器端与客户端通信,使用TCP socket阿里云ECS服务器与本机通信
    Ubuntu16.04安装、卸载宝塔软件
    Ubuntu一键安装LAMP,LNMP
    STM32使用K型热电偶测温:运算放大器+内置ADC+K型热电偶分度表+中间温度定律 | K型热电偶的温度-热电势曲线
    盘点几种DIY加密狗的制作方法,适用于穿越机模拟器
    变频器通讯参数PKW和PZD的含义
    穿越机从0到起飞:选件
    西门子S7-1200PLC不让下载一直报“模块具有激活的测试和调试功能,防止下载到设备”解决方法
  • 原文地址:https://www.cnblogs.com/yxwkf/p/3997104.html
Copyright © 2011-2022 走看看