0

我很难将每个精灵设置为可见(假),只是为了从我的菜单中显示其他图层。有人可以帮我进行图层转换吗?希望有完整代码的答案。

这是我的菜单类

#include "MenuScene.h"
#include "LoadingScreen.h"
#include "cocos2d.h"

USING_NS_CC;
using namespace cocos2d;

CCSprite *car;
CCSprite *gameTitle;
CCSprite *touchBegin;
CCSprite *copyright;
CCSprite *version;
CCSprite *about;
CCSprite *optionTitle;
CCSprite *mBar;
CCMenuItemImage *back;

CCMenu* pMenu;
CCMenu* oMenu;


CCScene* MenuScene::scene()
{
CCScene *scene = CCScene::create();
MenuScene *layer = MenuScene::create();
scene->addChild(layer);

return scene;
}

bool MenuScene::init()
{

if ( !CCLayer::init() )
{
    return false;
}
this->setTouchEnabled(true);
//this->schedule( schedule_selector(MenuScene::update) );

CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();


// add sprite below...
CCSprite *menuLayout = CCSprite::create("sky.png");
menuLayout->setPosition(ccp(size.width/2, size.height/2));
this->addChild(menuLayout, -1);
float aX = size.width / menuLayout->getContentSize().width;
float aY = size.height / menuLayout->getContentSize().height;
menuLayout->setScaleX(aX);
menuLayout->setScaleY(aY);

CCSprite *beam2 = CCSprite::create("beam2.png");
beam2->setPosition(ccp(size.width/2, size.height/2));
this->addChild(beam2, -1);
float bX = size.width / beam2->getContentSize().width;
float bY = size.height / beam2->getContentSize().height;
beam2->setScaleX(bX);
beam2->setScaleY(bY);

CCSprite *beam = CCSprite::create("beam.png");
beam->setPosition(ccp(size.width/2, size.height/2));
this->addChild(beam, -1);
float cX = size.width / beam->getContentSize().width;
float cY = size.height / beam->getContentSize().height;
beam->setScaleX(bX);
beam->setScaleY(bY);

CCSprite *buildings = CCSprite::create("buildings.png");
buildings ->setPosition(ccp(size.width / 2, size.height / 2));
this->addChild(buildings, -1);
float dX = size.width / buildings->getContentSize().width;
float dY = size.height / buildings->getContentSize().height;
buildings->setScaleX(dX);
buildings->setScaleY(dY);

car = CCSprite::create("limousine.png");
car ->setPosition(ccp(size.width/3.5, size.height/3.5));
this->addChild(car, -1);
float eX = size.width / car->getContentSize().width;
float eY = size.height / car->getContentSize().height;
car->setScaleX(aX);
car->setScaleY(aY);

CCSprite *crowd = CCSprite::create("crowd.png");
crowd ->setPosition(ccp(size.width / 2, size.height / 2));
this->addChild(crowd, -1);
float fX = size.width / crowd->getContentSize().width;
float fY = size.height / crowd->getContentSize().height;
crowd->setScaleX(fX);
crowd->setScaleY(fY);

gameTitle = CCSprite::create("titlescreen_title.png");
gameTitle ->setPosition(ccp(size.width / 2, size.height / 3));
this->addChild(gameTitle, -1);
float gX = size.width / gameTitle->getContentSize().width;
float gY = size.height / gameTitle->getContentSize().height;
gameTitle->setScaleX(gX);
gameTitle->setScaleY(gY);

touchBegin = CCSprite::create("touch_begin.png");
touchBegin ->setPosition(ccp(595,290));
this->addChild(touchBegin, -1);
float hX = size.width / touchBegin->getContentSize().width/2;
float hY = size.height / touchBegin->getContentSize().height/6;
touchBegin->setScaleX(hX);
touchBegin->setScaleY(hY);

copyright = CCSprite::create("copyright.png");
copyright ->setPosition(ccp(105,35));
this->addChild(copyright, -1);
float iX = size.width / copyright->getContentSize().width/7;
float iY = size.height / copyright->getContentSize().height/17;
copyright->setScaleX(iX);
copyright->setScaleY(iY);

version = CCSprite::create("version_number.png");
version ->setPosition(ccp(1125,35));
this->addChild(version, -1);
float jX = size.width / version->getContentSize().width/9;
float jY = size.height / version->getContentSize().height/19;
version->setScaleX(jX);
version->setScaleY(jY);

return true;

}


//callfuncN_selector(MainScene::spriteMoveFinished)
//backcalls the function spriteMoveFinished()
void MenuScene::spriteMoveFinished(CCNode* pSender)
{
CCSprite *sprite = (CCSprite *)pSender;
this->removeChild(sprite, true);
}


void MenuScene::Menus()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();

CCMenuItemImage *startItem = CCMenuItemImage::create(

"button_startgame.png",

"button_startgame.png",
                                                    this,

menu_selector(MenuScene::nextScene));

startItem->setPosition(ccp(595, 490));
float aX = size.width / startItem->getContentSize().width/2;
float aY = size.height / startItem->getContentSize().height/10;
startItem->setScaleX(aX);
startItem->setScaleY(aY);

about = CCSprite::create("button_about2.png");
about->setPosition(ccp(595,370));
this->addChild(about, -1);
float bX = size.width / about->getContentSize().width/2;
float bY = size.height / about->getContentSize().height/10;
about->setScaleX(bX);
about->setScaleY(bY);


CCMenuItemImage *optionsItem = CCMenuItemImage::create(
                                            "options.png",
                                            "options.png",
                                            this,

menu_selector(MenuScene::optionsScene));

optionsItem->setPosition(ccp(595, 250));
float cX = size.width / optionsItem->getContentSize().width/3;
 float cY = size.height / optionsItem->getContentSize().height/10;
optionsItem->setScaleX(cX);
optionsItem->setScaleY(cY);


CCMenuItemImage *quitItem = CCMenuItemImage::create(
                                        "button_quitgame1.png",
                                        "button_quitgame1.png",
                                        this,

menu_selector(MenuScene::menuCloseCallback));

quitItem->setPosition(ccp(590, 130));
float dX = size.width / quitItem->getContentSize().width/2.5;
float dY = size.height / quitItem->getContentSize().height/10;
quitItem->setScaleX(dX);
quitItem->setScaleY(dY);

pMenu = CCMenu::create(startItem, optionsItem, quitItem, NULL);
pMenu->setPosition(0,0);
this->addChild(pMenu, 0);
}


void MenuScene::ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event){
Menus();
gameTitle->removeFromParent();
touchBegin->removeFromParent();
copyright->removeFromParent();
version->removeFromParent();
}


void MenuScene::ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event){
Menus();
}

void MenuScene::ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event){
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

void MenuScene::ccTouchesCancelled(cocos2d::CCSet* touches, cocos2d::CCEvent* 
event){
//CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

void MenuScene::nextScene()
{

 CCDirector::sharedDirector()->replaceScene(CCTransitionFade:
                                     :create(3.0f,LoadingScreen::scene()    ));
}


void MenuScene::optionsScene()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();

pMenu->setVisible(false);
about->setVisible(false);

optionTitle = CCSprite::create("options.png");
optionTitle->setPosition(ccp(595,525));
this->addChild(optionTitle, -1);
float aX = size.width / optionTitle->getContentSize().width/2;
float aY = size.height / optionTitle->getContentSize().height/8;
optionTitle->setScaleX(aX);
optionTitle->setScaleY(aY);

mBar = CCSprite::create("menu_bar.png");
mBar->setPosition(ccp(595,447));
this->addChild(mBar, -1);
 float bX = size.width / mBar->getContentSize().width/2;
float bY = size.height / mBar->getContentSize().height/16;
mBar->setScaleX(bX);
mBar->setScaleY(bY);

 back = CCMenuItemImage::create(
                                           "button_backmenu.png",
                                           "button_backmenu.png",
                                            this,

   menu_selector(MenuScene::backClicked));

back->setPosition(ccp(595,185));
this->addChild(back, -1);
float eX = size.width / back->getContentSize().width/2;
float eY = size.height / back->getContentSize().height/10;
 back->setScaleX(eX);
 back->setScaleY(eY);

oMenu = CCMenu::create(back, NULL);
oMenu->setPosition(0,0);
this->addChild(oMenu, -1);

}


void MenuScene::backClicked()
{
Menus();
optionTitle->removeFromParent();
mBar->removeFromParent();
back->removeFromParent();
}


void MenuScene::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}


/* namespace cocos2d */
4

0 回答 0