您的位置:首页 > 理论基础 > 计算机网络

仿TestCpp实现Layout页面切换效果

2014-09-13 15:55 330 查看
//HelloWorld.h
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "First.h"
#include "Second.h"
#include "Third.h"
USING_NS_CC;
using namespace ui;

enum Tag
{
FIRST = 1,
SECOND,
THIRD
};

class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
Size winSize;
First* first;
Second* second;
Third* third;
void createLayout(int tag);
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
void touchEvent(Ref *pSender, cocos2d::ui::Widget::TouchEventType type);
//    void touchEvent(Ref* pSender,cocos2d::ui::Widget::TouchEventType type);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};


//HelloWorld.cpp
#include "HelloWorldScene.h"
#define MAX_LAYER 3;
USING_NS_CC;

Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto layer = HelloWorld::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;
}
static int sceneIdx = -1;
Layout* createActionManagerLayer(int nIndex)
{
log("sceneIdx = %d",nIndex);
switch(nIndex)
{
case 0: return First::create();
case 1: return Second::create();
case 2: return Third::create();
}

return NULL;
}

Layout* nextActionManagerAction();
Layout* backActionManagerAction();
Layout* restartActionManagerAction();

Layout* nextActionManagerAction()
{
sceneIdx++;
sceneIdx = sceneIdx % MAX_LAYER;

auto layer = createActionManagerLayer(sceneIdx);
// layer->autorelease();

return layer;
}

Layout* backActionManagerAction()
{
sceneIdx--;
int total = MAX_LAYER;
if( sceneIdx < 0 )
sceneIdx += total;

auto layer = createActionManagerLayer(sceneIdx);
// layer->autorelease();

return layer;
}

Layout* restartActionManagerAction()
{
auto layer = createActionManagerLayer(sceneIdx);
// layer->autorelease();

return layer;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.

// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));

// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
winSize = Director::getInstance()->getWinSize();

auto layout = Layout::create();
layout->setSize(Size(winSize.width,winSize.height/4));
layout->setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
layout->setBackGroundColor(Color3B::WHITE);
layout->ignoreAnchorPointForPosition(false);
layout->setAnchorPoint(Vec2::ANCHOR_MIDDLE_TOP);
layout->setPosition(Vec2(winSize.width/2,winSize.height));
addChild(layout);

auto image1 = ui::ImageView::create("b2.png");
image1->setTag(FIRST);
image1->setPosition(Vec2(layout->getSize().width/4,layout->getSize().height/2));
image1->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchEvent, this));
layout->addChild(image1);

auto image2 = ui::ImageView::create("r2.png");
image2->setTag(SECOND);
image2->setPosition(Vec2(layout->getSize().width/2,layout->getSize().height/2));
image2->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchEvent, this));
layout->addChild(image2);

auto image3 = ui::ImageView::create("f2.png");
image3->setTag(THIRD);
image3->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchEvent, this));
image3->setPosition(Vec2(layout->getSize().width/4*3,layout->getSize().height/2));
layout->addChild(image3);

auto first = First::create();
first->setTag(10);
first->setPosition(Vec2(winSize.width/2,winSize.width/4));
addChild(first);

return true;
}

void HelloWorld::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
auto tag = static_cast<ui::ImageView*>(pSender)->getTag();
log("tag=%d",tag);
auto child = getChildByTag(10);
if(child != nullptr)
{
removeChildByTag(10);
}
switch(tag)
{
case FIRST:
{
log("First");
auto temp = backActionManagerAction();
temp->setTag(10);
temp->setPosition(Vec2(winSize.width/2,winSize.width/4));
addChild(temp);
break;
}
case SECOND:
{
log("Second");
auto temp = restartActionManagerAction();
temp->setPosition(Vec2(winSize.width/2,winSize.width/4));
temp->setTag(10);
addChild(temp);
break;
}
case THIRD:
{
log("Third");
auto temp = nextActionManagerAction();
temp->setPosition(Vec2(winSize.width/2,winSize.width/4));
temp->setTag(10);
addChild(temp);
break;
}
default:
{

break;
}
}
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif

Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
//First.h
#include "cocos2d.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace ui;

class First : public Layout
{
public:
CREATE_FUNC(First);
virtual bool init();
void touchEvent(Ref* pSender,cocos2d::ui::Widget::TouchEventType type);
};

//First.cpp
#include "First.h"
bool First::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!Layout::init());

auto winSize = Director::getInstance()->getWinSize();
setSize(Size(winSize.width/2,winSize.height/4));
setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
setBackGroundColor(Color3B::GREEN);
ignoreAnchorPointForPosition(false);
setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
auto text = Text::create("1","",30);
text->setColor(Color3B::WHITE);
text->setPosition(Vec2(getSize().width/2,getSize().height/2));
addChild(text);

bRet = true;
}while(0);
return bRet;
}

void First::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
auto temp =  static_cast<ui::ImageView*>(pSender);
log("tag = %d",temp->getTag());
}

//Second.h
#include "cocos2d.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace ui;

class Second : public Layout
{
public:
CREATE_FUNC(Second);
virtual bool init();
void touchEvent(Ref* pSender,cocos2d::ui::Widget::TouchEventType type);
};

//Second.cpp
#include "Second.h"
bool Second::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!Layout::init());

auto winSize = Director::getInstance()->getWinSize();
setSize(Size(winSize.width/2,winSize.height/4));
setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
setBackGroundColor(Color3B::GREEN);
ignoreAnchorPointForPosition(false);
setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
auto text = Text::create("2","",30);
text->setColor(Color3B::WHITE);
text->setPosition(Vec2(getSize().width/2,getSize().height/2));
addChild(text);

bRet = true;
}while(0);
return bRet;
}

void Second::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
auto temp =  static_cast<ui::ImageView*>(pSender);
log("tag = %d",temp->getTag());
}

//Third.h
#include "cocos2d.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace ui;

class Third : public Layout
{
public:
CREATE_FUNC(Third);
virtual bool init();
void touchEvent(Ref* pSender,cocos2d::ui::Widget::TouchEventType type);
};

//Third.cpp
#include "Third.h"
bool Third::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!Layout::init());
auto winSize = Director::getInstance()->getWinSize();
setBackGroundColorType(cocos2d::ui::Layout::BackGroundColorType::SOLID);
setBackGroundColor(Color3B::GREEN);
setSize(Size(winSize.width/2,winSize.height/4));
ignoreAnchorPointForPosition(false);
setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
auto text = Text::create("3","",30);
text->setColor(Color3B::WHITE);
text->setPosition(Vec2(getSize().width/2,getSize().height/2));
addChild(text);

bRet = true;
}while(0);
return bRet;
}

void Third::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
auto temp =  static_cast<ui::ImageView*>(pSender);
log("tag = %d",temp->getTag());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息