The menu buttons with cooling are perfectly encapsulated.
The following two images are used: the blue button, and the gray one is the cooling mask effect.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0644245136-0.png "style =" float: none; "title =" tool3_ B .png "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0644246492-1.png "title =" tool3.png "style =" float: none; "/>
The button has three statuses: Not clicked, cool, not clicked. See the figure below:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0644246102-2.png "style =" float: none; "title =" 0AD82048-7E27-4437-B904-1518EC1EA21F.png "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0644245326-3.png "title =" A1B903C0-8E76-4FC0-AFA1-FDF8F4A7172B.png "style =" float: none; "/> 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0644243395-4.png "title =" D5F8698D-E426-4A4C-8E91-589810C7A137.png "style =" float: none; "/>
Of course, numbers are not counted, and images are ugly. Okay, very good. I hate being unprofessional.
What we need to do is inherit the CCMenuItemSprite provided by cocos2d and add the effect to it.
First, we need to know how to use CCMenuItemSprite:
// Texture cache CCTextureCache * textureCache = [CCTextureCache sharedTextureCache]; // unselected status CCSprite * normal = [CCSprite cached: [textureCache textureForKey: @ "tool.png"]; // select the CCSprite * selected = [CCSprite spriteWithTexture: [textureCache textureForKey: @ "tool.png"]; // The unselectable status can be blank.) CCSprite * disabled = [CCSprite spriteWithTexture: [textureCache textureForKey: @ "tool.png"]; // genie menu item shortcut * tool = [CCMenuItemSprite itemWithNormalSprite: normal selectedSprite: selected disabledSprite: disabled target: self selector: @ selector (toolCallback)]; // menu add menu item CCMenu * menu = [CCMenu menuWithItems: tool, nil]; // The menu coordinate is the origin so that the menu item can be set according to the screen Coordinate Position [menu setPosition: CGPointZero]; [self addChild: menu];
Why don't you load images directly? Doesn't You Think That loading images into the cache at the beginning of the game will make the game smoother? This is why we use CCMenuItemSprite. In addition, the callback function set in the above Code to respond to the button:
-(Void) toolCallback {// Button Function}
The above is the use of CCMenuItemSprite, of course, our encapsulation should also be used like this. There is a blog post on the Internet that writes the callback function of the response button in the button class for button encapsulation, which makes the encapsulation meaningless. Add a button and write another class? Not professional.
First Declaration:
# Import <Foundation/Foundation. h> # import "cocos2d. h "@ interface MYMenuItemSprite: CCMenuItemSprite {// masked sprite CCSprite * _ becloundsSprite; // cool-down progress bar CCProgressTimer * _ progressSprite; // cool-down time ccTime _ time; // whether the image is being cooled BOOL isCool;} // response Button Function + (id) initMenuItemSprite (CCSprite *) in the current layer of the cool-down time of the masked Image *) normalSprite becloudsSprite :( CCSprite *) becloundsSprite duration :( ccTime) time target :( id) target selector :( SEL) selector;-(id) specify :( CCSprite *) normalSprite becloudsSprite :( CCSprite *) becloundsSprite duration :( ccTime) time target :( id) target selector :( SEL) selector; // cool-down effect-(void) coolingEffect; // After cool-down,-(void) progressCallback; // whether the button is available and the parent class is just a bit like)-(void) setEnabled :( BOOL) is; @ end
Our own cool-down button is also a button that inherits from CCMenuItemSprite and expands its functions.
The implementation section is as follows:
# Import "MYMenuItemSprite. h "@ implementation MYMenuItemSprite + (id) initMenuItemSprite :( CCSprite *) normalSprite becloudsSprite :( CCSprite *) becloundsSprite duration :( ccTime) time target :( id) target selector :( SEL) selector {// return an object that can be automatically released. return [[[self alloc] initMenuItemSprite: normalSprite becloudsSprite: becloundsSprite duration: time target: target selector: selector] autorel.pdf];} // self-built structure-(id) initMenuIt EmSprite :( CCSprite *) normalSprite becloudsSprite :( CCSprite *) becloundsSprite duration :( ccTime) time target :( id) target selector :( SEL) selector {// you have not selected CCSprite * sprite = [CCSprite spriteWithTexture: normalSprite. texture]; // structure of the parent class self = [super initWithNormalSprite: normalSprite selectedSprite: sprite disabledSprite: nil target: target selector: selector]; // new structure if (self) {// masked sprite _ becloundsSprite = [CCSprite spriteWithTexture: becloundsSprite. texture]; _ becloundsSprite. visible = NO; // The layer that contains the menu, including the masked Sprite. On top of the menu item, [target addChild: _ becloundsSprite z: 2]; // cool-down genie, that is, the button image CCSprite * sprite2 = [CCSprite spriteWithTexture: normalSprite. texture]; _ progressSprite = [CCProgressTimer progressWithSprite: sprite2]; // layer containing the menu, containing the cool-down genie, on top of the mask [target addChild: _ progressSprite z: 3]; // cool-down time _ time = time;} return self ;}-(Void) dealloc {[super dealloc];} // Add the template Genie's location to the parent class method-(void) setPosition :( CGPoint) position {[super setPosition: position]; _ becloundsSprite. position = position; [_ progressSprite setPosition: position];} // callback function after the parent class button is clicked-(void) selected {[super selected];} // callback function of the exit button of the parent class-(void) unselected {[super unselected]; // click the button to play the cool-down effect [self coolingEffect];} // callback function of the parent class-(void) activate {// execute the callback function [super activate] of the corresponding button; // After the callback function is executed, the button cannot be in the self state. isEnabled = NO;} // cool-down effect-(void) coolingEffect {// cool-down effect CCActionInterval * action = [CCProgressTo actionWithDuration: _ time percent: 100]; CCCallFunc * callFunc = [CCCallFunc actionWithTarget: self selector: @ selector (progressCallback)]; CCSequence * seq = [CCSequence actions: action, callFunc, nil]; [_ progressSprite runAction: seq]; // mask visible _ becloundsSprite. visible = YES; // isCool in cool state = YES;}-(void) progressCallback {if (isCool) {isCool = NO;} else {// if the cool-down state has been terminated before, return directly ;} // cool-down activation button self. isEnabled = YES; // The mask is invisible _ becloundsSprite. visible = NO;} // set whether the current button is available-(void) setEnabled :( BOOL) is {// unavailable when cooling, set to unavailable when cooling, if (is) {if (! IsCool) {self. isEnabled = YES; _ progressSprite. visible = YES; _ becloundsSprite. visible = NO ;}} else {isCool = NO; self. isEnabled = NO; _ progressSprite. visible = NO; _ becloundsSprite. visible = YES; }}@ end
Note that the callback function object we pass in is target: it can only be the CCNode of the currently added menu, because it needs to draw our masked image. Of course, we also write this in normal times. It is only allowed here. If you want to pass a non-CCNode object, you can modify it by yourself, add a parameter, and pass the CCNode of the add menu. For example, add scene :( CCNode *) scene at the end of the constructor.
Finally, we use our own cooling button items:
{CCTextureCache * textureCache = [CCTextureCache sharedTextureCache]; // masked image CCSprite * tool_ B = [CCSprite encoding: [textureCache textureForKey: @ "tool_ B .png"]; CCSprite * normal = [CCSprite attributes: [textureCache textureForKey: @ "tool.png"]; // your own cooling menu button item MYMenuItemSprite * tool = [MYMenuItemSprite attributes: normal becloudsSprite: tool_ B duration: 1.1 target: self selector: @ selector (toolMenuCallback)]; // set the position of the cool-down menu item [tool setPosition: ccp (0, 0)]; menu = [CCMenu menuWithItems: tool, nil]; [menu setPosition: CGPointZero]; [self addChild: menu z: 1];}-(void) toolMenuCallback {// Button Function}
Is it very exciting to use the same tool as CCMenuItemSprite. The perfect encapsulation ends here. You can modify and add content as needed.
This article is from the "taodym" blog, please be sure to keep this source http://taodym.blog.51cto.com/3032223/1220239