Hi everybody!
I have written a small class to help me in the design of control objects (like special buttons, sliders, drag&drop object, etc.) into Cocos2d. I was inspired by the UIControl API from the UIKit framework. I would share it with you in order to see if this could have any utility and why not improved it in order to better meet the needs of everyone.
You can find the file to my github here: https://github.com/YannickL/CCControl
To illustrate how to use it I have modified the CCSlider from the cocos2d-iphone-extensions. You can download it here: https://github.com/YannickL/CCControl-Examples
A quick example to show you how we use the CCSlider after integrating the CCControl here:
- (id)init
{
if((self=[super init]))
{
CGSize screenSize = [[CCDirector sharedDirector] winSize];
// Add a label in which the slider value will be display
self.displayValueLabel = [CCLabelTTF labelWithString:@"Move the slider thumb!" fontName:@"Marker Felt" fontSize:32];
displayValueLabel.anchorPoint = ccp(0.5f, -1);
displayValueLabel.position = ccp(screenSize.width / 2.0f, screenSize.height / 2.0f);
[self addChild:displayValueLabel];
CCSlider *slider = [CCSlider sliderWithBackgroundFile:@"sliderBG.png" thumbFile:@"sliderThumb.png"];
slider.anchorPoint = ccp(0.5f, 1);
slider.position = ccp(screenSize.width / 2.0f, screenSize.height / 2.0f);
// When the value of the slider will change, the given selector will be call
[slider addTarget:self action:@selector(valueChanged:) forControlEvents:CCControlEventValueChanged];
[self addChild:slider];
}
return self;
}
- (void)valueChanged:(CCSlider *)sender
{
// Change value of label.
displayValueLabel.string = [NSString stringWithFormat:@"Slider value = %.02f", sender.value];
}
For more details/documentation you can check this blog post: http://yannickloriot.com/2011/08/create-a-control-object-with-cocos2d-for-iphone/
Best,
Yannick


