From 6c01009a182247f624d5c5cf166db4be0ed73cc8 Mon Sep 17 00:00:00 2001 From: James Chen Date: Wed, 29 Feb 2012 10:07:18 +0800 Subject: [PATCH] fixed #986: Add init() member funciton of CCMenu class. --- cocos2dx/include/CCMenu.h | 15 ++++--- cocos2dx/menu_nodes/CCMenu.cpp | 73 +++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/cocos2dx/include/CCMenu.h b/cocos2dx/include/CCMenu.h index 0979ce9973..70a9228f46 100755 --- a/cocos2dx/include/CCMenu.h +++ b/cocos2dx/include/CCMenu.h @@ -60,17 +60,20 @@ namespace cocos2d{ {} virtual ~CCMenu(){} - /** creates an empty CCMenu */ - static CCMenu* node(); + /** creates an empty CCMenu */ + static CCMenu* node(); - /** creates a CCMenu with it's items */ - static CCMenu* menuWithItems(CCMenuItem* item, ...); + /** creates a CCMenu with it's items */ + static CCMenu* menuWithItems(CCMenuItem* item, ...); /** creates a CCMenu with it's item, then use addChild() to add * other items. It is used for script, it can't init with undetermined * number of variables. */ - static CCMenu*menuWithItem(CCMenuItem* item); + static CCMenu* menuWithItem(CCMenuItem* item); + + /** initializes an empty CCMenu */ + bool init(); /** initializes a CCMenu with it's items */ bool initWithItems(CCMenuItem* item, va_list args); @@ -122,7 +125,7 @@ namespace cocos2d{ protected: CCMenuItem* itemForTouch(CCTouch * touch); tCCMenuState m_eState; - CCMenuItem *m_pSelectedItem; + CCMenuItem *m_pSelectedItem; }; } diff --git a/cocos2dx/menu_nodes/CCMenu.cpp b/cocos2dx/menu_nodes/CCMenu.cpp index 3e2309ec48..40d17bdb69 100644 --- a/cocos2dx/menu_nodes/CCMenu.cpp +++ b/cocos2dx/menu_nodes/CCMenu.cpp @@ -72,35 +72,46 @@ namespace cocos2d{ return menuWithItems(item, NULL); } + bool CCMenu::init() + { + if (CCLayer::init()) + { + this->m_bIsTouchEnabled = true; + + // menu in the center of the screen + CCSize s = CCDirector::sharedDirector()->getWinSize(); + + this->m_bIsRelativeAnchorPoint = false; + setAnchorPoint(ccp(0.5f, 0.5f)); + this->setContentSize(s); + + // XXX: in v0.7, winSize should return the visible size + // XXX: so the bar calculation should be done there + CCRect r; + CCApplication::sharedApplication().statusBarFrame(&r); + ccDeviceOrientation orientation = CCDirector::sharedDirector()->getDeviceOrientation(); + if (orientation == CCDeviceOrientationLandscapeLeft || orientation == CCDeviceOrientationLandscapeRight) + { + s.height -= r.size.width; + } + else + { + s.height -= r.size.height; + } + setPosition(ccp(s.width/2, s.height/2)); + // [self alignItemsVertically]; + m_pSelectedItem = NULL; + m_eState = kCCMenuStateWaiting; + return true; + } + return false; + } + bool CCMenu::initWithItems(CCMenuItem* item, va_list args) { - if (CCLayer::init()) - { - this->m_bIsTouchEnabled = true; - - // menu in the center of the screen - CCSize s = CCDirector::sharedDirector()->getWinSize(); - - this->m_bIsRelativeAnchorPoint = false; - setAnchorPoint(ccp(0.5f, 0.5f)); - this->setContentSize(s); - - // XXX: in v0.7, winSize should return the visible size - // XXX: so the bar calculation should be done there - CCRect r; - CCApplication::sharedApplication().statusBarFrame(&r); - ccDeviceOrientation orientation = CCDirector::sharedDirector()->getDeviceOrientation(); - if (orientation == CCDeviceOrientationLandscapeLeft || orientation == CCDeviceOrientationLandscapeRight) - { - s.height -= r.size.width; - } - else - { - s.height -= r.size.height; - } - setPosition(ccp(s.width/2, s.height/2)); - - int z=0; + if (init()) + { + int z=0; if (item) { @@ -113,14 +124,10 @@ namespace cocos2d{ i = va_arg(args, CCMenuItem*); } } - // [self alignItemsVertically]; - m_pSelectedItem = NULL; - m_eState = kCCMenuStateWaiting; return true; - } - - return false; + } + return false; } /*