Instantly share code, notes, and snippets.
Created
March 24, 2014 06:09
-
Star
0
(0)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save lzubiaur/9734997 to your computer and use it in GitHub Desktop.
Custom cocos2dx menu item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2012-2014 Laurent Zubiaur - pix2d.com | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
#include "custommenusprite.h" | |
#include "SimpleAudioEngine.h" | |
using namespace CocosDenshion; | |
#define SCALE_ACTION_DURATION .4f | |
#define ACTIVATION_DELAY .1f | |
#define SCALE_EFFECT_FACTOR 0.8f | |
CustomMenuSprite::CustomMenuSprite() | |
: CCMenuItemSprite() | |
, mOldScaleX(.0f) | |
, mOldScaleY(.0f) | |
{} | |
CustomMenuSprite * CustomMenuSprite::create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite) | |
{ | |
return CustomMenuSprite::create(normalSprite, selectedSprite, disabledSprite, NULL, NULL); | |
} | |
CustomMenuSprite * CustomMenuSprite::create(CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector) | |
{ | |
return CustomMenuSprite::create(normalSprite, selectedSprite, NULL, target, selector); | |
} | |
CustomMenuSprite * CustomMenuSprite::create(CCNode *normalSprite, CCNode *selectedSprite, CCNode *disabledSprite, CCObject *target, SEL_MenuHandler selector) | |
{ | |
CustomMenuSprite *pRet = new CustomMenuSprite(); | |
pRet->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector); | |
pRet->autorelease(); | |
return pRet; | |
} | |
void CustomMenuSprite::onEnter() | |
{ | |
CCMenuItemSprite::onEnter(); | |
mOldScaleX = getScaleX(); | |
mOldScaleY = getScaleY(); | |
} | |
void CustomMenuSprite::onExit() | |
{ | |
stopAllActions(); | |
CCMenuItemSprite::onExit(); | |
} | |
void CustomMenuSprite::activate() | |
{ | |
/// Activation is postponed so the scale action is not stopped when using Cocosbuilder actions with this custom button. | |
/// CCMenuItemImage::activate(); | |
scheduleOnce(schedule_selector(CustomMenuSprite::postponedActivate), ACTIVATION_DELAY); | |
} | |
void CustomMenuSprite::postponedActivate(float dt) | |
{ | |
CCMenuItemSprite::activate(); | |
/// Plays the sound effect after activated | |
if (mSoundEffect.length()) | |
SimpleAudioEngine::sharedEngine()->playEffect(mSoundEffect.c_str(),false); | |
} | |
void CustomMenuSprite::selected() | |
{ | |
CCMenuItemSprite::selected(); | |
runAction(CCEaseElasticOut::create(CCScaleTo::create(SCALE_ACTION_DURATION, mOldScaleX * SCALE_EFFECT_FACTOR, mOldScaleY* SCALE_EFFECT_FACTOR))); | |
} | |
void CustomMenuSprite::unselected() | |
{ | |
CCMenuItemSprite::unselected(); | |
runAction(CCEaseElasticOut::create(CCScaleTo::create(SCALE_ACTION_DURATION, mOldScaleX, mOldScaleY))); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2012-2014 Laurent Zubiaur - pix2d.com | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
#ifndef CUSTOMMENUSPRITE_H | |
#define CUSTOMMENUSPRITE_H | |
#include <cocos2d.h> | |
USING_NS_CC; | |
class CustomMenuSprite : public CCMenuItemSprite | |
{ | |
public: | |
CustomMenuSprite(); | |
virtual ~CustomMenuSprite() {} | |
static CustomMenuSprite * create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite = NULL); | |
static CustomMenuSprite * create(CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector); | |
static CustomMenuSprite * create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector); | |
/// Play a sound effect when the item is activated | |
void setSoundEffect(const char *filename) { mSoundEffect = filename; } | |
/// Override CCNode methods | |
virtual void onEnter(); | |
virtual void onExit(); | |
/// Override CCMenuItem methods | |
virtual void selected(); /// Get focus | |
virtual void unselected(); /// Lost focus | |
/// Override activate() to get noticed when the button is clicked | |
virtual void activate(); | |
virtual void postponedActivate(float dt); | |
protected: | |
float mOldScaleX, mOldScaleY; | |
std::string mSoundEffect; | |
}; | |
#endif /* CUSTOMMENUSPRITE_H */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi ! I'm a student ! i found your code here. I tried to use it in my game. And all i could do is Selected() and unSelected to make it scale. I have no idea how to use onEnter() , onExit(), activate() and postponedActivate() in the right way and what is SEL_MenuHandler selector in the function create(). I tried so much on google but hopeless, so i come back here, hope you can give me a little exemple. I will be so grateful.
