大家好我这次呢要给大家讲的是cocos2dx游戏开发实战项目值背包系统的开发,大家都知道背包是什么都不用我仔细说明了,好废话不多说了看客户端代码,
这里我是继承的cocos 里面的TableView来开发的,我为什么用这个而不自己写,这个嘛我不好意思说有点,应为我比较害懒了。
#ifndef __BAG_H__
#define __BAG_H__
#include "cocos2d.h"
//#include "ui/UIWidget.h"
#include "cocostudio/CocoStudio.h"
/* #include "ui/CocosGUI.h" #include "ui/UIScrollView.h" #include "ui/UIScale9Sprite.h"*/
#include "extensions/cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace cocos2d::ui;
//class Bag : public Layer,public ScrollView,public ScrollViewDelegate //,public cocos2d::extension::ScrollViewDelegate
class Bag : public Layer,public cocos2d::extension::TableViewDataSource,public cocos2d::extension::TableViewDelegate
{
public:
Bag();
~Bag();
static cocos2d::Scene* bagScene();
virtual bool init();
//virtual void BagRollView(cocos2d::extension::ScrollView* view){};
virtual void //scrollViewDidZoom(cocos2d::extension::ScrollView* view) {}
//点击哪个cell;
virtual void tableCellTouched(cocos2d::extension::TableView* table,cocos2d::extension::TableViewCell* cell);
//每个cell的size;
virtual cocos2d::Size tableCellSizeForIndex(cocos2d::extension::TableView *table,ssize_t idx);
//生成cell;
virtual cocos2d::extension::TableViewCell* tableCellAtIndex(cocos2d::extension::TableView *table,ssize_t idx);
//cell的数量;
virtual ssize_t numberOfCellsInTableView(cocos2d::extension::TableView *table);
//背包里面的物品按钮的监听;
void BagtouchEvent(Ref *pSender,Widget::TouchEventType type);
Sprite* Bag_sprite;
CREATE_FUNC(Bag);
};
#endif // !__BAG_H__
CPP
#include "Bag.h"
Bag::Bag()
{
}
Bag::~Bag()
{
}
Scene* Bag::bagScene()
{
auto scene = Scene::create();
auto layer = Bag::create();
scene->addChild(layer);
return scene;
}
bool Bag::init()
{
if (!Layer::init())
{
return false;
}
Size size = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Bag_sprite = Sprite::create("BagCrete/Bag_Image.png");
Bag_sprite->setPosition(Vec2(size.width / 2,size.height / 2));
this->addChild(Bag_sprite);
TableView* tableView = TableView::create(this,Size(440,416));
// tableView = TableView::create(this,Size(100,100));
tableView->setDirection(ScrollView::Direction::VERTICAL);
tableView->setPosition(Vec2( 296,150));
tableView->setDelegate(this);
tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
this->addChild(tableView);
tableView->reloadData();
return true;
}
void Bag::tableCellTouched(cocos2d::extension::TableView* table,cocos2d::extension::TableViewCell* cell)
{
CCLOG("cell touched at index: %ld",cell->getIdx());
}
cocos2d::Size Bag::tableCellSizeForIndex(cocos2d::extension::TableView *table,ssize_t idx)
{
return Size(300,130);
}
TableViewCell* Bag::tableCellAtIndex(cocos2d::extension::TableView *table,ssize_t idx)
{
Size size = Director::getInstance()->getVisibleSize();
TableViewCell *cell = table->dequeueCell();
if (!cell)
{
cell = new (std::nothrow)TableViewCell();
cell->autorelease();
for (int i = 0; i < 4; i++)
{
auto image_sprite = Sprite::create("BagCrete/icon.png");
image_sprite->setPosition(Vec2(10 * (10 * i + 6),45));
cell->addChild(image_sprite);
// = Button::create("","");
}
}
return cell;
}
//设置个数;
ssize_t Bag::numberOfCellsInTableView(cocos2d::extension::TableView *table)
{
return 30;
}
//
void Bag::BagtouchEvent(Ref *pSender,Widget::TouchEventType type)
{
switch (type)
{
case cocos2d::ui::Widget::TouchEventType::BEGAN:
break;
case cocos2d::ui::Widget::TouchEventType::MOVED:
break;
case cocos2d::ui::Widget::TouchEventType::ENDED:
break;
case cocos2d::ui::Widget::TouchEventType::CANCELED:
break;
default:
break;
}
}
待续 还没写完下次继续