如何从屏幕设置位置菜单?

时间:2013-10-03 03:00:50

标签: menu cocos2d-iphone position

如何使用常用的菜单

在屏幕中央设置我的菜单Sprite

CCSize size = CCDirector :: sharedDirector() - > getWinSize(); sprite - > 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 */

2 个答案:

答案 0 :(得分:0)

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

   sprite ->setPosition(ccp(size.width/2, size.height/2));

答案 1 :(得分: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];