在cocos2d-x中需要经常建立图层,下面是建立一个图层的模板。
包括触摸事件,帧的更新。
以及单独的函数创建Sprite之类的,还可以单独创建practice.
#ifndef VICTORIANRUSHHOURLAYER_H
#define VICTORIANRUSHHOURLAYER_H
#include "cocos2d.h"
USING_NS_CC;
class VictorianRushHourLayer : public Layer {
enum {
kBackground,
kMiddleground,
kForeground
};
public:
VictorianRushHourLayer();
virtual ~VictorianRushHourLayer();
virtual bool init();
static Scene *createScene();
CREATE_FUNC(VictorianRushHourLayer);
virtual void update(float delta);
virtual bool onTouchBegan (Touch *touch, Event *unused_event);
private:
Size _screenSize = Director::getInstance()->getWinSize();
SpriteBatchNode *_gameBatchNode;
void createGameScene();
};
#endif // VICTORIANRUSHHOURLAYER_H
#include "VictorianRushHourLayer.h"
#include "cocos2d.h"
USING_NS_CC;
Scene *VictorianRushHourLayer::createScene()
{
Scene *scene = new Scene();
Layer *layer = VictorianRushHourLayer::create();
scene->addChild(layer);
return scene;
}
void VictorianRushHourLayer::update(float delta)
{
}
bool VictorianRushHourLayer::onTouchBegan(Touch *touch, Event *unused_event)
{
return false;
}
void VictorianRushHourLayer::createGameScene()
{
auto quickSprite = Sprite::create("blank.png");
quickSprite->setTextureRect(Rect(0, 0, 100, 100));
quickSprite->setColor(Color3B(255, 255, 255));
quickSprite->setPosition(Vec2(_screenSize.width * 0.5, _screenSize.height * 0.5));
_gameBatchNode = SpriteBatchNode::create("blank.png", 200);
this->addChild(_gameBatchNode, kMiddleground);
_gameBatchNode->addChild(quickSprite);
}
bool VictorianRushHourLayer::init()
{
if (!Layer::init()) return false;
createGameScene();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(VictorianRushHourLayer::onTouchBegan, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
this->schedule(schedule_selector(VictorianRushHourLayer::update));
return true;
}
VictorianRushHourLayer::~VictorianRushHourLayer()
{
}
VictorianRushHourLayer::VictorianRushHourLayer()
{
}