Update Score Label - ios

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

Related

How to add 1 to the score each time an object's y coordinate is equal to another object's y coordinate

i have a game where the user must move the ball in a vertical way between 2 objects , i want to have a scoring system that allows me to add 1 to the score each time the ball is equal to the 2 objects so i tried the code below but it is not working probably,it is not adding one each time the ball's y coordinates are equal to the object's y cooridnates.
if(imageview.center.y == imageview1.center.y){
int score = score + 1;
scorelabel.text = [NSString stringWithFormat:#"%i",score];
}
As I assume your imageview.centre.y will be different then imageView1.centre.y .Also you could use this method CGRectIntersection(<#CGRect r1#>, <#CGRect r2#>) and place your both frame in provided fields. So it will notify whenever there is an intersection of two frames. Code will be like -
if (CGRectIntersectsRect(self.pacman.frame, self.exit.frame))
{
//Wirte your score handling code here...
}
else
{
//Skip or do what you want...
}
Hope this might help you.
Your variable is recreated every time your if is performing.
Define it somewhere before #implementation
int score = 0;
And replace int score = score + 1; with score++
As comment you should be aware of comparison of double. See This Question for details

Average of textfields depending on how many textfield are "used"

Little explication: I have an app with 5 textfields, i already got the code to make the avaerage of them. Using floates and giving a variable to each textfield.so, the adding of the value of all the textfield /5 (divided per 5)
My problem is that when a textfield is leaved emptty, its not goona be /5, if user fill in only 4 cells it will be/4 and so on.
Question: how to divide the adding of cells depending in how many cells have content?
I was tryng with textfield.text.length > 0 in an if. But i dont get it
Thanks, hope i was clear.
You could check each textfield and see if the text is NULL or blank (#"")
Use an int to keep track of the number of textfields that have a value.
For example:
int counter = 0;
float total = 0.0;
if([textfield1.text length] > 0)
{
counter++;
float thisValue = [textfield1.text floatValue];
total = total + thisValue;
}
repeat this for all 5 textfields and then divide your total by the counter variable.
You could include the if statement in a loop if you are going to vary the number of text fields or to reduce the amount of coding you do.
EDIT I have changed my answer based on the good suggestions of other to use text.length
try to count the number of used textfields by increasing a number every time you read out a textfield that has valid text in it and divide the result by that number. (logically the "number" should be initialized as 0)

trying to add number to number allready saved on label

So I have a TextField integer that needs to be added to a label.
For example when I type 20, it appears in the label and when I save it and come back it's still there, so far so good right, i got this part down.
Now lets say I come back and want to add another amount to whats already there, to the 20. how exactly would I go about doing it?
I used the NSUserDefaults to load/save the data but when I go to update the label I get stuck when trying to use my variables, say: currentValue, valueToAdd and valueTotalAfterAddition.
You have to get the intValue of the text, and then add whatever number. Example
int currentValue = textField.text.intValue;
int valueToAdd = 10; //change this to whatever you need
int valueTotalAfterAddition = currentValue + valueToAdd;
label.text = [NSString stringWithFormat:#"%d", valueTotalAfterAddition];

iOS accessing int from another class

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

Adding a visual side to a schedule in Cocos2d

I am trying to make a timer for my game that counts down from a number, let's call it 100. I am following the cocos2d best practices, and therefore I am not using a NSTimer. What I am looking to do is that every second, I want to numbers of this timer to change. I could probably find a way to do it using a spritesheet with all of the numbers from 100-0, but I know there is a way to do it using just the numbers 0-9 and their pictures.
This is the code that I am using, with the corresponding -(void)
[self schedule: #selector(tick:)];
[self schedule: #selector(tick2:) interval:1];
All in all, I would like to know how to make it count down from 100, but also to know how to make those ticks decrease the value by 1 every second.
Initialize an integer variable that will keep your countdown value:
int count = 100;
You will want to keep a label (CCLabelBMFont etc) to display this count value. I recommend Glyph Designer (or Hierro if you want something free) to generate the 0 to 9 cocos2D-compatible font bitmaps, which you can then use in your CCLabelBMFont:
CCLabelBMFont* countLabel = [CCLabelBMFont labelWithString:#"0" fntFile:#"myFont.fnt"];
Next, schedule a single tick function that will fire every second:
[self schedule: #selector(tick:) interval:1];
This tick function decrements count by 1 each time it is called. Also, add the condition that if count has reached 0, it will unschedule itself:
-void tick:(ccTime) dt
{
count --; // decrement count by 1 each time this function is called
if (count == 0)
[self unschedule: #selector(tick:)];
}
And finally, in your main update loop (or even in the tick function itself after you decrement your count), you can update and redraw this label with the latest value each time:
[countLabel setString:[NSString stringWithFormat:#"%i", count]];
All the best.

Resources