I am new in cocos2d iPhone development.My question is how to use CCSlider in Cocos2d v3.2.
CCSlider has only two parameter (background spriteFrame and HandleImage).How we can change the colour/Image of slider when we change the Thumb position.How we can differentiate between empty and full bars, how to make progress of filling?
Suppose I want to use to colour like red(Empty) and green(Fill)/two different images for slider.By default the slider colour is red means the slider value is 0.0f(Empty).if I increase the value of slider then the slider colour will change to green(Fill) from position 0.0 to current thumb position.Please help me.sorry for my question i am very week in english
Thank You.
Here is how i use CCSlider for volume control. This code is in the init method of a GameOptions menu class:
CCSpriteFrame *frRuler = [CCSpriteFrame frameWithImageNamed:#"slider_track.png"];
CCSpriteFrame *frKnob = [CCSpriteFrame frameWithImageNamed:#"slider_knob.png"];
_mainGain = [[CCSlider alloc] initWithBackground:frRuler andHandleImage:frKnob];
[self addChild:_mainGain];
[_mainGain setTarget:self selector:#selector(onMainGainChange:)];
_mainGain.anchorPoint = ccp(.5, .5);
_mainGain.visible = YES;
_mainGain.positionType = CCPositionTypePoints;
_mainGain.position = ccp(kScreenWidth / 2, kScreenHeight / 2 - 55);
_mainGain.continuous = YES;
_mainGain.sliderValue = _battlesSettings.audio.masterVolumeGain; // initial
with the following method to receive the slider control 'slide' events :
- (void)onMainGainChange:(id)sender {
CCSlider *sl = (CCSlider *) sender;
_battlesSettings.audio.masterVolumeGain = sl.sliderValue;
_isSettingsDirty = YES;
}
Related
I have a UIButton, and I would like to access the UIImageView of its background image so that I can make the image circular. I know that I can affect the image itself but I would prefer to do this more elegantly. I also know that I can use the button.currentBackgroundImage property to get the UIImage in the background, but I want the view itself. Once I have the view I intend to use this code:
buttonImageView.layer.cornerRadius = buttonImageView.frame.size.width / 2;
buttonImageView.clipsToBounds = YES;
How can I access the buttonImageView?
EDIT:
Due to #vhristoskov's suggestions, I tried cropping the button itself with this code:
self.button.layer.cornerRadius = self.button.frame.size.width/2;
self.button.clipsToBounds = YES;
And it made this shape:
After debugging I found that frame.size.width was 46, when it should have been 100. So I tried this code instead:
self.button.layer.cornerRadius = self.button.currentBackgroundImage.size.width/2;
self.button.clipsToBounds = YES;
And that produced this shape:
And this time, the cornerRadius was being set to 65. So it actually seems like my problem is that I don't have the correct width at the moment. Which property should I access to get the correct number?
Well as I guessed and as you've already found - the problem is in the button size. To be sure that your button's size at runtime is what you expected to be - review your constraints. In the example below the button has vertical and horizontal central alignment and fixed width and height.
Constraints:
To have perfectly circular button you should have button.width == button.height. If this condition is met your code should work like a charm:
self.button.layer.cornerRadius = CGRectGetWidth(self.button.frame) / 2;
self.button.clipsToBounds = YES;
Assuming that you called something like:
[self.myButton setBackgroundImage:[UIImage imageNamed:#"image"] forState:UIControlStateNormal];
in viewDidLoad or earlier, you can then call the following in viewDidAppear:
if (self.myButton.subviews.count > 0 && [self.myButton.subviews[0] isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = self.myButton.subviews[0];
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
imageView.clipsToBounds = YES;
}
but it is not more elegant as the subview ordering is an implementation detail of UIButton and may change at any time.
I have been wondering on how to set up a scrolling (Horizontal) Menu. I have seen some tutorials but in Cocos2d and Cocos2d and SpriteKit are completely different things. I am working in swift and have no clue on how to implement this. I saw a GithHub repository which did a scrolling menu tutorial but it was in Objective-C and I know a bit of Objective-C but there was alot of files in the project to convert. Or is it possible to convert the Cocos2d to swift? I was thinking that I may be able to use a UIPageViewController to get my desired effect. If there is anybody out there who knows how to do this.
Thanks!
Here's the Cocos2d Code and the blog post is here.
// get screen size
CGSize screenSize = [CCDirector sharedDirector].winSize;
/////////////////////////////////////////////////
// PAGE 1
////////////////////////////////////////////////
// create a blank layer for page 1
CCLayer *pageOne = [[CCLayer alloc] init];
// create a label for page 1
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Page 1" fontName:#"Arial Rounded MT Bold" fontSize:44];
label.position = ccp( screenSize.width /2 , screenSize.height/2 );
// add label to page 1 layer
[pageOne addChild:label];
/////////////////////////////////////////////////
// PAGE 2
////////////////////////////////////////////////
// create a blank layer for page 2
CCLayer *pageTwo = [[CCLayer alloc] init];
// create a custom font menu for page 2
CCLabelBMFont *tlabel = [CCLabelBMFont labelWithString:#"Page 2" fntFile:#"customfont.fnt"];
CCMenuItemLabel *titem = [CCMenuItemLabel itemWithLabel:tlabel target:self selector:#selector(testCallback:)];
CCMenu *menu = [CCMenu menuWithItems: titem, nil];
menu.position = ccp(screenSize.width/2, screenSize.height/2);
// add menu to page 2
[pageTwo addChild:menu];
////////////////////////////////////////////////
// now create the scroller and pass-in the pages (set widthOffset to 0 for fullscreen pages)
CCScrollLayer *scroller = [[CCScrollLayer alloc] initWithLayers:[NSMutableArray arrayWithObjects: pageOne,pageTwo,pageThree,nil] widthOffset: 230];
// finally add the scroller to your scene
[self addChild:scroller];
Click for the The GithHub Project
I am trying to make a progress bar with 2 images. One of them is grey and the other picture is green. When I click the button, I want to progress it, but it is not working.
Here is the my code (progressGreen and progressGrey are UIImageViews):
- (IBAction)nextButton:(id)sender {
self.progressGreen.tag = rowNumber; // I am giving a number
[self progess];
}
-(void) progess {
CGRect rect = self.progressGrey.frame;
rect.size.width = (rect.size.width * (self.progressGreen.tag)) / questions.count;
self.progressGreen.frame = rect;
}
What is my mistake?
Use the progressImage and trackImage properties on UIProgressView. This will be much easier than doing it manually.
Hello I am making a side scrolling cocos2d game and I want a label to show how far the user has flown in the game. For some reason with the code I wrote the label is not appearing. Here is my GameEngine class that calls the class method that is supposed to make the label appear:
//Set the meterDistance
meterDistance = [MeterDistance createTheMeterDistance];
[self addChild:meterDistance z:10];
Here is the code in the MeterDistance class:
meters = 1;
meterLabel = [CCLabelBMFont labelWithString:#"0" fntFile:#"green_arcade-ipad.fnt"];
meterLabel.position = ccp(200, screenHeight - 100);
[self addChild:meterLabel z:10];
meterLabel.anchorPoint = ccp(1.0, 0.5);
[self schedule:#selector(updateLabel:)interval:1.0f/20.0f];
Here is the updateLabel method:
-(void)updateLabel:(ccTime)delta{
meters++;
NSString* scoreString = [NSString stringWithFormat:#"%d", meters];
[meterLabel setString:scoreString];
}
It's been a while since I last dealt with cocos2d code...
What you've written looks ok.
Take it one step at a time and see where it goes wrong.
Position your label in the middle of the screen (maybe screenheight is off, or anchorPoint moves the label outside of the screen).
Another possible cause is if the font file name is not exactly #"green_arcade-ipad.fnt".
Maybe you missed a capital letter?
Otherwise maybe some other element of your layer could be obstructing the label.
I'm using muZZkat's custom range slider: NMRangeSlider#github1
But i need to fade the slider. When i create the range slider, i put it into a content view. I've tried setting the alpha of the content view to .7
UIView *sliderView = [self configureLabelSliderWithSize:CGSizeMake(300, 70)];
sliderView.alpha = .4;
but it does not give the desired effect:
The UIImageView upperHandle and lowerHandle also fade out which shows the background track (the blue part). I'd rather have the handle's alpha be higher so you can't see the blue part behind it.
I tried adding a method to the ranger slider class:
-(void)toggleAlpha {
self.trackBackground.alpha = .3;
self.track.alpha = .3;
self.lowerHandle.alpha = .3;
self.upperHandle.alpha = .3;
}
but this does not change the alpha at all.
The UIImageViews are all initalized in the -(void)addSubviews method:
- (void) addSubviews
{
//------------------------------
// Track Brackground
self.trackBackground = [[UIImageView alloc] initWithImage:self.trackBackgroundImage];
self.trackBackground.frame = [self trackBackgroundRect];
//------------------------------
// Track
self.track = [[UIImageView alloc] initWithImage:self.trackImage];
self.track.frame = [self trackRect];
//------------------------------
// Lower Handle Handle
self.lowerHandle = [[UIImageView alloc] initWithImage:self.lowerHandleImageNormal highlightedImage:self.lowerHandleImageHighlighted];
self.lowerHandle.frame = [self thumbRectForValue:_lowerValue image:self.lowerHandleImageNormal];
//------------------------------
// Upper Handle Handle
self.upperHandle = [[UIImageView alloc] initWithImage:self.upperHandleImageNormal highlightedImage:self.upperHandleImageHighlighted];
self.upperHandle.frame = [self thumbRectForValue:_upperValue image:self.upperHandleImageNormal];
[self addSubview:self.trackBackground];
[self addSubview:self.track];
[self addSubview:self.lowerHandle];
[self addSubview:self.upperHandle];
}
and if i set their alpha lower here, it works.
I don't understand why i can't alter the alpha from anywhere else?
I just added the NMRangeSlider-Demo and checked it out.
I think the problem is that you're probably calling toggleAlpha in viewDidLoad method, right?
You should instead call in the viewDidAppear method, since layoutSubviews method (which in turn calls addSubview) in the NMRangeSlider class, is called after viewDidLoad and before viewDidAppear, so whatever you do in viewDidLoad will be reset as the subviews are initialized in addSubview method.
Just adding toggleAlpha in viewDidAppear worked for me.