gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:27:35 25 4
gpt4 key购买 nike

Layer01.cpp、Layer02.cpp、Layer03.cpp 等 20 多个层...在 HelloWorld.cpp 中有一个名为“itemSlots”的 TableView 。当用户触摸 Layer01 中的龙按钮时,HelloWorld.cpp 中的香蕉 Sprite 将消失,并且一个便便出现在 itemSlots 中。这就是我想要做的,我认为这很简单。我也制作了 Layers 和 tableview,如下所示,但仍然找不到在按钮和 Sprite 之间进行交互的方法它们是用不同的 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;
}

最佳答案

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

//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 会在那时自动被垃圾回收。您需要在创建后保留它,并在不再需要时释放


更好的选择是在 Layer01 中有一个指向 HelloWorld 层的指针,并有一个单独的方法来设置 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"));
}

关于android - 如何在不同 cpps 中创建的节点之间进行交互...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37207477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com