如何在不同cpps中创建的节点之间进行交互...?

时间:2016-05-13 10:21:10

标签: android c++ ios cocos2d-x

有超过20个层,如Layer01.cpp,Layer02.cpp,Layer03.cpp ...... 在HelloWorld.cpp中有一个名为“itemSlots”的tableview。 当用户触摸Layer01中的龙按钮时, HelloWorld.cpp中的香蕉精灵会消失, 并且一个便便出现在itemSlots中。 这就是我想做的一切,我认为这很简单。 我也制作了图层和桌面视图,如下所示, 但仍然找不到按钮和精灵之间进行交互的方法 它们是用不同的cpps制作的。

  
    
      

Layer01.h

    
  
#ifndef __LAYER01__H__
#define __LAYER01__H__

#include "cocos2d.h"
#include "ui/CocosGUI.h"

class Layer01 : public cocos2d::LayerColor
{
public:
    Layer01();

    virtual bool init();

    cocos2d::ui::Button* dragon;

    void touchDragon();

private:

};

#endif
  
    
      

Layer01.cpp

    
  
#include "Layer01.h"

USING_NS_CC;

Layer01::Layer01()
{
    bool bOk = initWithColor(Color4B::BLACK, 750, 400);
    if (bOk == true) { 
        this->autorelease(); 
        init();
    };
}

bool Layer01::init()
{
    scene01 = Sprite::create("images/scene01.jpg");
    scene01->setScale(this->getContentSize().width/sc02a->getContentSize().width);
    scene01->setAnchorPoint(Point::ZERO);
    scene01->setPosition(Point::ZERO);
    this->addChild(scene01);

    dragon = ui::Button::create("images/dragon.png", "images/dragon.png", "");
    dragon->setContentSize(Size(50, 50));
    dragon->setPosition(Point(250,300));
    dragon->addClickEventListener(CC_CALLBACK_0(Layer01::touchDragon, this));
    this->addChild(dragon); 

    return true;
}

void Layer01::touchDragon()
{
    /*
        layer01->removeChild(banana);
        auto poopCell = itemSlots->cellAtIndex(2);
        poopCell->addChild(poop);
    */
}
  
    
      

HelloWorld.h

    
  
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "Layer01.h"
#include "ui/CocosGUI.h"
#include "cocos-ext.h"
#include "CustomTableViewCell.h"


class HelloWorld : public cocos2d::Layer,
    public cocos2d::extension::TableViewDataSource,
    public cocos2d::extension::TableViewDelegate
{
public:
    static cocos2d::Scene* createScene();
    virtual bool init();
    CREATE_FUNC(HelloWorld);


    cocos2d::Sprite* banana;
    cocos2d::Sprite* poop;
    cocos2d::extension::TableView* itemSlots;


    virtual void tableCellTouched(cocos2d::extension::TableView* table, 
                                  cocos2d::extension::TableViewCell* cell);
    virtual cocos2d::Size tableCellSizeForIndex
                                 (cocos2d::extension::TableView* table, ssize_t idx);
    virtual cocos2d::extension::TableViewCell* tableCellAtIndex
                                 (cocos2d::extension::TableView* table, ssize_t idx);
    virtual ssize_t numberOfCellsInTableView(cocos2d::extension::TableView* view);

};

#endif // __HELLOWORLD_SCENE_H__
  
    
      

HelloWorld.cpp

    
  
#include "HelloWorldScene.h"

USING_NS_CC;
USING_NS_CC_EXT;

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }

    layer01 = new Layer01();
    layer->setPosition(Point(0, 100));
    this->addChild(layer01);  


    banana = Sprite::create("images/banana.png");
    banana->setPosition(Point(300,300));
    layer01->addChild(banana);

    poop = Sprite::create("images/poop.png");

    itemSlots = TableView::create(this, Size(535, 70));
    itemSlots->setDirection(ScrollView::Direction::HORIZONTAL);
    itemSlots->setPosition(Point(115, 15));
    itemSlots->setDelegate(this);
    this->addChild(itemSlots);
    itemSlots->reloadData();

    return true;
}


void HelloWorld::tableCellTouched(TableView* table, TableViewCell* cell)
{
}  
Size HelloWorld::tableCellSizeForIndex(TableView* table, ssize_t idx)
{
    return Size(77, 77);
}
TableViewCell* HelloWorld::tableCellAtIndex(TableView* table, ssize_t idx)
{
    auto string = String::createWithFormat("%ld", idx);
    TableViewCell* cell = table->dequeueCell();

    if (cell == false)
    {
        cell = new CustomTableViewCell();
        cell->autorelease();

        auto sprite01 = Sprite::create();
        sprite01->setAnchorPoint(Point::ZERO);
        sprite01->setPosition(Point::ZERO);
        cell->addChild(sprite01);

        auto label = LabelTTF::create(string->getCString(), "arial", 20.0);
        label->setAnchorPoint(Point::ZERO);
        label->setPosition(Point(5, 5));
        label->setTag(120);
        cell->addChild(label);
    }
    else {
        auto label = (LabelTTF*)cell->getChildByTag(120);
        label->setString(string->getCString());
    }

    return cell;
} 
ssize_t HelloWorld::numberOfCellsInTableView(TableView* table)
{
    return 20;
}

1 个答案:

答案 0 :(得分:1)

最简单但不安全的方法是执行以下操作:

//Layer01.cpp
#include "HelloWorld.h"
...
void Layer01::touchDragon()
{
    removeChild(banana);

    // As your parent is a HelloWorld you can cast it:
    HelloWorld* helloWorld = (HelloWorld*)getParent();
    auto poopCell = helloWorld->itemSlots->cellAtIndex(2);
    poopCell->addChild(helloWorld->poop);
}

但是poop会在那时自动进行垃圾回收。创建后需要retain,不再需要时release

更好的选择是在HelloWorld中设置指向Layer01图层的指针,并使用单独的方法设置poop

  

Layer01.h

// Forward declaration
class HelloWorld;

class Layer01
{
    ...
    HelloWorld* m_hellowWorld;

    // Don't forget about create function
    static Layer01* create(HelloWorld* helloWorld)
    {
        Layer01* result = new (std::nothrow) Layer01();
        if(result && result->init(helloWorld))
        {
            result->autorelease();
        }
        else
        {
            delete result;
            result = nullptr;
        }
        return result;
    }

    bool Layer01::init(HelloWorld* helloWorld);

    ...
};
  

Layer01.cpp

#include "Layer01.h"
#include "HelloWorld.h"

Layer01::Layer01()
: HelloWorld(nullptr)
{}

bool Layer01::init(HelloWorld* helloWorld)
{
    removeChild(banana);
    m_hellowWorld = helloWorld;
    ...
}

void Layer01::touchDragon()
{
    m_hellowWorld->setPoop();
}
  

HelloWorld.h

class HelloWorld
{
    ...
    void setPoop();
    ...
};
  

HelloWorld.cpp

...
void HelloWorld::setPoop()
{
    auto poopCell = itemSlots->cellAtIndex(2);
    poopCell->addChild(Sprite::create("images/poop.png"));
}