CCButton Class Reference
Inherits from | CCControl : CCNode : CCResponder : NSObject |
---|---|
Declared in | CCButton.h |
Overview
A button represents an area of the screen that reacts to touches/clicks. The button creates a CCSprite9Slice node for its background image and a CCLabelTTF for its text.
The button is presented with a stretchable background image and/or a title label. Different images, colors and opacity can be set for each of the button’s different states.
Methods for setting callbacks for the button are inherited from CCControl, see [CCControl setTarget:selector:] and [CCControl setBlock:].
Note: It is worth noting that background and label are optional. If you hide both, you can have an invisible button that still reacts to touches.
Note: You should not add child nodes to a button, nor remove the existing background and label nodes.
Creating a Button
+ buttonWithTitle:
Creates a new button with a title and no background. Uses default font and font size.
+ (id)buttonWithTitle:(NSString *)title
Parameters
title |
The title text of the button. |
---|
Return Value
A new button.
Declared In
CCButton.h
+ buttonWithTitle:fontName:fontSize:
Creates a new button with a title and no background.
+ (id)buttonWithTitle:(NSString *)title fontName:(NSString *)fontName fontSize:(float)size
Parameters
title |
The title text of the button. |
---|---|
fontName |
|
size |
Return Value
A new button.
Declared In
CCButton.h
+ buttonWithTitle:spriteFrame:
Creates a new button with the specified title for the label and sprite frame for its background.
+ (id)buttonWithTitle:(NSString *)title spriteFrame:(CCSpriteFrame *)spriteFrame
Parameters
title |
The title text of the button. |
---|---|
spriteFrame |
Stretchable background image. |
Return Value
A new button.
See Also
Declared In
CCButton.h
+ buttonWithTitle:spriteFrame:highlightedSpriteFrame:disabledSpriteFrame:
Creates a new button with the speicified title for the label, sprite frames for its background in different states.
+ (id)buttonWithTitle:(NSString *)title spriteFrame:(CCSpriteFrame *)spriteFrame highlightedSpriteFrame:(CCSpriteFrame *)highlighted disabledSpriteFrame:(CCSpriteFrame *)disabled
Parameters
title |
The title text of the button. |
---|---|
spriteFrame |
Stretchable background image for the normal state. |
highlighted |
Stretchable background image for the highlighted state. |
disabled |
Stretchable background image for the disabled state. |
Return Value
A new button.
See Also
Declared In
CCButton.h
– initWithTitle:
Initializes a new button with a title and no background. Uses default font and font size.
- (id)initWithTitle:(NSString *)title
Parameters
title |
The title text of the button. |
---|
Return Value
A new button.
Declared In
CCButton.h
– initWithTitle:fontName:fontSize:
Initializes a new button with a title and no background.
- (id)initWithTitle:(NSString *)title fontName:(NSString *)fontName fontSize:(float)size
Parameters
title |
The title text of the button. |
---|---|
fontName |
|
size |
Return Value
A new button.
Declared In
CCButton.h
– initWithTitle:spriteFrame:
Initializes a new button with the specified title for the label and sprite frame for its background.
- (id)initWithTitle:(NSString *)title spriteFrame:(CCSpriteFrame *)spriteFrame
Parameters
title |
The title text of the button. |
---|---|
spriteFrame |
Stretchable background image. |
Return Value
A new button.
See Also
Declared In
CCButton.h
– initWithTitle:spriteFrame:highlightedSpriteFrame:disabledSpriteFrame:
Initializes a new button with the speicified title for the label, sprite frames for its background in different states.
- (id)initWithTitle:(NSString *)title spriteFrame:(CCSpriteFrame *)spriteFrame highlightedSpriteFrame:(CCSpriteFrame *)highlighted disabledSpriteFrame:(CCSpriteFrame *)disabled
Parameters
title |
The title text of the button. |
---|---|
spriteFrame |
Stretchable background image for the normal state. |
highlighted |
Stretchable background image for the highlighted state. |
disabled |
Stretchable background image for the disabled state. |
Return Value
A new button.
See Also
Declared In
CCButton.h
Button Child Nodes
background
The CCSprite9Slice instance used as the button’s background.
@property (nonatomic, readonly) CCSprite9Slice *background
See Also
Declared In
CCButton.h
label
The CCLabelTTFF instance used as the button’s text label.
@property (nonatomic, readonly) CCLabelTTF *label
Discussion
Warning: Setting the [CCLabelTTF setString:] directly will prevent the button from resizing if the label’s dimensions change. Instead use the title property to change the button’s label text.
See Also
Declared In
CCButton.h
Button Behavior
zoomWhenHighlighted
If YES, will slightly enlarge the button while the button is highlighted (mouse/finger “hovering” over button). Similar to what CCMenuItem did.
@property (nonatomic, assign) BOOL zoomWhenHighlighted
Declared In
CCButton.h
togglesSelectedState
If YES, changes the button to a toggle button that toggles between turned on (pressed) and off (normal) states every time it is tapped/clicked.
@property (nonatomic, assign) BOOL togglesSelectedState
Declared In
CCButton.h
Padding and Title
horizontalPadding
Adds horizontal padding to both sides of the label, increasing its width by 2x horizontalPadding.
@property (nonatomic, assign) float horizontalPadding
Declared In
CCButton.h
verticalPadding
Adds vertical padding to both sides of the label, increasing its height by 2x verticalPadding.
@property (nonatomic, assign) float verticalPadding
Declared In
CCButton.h
title
The button’s title. Use this to change the label.
@property (nonatomic, strong) NSString *title
Discussion
Warning: Setting the [CCLabelTTF setString:] directly will prevent the button from resizing if the label’s dimensions change.
Declared In
CCButton.h
Background Properties
– setBackgroundColor:forState:
Sets the background color for the specified state. The color is multiplied into the background sprite frame.
- (void)setBackgroundColor:(CCColor *)color forState:(CCControlState)state
Parameters
color |
Color applied to background image. |
---|---|
state |
State to apply the color to. |
See Also
Declared In
CCButton.h
– backgroundColorForState:
Gets the background color for the specified state.
- (CCColor *)backgroundColorForState:(CCControlState)state
Parameters
state |
State to get the color for. |
---|
Return Value
Background color.
See Also
Declared In
CCButton.h
– setBackgroundOpacity:forState:
Sets the background’s opacity for the specified state.
- (void)setBackgroundOpacity:(CGFloat)opacity forState:(CCControlState)state
Parameters
opacity |
Opacity to apply to the background image |
---|---|
state |
State to apply the opacity to. |
See Also
Declared In
CCButton.h
– backgroundOpacityForState:
Gets the background opacity for the specified state.
- (CGFloat)backgroundOpacityForState:(CCControlState)state
Parameters
state |
State to get the opacity for. |
---|
Return Value
Opacity.
See Also
Declared In
CCButton.h
– setBackgroundSpriteFrame:forState:
Sets the background’s sprite frame for the specified state. The sprite frame will be stretched to the preferred size of the label. If set to NULL
no background will be drawn.
- (void)setBackgroundSpriteFrame:(CCSpriteFrame *)spriteFrame forState:(CCControlState)state
Parameters
spriteFrame |
Sprite frame to use for drawing the background. |
---|---|
state |
State to set the background for. |
See Also
Declared In
CCButton.h
– backgroundSpriteFrameForState:
Gets the background’s sprite frame for the specified state.
- (CCSpriteFrame *)backgroundSpriteFrameForState:(CCControlState)state
Parameters
state |
State to get the sprite frame for. |
---|
Return Value
Background sprite frame.
See Also
Declared In
CCButton.h
Label Properties
– setLabelColor:forState:
Sets the label’s color for the specified state.
- (void)setLabelColor:(CCColor *)color forState:(CCControlState)state
Parameters
color |
Color applied to the label. |
---|---|
state |
State to set the color for. |
See Also
Declared In
CCButton.h
– labelColorForState:
Gets the label’s color for the specified state.
- (CCColor *)labelColorForState:(CCControlState)state
Parameters
state |
State to get the color for. |
---|
Return Value
Label color.
See Also
Declared In
CCButton.h
– setLabelOpacity:forState:
Sets the label’s opacity for the specified state.
- (void)setLabelOpacity:(CGFloat)opacity forState:(CCControlState)state
Parameters
opacity |
Opacity applied to the label. |
---|---|
state |
State to set the opacity for. |
See Also
Declared In
CCButton.h
– labelOpacityForState:
Gets the label’s opacity for the specified state.
- (CGFloat)labelOpacityForState:(CCControlState)state
Parameters
state |
State to get the opacity for. |
---|
Return Value
Label opacity.
See Also
Declared In
CCButton.h