0

如何使用通常的菜单查找将我的菜单 Sprites 设置在屏幕中央

CCSize 大小 = CCDirector::sharedDirector()->getWinSize(); 精灵->setPosition(ccp(size.width, size.height));

请帮我解决(ccp(?,?))

Sprites:开始、选项、退出游戏菜单

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

USING_NS_CC;
using namespace cocos2d;

CCSprite *car;

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

return scene;
}


// on "init" you need to initialize your instance
bool MenuScene::init()
{

// 1. super init first
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 a "close" icon to exit the progress. it's an autorelease object
//    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
//                                        "CloseNormal.png",
//                                        "CloseSelected.png",
//                                        this,
//    menu_selector(MenuScene::menuCloseCallback));
//
//  pCloseItem->setPosition(ccp(origin.x + visibleSize.width -     
pCloseItem->getContentSize().width ,
//                                origin.y +  
pCloseItem->getContentSize().height));

// create menu, it's an autorelease object
//    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
//    pMenu->setPosition(CCPointZero);
//    this->addChild(pMenu, 1);


// 3. 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);

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("sprite.png",     
"sprite.png",this,menu_selector(MenuScene::menuCloseCallback));

startItem->setPosition(ccp(590, 450));
float aX = size.width / startItem->getContentSize().width;
float aY = size.height / startItem->getContentSize().height;
//  startItem->setScaleX(aX);
//  startItem->setScaleY(aY);

CCMenuItemImage *extraItem = CCMenuItemImage::create("sprite2.png", 
"sprite2.png",this,menu_selector(MenuScene::menuCloseCallback));

extraItem->setPosition(ccp(590,350));
//float bX = size.width / extraItem->getContentSize().width;
//float bY = size.height / extraItem->getContentSize().height;
//extraItem->setScaleX(bX);
//extraItem->setScaleY(bY);

CCMenuItemImage *optionsItem = CCMenuItemImage::create("options.png",
"options.png",this,menu_selector(MenuScene::menuCloseCallback));

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

CCMenuItemImage *quitItem = CCMenuItemImage::create("quit.png",
"quit.png",this,menu_selector(MenuScene::menuCloseCallback));

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

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

}

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

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

void MenuScene::ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event){

}

void MenuScene::ccTouchesCancelled(cocos2d::CCSet* touches, cocos2d::CCEvent* 
event){

}


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

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


 /* namespace cocos2d */
4

2 回答 2

0

如果您希望将菜单定位在屏幕的中心,您必须在 ccl 中提供中心坐标

   sprite ->setPosition(ccp(size.width/2, size.height/2));
于 2013-10-03T04:27:08.513 回答
0

首先你得到窗口大小......

CGSize Window_size=[[CCDirector sharedDirector]winSize];

现在在 CCMenu 中设置 CCMenuItem 的位置...

CCMenuItem *Minus_Menu_item = [CCMenuItemImage itemWithNormalImage:@"ButtonMinus.png" selectedImage:@"ButtonMinus.png" disabledImage:@"ButtonNum1.png" target:self selector:@selector(Minus_Button:)];
Minus_Menu_item.position=ccp(180, 200);
Menu_Minus=[CCMenu menuWithItems:Minus_Menu_item, nil];
Menu_Minus.position = CGPointZero;
[self addChild:Menu_Minus];
于 2013-10-03T04:52:31.713 回答