I have two classes named MainGame and YourScore. In MainGame I have a Score int that keeps the track of the score in the game. In YourScore I want to show the score in a text label.
Can I change the "Hello world" text in my label to the number of score?
This is the code i have for the text label
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Hello World" fontName:#"Times New Roman" fontSize:32];
label.position = ccp(screenWidth / 2 , screenHeight /1.5 );
label.color = ccc3(255, 255, 255);
[self addChild: label];
You can do that very easily. In the first view controller where the score is declared. Just declare it as a property like the following
#property (nonatomic,assign) int score;
You can access that property in the second class where you want to change the Hello world text label.
just make the object of the class and use label.text = classObject.score;
I have found that it is best to use KVO for this type of UI updating. Basically, the UI object registers as an observer for a specific property on another object. When that property changes, a message is sent to a handler on the UI object, which can use the data to update the UI. This way the model object doesn't even have to be aware of the UI object.
http://iphonedevelopment.blogspot.com/2009/02/kvo-and-iphone-sdk.html
Related
need help here. I am trying to update the score with the code below but instead of displaying a new score, the label text name was overlap each other. For example, initial value is 0 and new value is 10, instead of replacing 0 with 10, the number 10 was overlap with 0. Anyone can help?
Code:
self.sumLabel = [SKLabelNode labelNodeWithFontNamed:#"Chalkduster"];
self.sumLabel.text = #"Score: 0";
self.sumLabel.fontSize = 20;
self.sumLabel.position = CGPointMake(self.size.width-160, self.size.height-450);
[self.sumLabel setText:[NSString stringWithFormat:#"Score: %i", self.initial]];
[self addChild:self.sumLabel];
It looks like you are creating and adding a label multiple times.
Try to reuse the previous one by storing the object into a property
When you want to update it, you just need to call setText
I can easily animate something like the position or size of a UIView. But how can I animate a "custom" variable (such as experiencePoints) to achieve interpolation of values that are not associated with a UIView.
// The variable being animated
CGFloat experiencePoints = 0;
// Pseudo-code
[experiencePoints animateTo:200 duration:2 timingFunction:someTimingFunction];
With this code, if I accessed experiencePoints while it was animating, I would get a value between 0 and 200 based on how long the animation has been going.
Bonus question: Is it possible to use a CAAnimation to do what I want?
You can go with Presentation layer with CADisplayLink and by adding observer on animation status you can increment the variable upto your final one. Observer will observe current status of Animation which will eventually provide you a way to get current value of that VAR.
Ended up using Facebook's POP animation library, which allows the animation of any property on an object. I recommend it!
Here is a quote from the readme, explaining how to do it:
The framework provides many common layer and view animatable properties out of box. You can animate a custom property by creating a new instance of the class. In this example, we declare a custom volume property:
prop = [POPAnimatableProperty propertyWithName:#"com.foo.radio.volume" initializer:^(POPMutableAnimatableProperty *prop) {
// read value
prop.readBlock = ^(id obj, CGFloat values[]) {
values[0] = [obj volume];
};
// write value
prop.writeBlock = ^(id obj, const CGFloat values[]) {
[obj setVolume:values[0]];
};
// dynamics threshold
prop.threshold = 0.01;
}];
anim.property = prop;
"Animating" something is simply moving a value from a starting position to an ending position over time. To "animate" a float from one value to another over a duration you could just run a timer and incrementally change it. Or possibly you could store the begin and end time of the "animation" and whenever the value is accessed, compare those to the actual time to come up with a value.
I feel like an idiot not even posting some code, but after reading several articles stating iOS7 Text Kit adds support for Text Folding, I can't actually find any sample code or an attribute to set on the text to fold it and Apple's documentation seems mute on it.
http://asciiwwdc.com/2013/sessions/220 makes me think I set a region of text into its own text container and then display/hide it, perhaps by overriding setTextContainer:forGlyphRange:
Am I anywhere close?
Thanks
There's a WWDC 2013 video that talks a bit about it when they're doing custom text truncation. Basically you implement the NSLayoutManagerDelegate method layoutManager: shouldGenerateGlyphs: properties: characterIndexes: font: forGlyphRange:
It took me way too much struggling to actually come up with code for this, but here's my implementation based on a property hideNotes
-(NSUInteger)layoutManager:(NSLayoutManager *)layoutManager shouldGenerateGlyphs:(const CGGlyph *)glyphs
properties:(const NSGlyphProperty *)props characterIndexes:(const NSUInteger *)charIndexes
font:(UIFont *)aFont forGlyphRange:(NSRange)glyphRange {
if (self.hideNotes) {
NSGlyphProperty *properties = malloc(sizeof(NSGlyphProperty) * glyphRange.length);
for (int i = 0; i < glyphRange.length; i++) {
NSUInteger glyphIndex = glyphRange.location + i;
NSDictionary *charAttributes = [_textStorage attributesAtIndex:glyphIndex effectiveRange:NULL];
if ([[charAttributes objectForKey:CSNoteAttribute] isEqualToNumber:#YES]) {
properties[i] = NSGlyphPropertyNull;
} else {
properties[i] = props[i];
}
}
[layoutManager setGlyphs:glyphs properties:properties characterIndexes:charIndexes font:aFont forGlyphRange:glyphRange];
return glyphRange.length;
}
[layoutManager setGlyphs:glyphs properties:props characterIndexes:charIndexes font:aFont forGlyphRange:glyphRange];
return glyphRange.length;
}
The NSLayoutManager method setGlyphs: properties: characterIndexes: font: forGlyphRange: is called in the default implementation and basically does all of the work. The return value is the number of glyphs to actually generate, returning 0 tells the layout manager to do its default implementation so I just return the length of the glyph range it passes in. The main part of the method goes through all of the characters in the text storage and if it has a certain attribute, sets the associated property to NSGlyphPropertyNull which tells the layout manager to not display it, otherwise it just sets the property to whatever was passed in for it.
I've got a test Shinobi Control chart working but wnat to put a hoop, "O", at each data point.
How do you do this?
You need to set the point color on the series style object appropriately. The series which have the ability to show point (SChartScatterSeries, SChartLineSeries, SChartBandSeries) all have a pointStyle property within their style object, which is an instance of SChartPointStyle. This has the following relevant properties:
innerColor
color
innerRadius
outerRadius
showPoints
Depending on the effect you want (and the series type you are using) you need to set these appropriately. For example, to show 'hoops' on an SChartScatterSeries, you could use the following for the data source method:
- (SChartSeries *)sChart:(ShinobiChart *)chart seriesAtIndex:(int)index
{
_series = [SChartColumnSeries new];
SChartScatterSeries *series = [SChartScatterSeries new];
series.style.pointStyle.innerColor = [UIColor whiteColor];
return series;
}
Note, if you're using SChartBandSeries or SChartLineSeries, these have showPoints set to NO by default, so you would need to set this to YES yourself.
I am using coreplot 0.9 .I had tried setting linecolor property for CPTLineStyle by
But it is giving error that color or fontSize is readonly property. Please give me some solution for this.
static CPTTextStyle *labelTextStyle= nil ;
labelTextStyle = [[CPTTextStyle alloc]init];
labelTextStyle.color =[CPTColor whiteColor];
labelTextStyle.fontSize = 10.0f ;
Use a CPTMutableTextStyle. In Core Plot, text styles, line styles, shadows, and numeric data objects come in two variants—mutable and immutable. This follows the pattern common to many Cocoa objects like NSString and NSArray.