cocos2d - x 서로 다른 층 제어 정령 및 충돌 검출

3991 단어 helloworldcocos2d-x
하 이, 추천 파일 "충돌 검출 예 2. zip"http://vdisk.weibo.com/s/J78kk
새 프로젝트 를 만 든 후 HudLayer 클래스 를 새로 만 듭 니 다.
HudLayer.h
#include "cocos2d.h"
USING_NS_CC;
class HudLayer:public CCLayer
{
public:
    CREATE_FUNC(HudLayer);
    bool init();
    
    //     
    void start();
};

HudLayer.cpp
bool HudLayer::init()
{
    bool pRet = false;
    do {
        CC_BREAK_IF(!CCLayer::init());
        //      
        CCMenuItemImage* start = CCMenuItemImage::create("Icon-72.png", "Icon-72.png", this, menu_selector(HudLayer::start));
        start->setPosition(ccp(100, 100));
        CCMenu* pMenu = CCMenu::create(start,NULL);
        this->addChild(pMenu);
        pRet = true;
    } while (0);
    return pRet;
}


void HudLayer::start()
{
    //             
    CCNotificationCenter::sharedNotificationCenter()->postNotification("change");
}

Hello World Scene 류 에 대해 다음 과 같이 수정 합 니 다.
HelloWorldScene.h
#include "HudLayer.h"
USING_NS_CC;
class HelloWorld : public cocos2d::CCLayerColor
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();

    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    HelloWorld();
    //       
    CCArray* _arr;
    //       
    CCArray* _mon;
    
    void update(float dt);
    
    
    HudLayer* hud;
    void change();
    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
};

HelloWorldScene.cpp
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)))
    {
        return false;
    }

    //      , HudLayer       
    CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(HelloWorld::change), "change", NULL);
    
    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    cache->addSpriteFramesWithFile("zzzz.plist");
    
    CCSpriteBatchNode* node = CCSpriteBatchNode::create("zzzz.pvr.ccz");
    this->addChild(node);
    
                                                            
    _arr = CCArray::create();
    _arr->retain();
    CCSprite* player = CCSprite::createWithSpriteFrameName("Player.png");
    player->setPosition(ccp(100, 100));
    node->addChild(player);
    _arr->addObject(player);
    hud = HudLayer::create();
    this->addChild(hud);
    
    //       
    _mon = CCArray::create();
    _mon->retain();
    for (int i = 0; i <10;i++ )
    {
        CCSprite* monster = CCSprite::createWithSpriteFrameName("projectile2.png");
        node->addChild(monster);
        _mon->addObject(monster);
        monster->setPosition(ccp(100, 200));
    }
    
    this->scheduleUpdate();
    return true;
}

void HelloWorld::update(float dt)
{
    CCObject* obj1 = NULL;
    CCObject* obj2 = NULL;
    
    CCARRAY_FOREACH(_arr, obj1)
    {
        CCSprite* player = (CCSprite*)obj1;
        CCArray* monsDelete = CCArray::create();
        CCARRAY_FOREACH(_mon, obj2)
        {
            CCSprite* mon = (CCSprite*)obj2;
            if (player->boundingBox().intersectsRect(mon->boundingBox()))
            {
                monsDelete->addObject(mon);
            }
        }
        CCARRAY_FOREACH(monsDelete, obj2)
        {
            CCSprite* mon = (CCSprite*)obj2;
            mon->setVisible(false);
        }
    }
}

void HelloWorld::change()
{
    CCSprite* player1 = (CCSprite*)_arr->objectAtIndex(0);
    CCMoveBy* moBy = CCMoveBy::create(1, ccp(0, 100));
    player1->runAction(moBy);
}

좋은 웹페이지 즐겨찾기