Reputation: 105
I have a game where the user collects different types of objects and a label that has a value of 0 at the start. Every time the user collects an object (by touching it) it should make the score = current score + 1; I have tried with the following code but it crashes when I click on the object.
This is the code for my score label which puts a 0 on the screen:
score = 0;
scoreLabel1 = [CCLabelTTF labelWithString:@"0" fontName:@"Times New Roman" fontSize:33];
scoreLabel1.position = ccp(240, 160);
[self addChild:scoreLabel1 z:1];
And this is the void function which I call every time I touch an object:
- (void) addScore
{
score = score + 1;
[scoreLabel1 setString:[NSString stringWithFormat:@"%@", score]];
}
And this is the actual part where I put the code for touching the object:
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self ccTouchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
for (Apple in self.appleArray)
{
if (CGRectContainsPoint(Apple.boundingBox, location))
{
[self addScore];
Apple.visible = NO;
}
}
Everything else works except for the score. Also is there a way to make the apple disappear instead of just making it invisible by apple.visible = false? because this way the apple is still there but not visible, I want to get rid of it.
Hope some one can help!
If you have any questions let me know.
Thanks.
This is where I draw the apples:
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
isTouchEnabled_ = YES;
self.appleArray = [CCArray arrayWithCapacity:20];
for (int i = 0; i < 5; i++) {
Apple = [CCSprite spriteWithFile:@"Apple4.png"];
[self addChild:Apple];
[appleArray addObject:Apple];
}
[Apple removeFromParentAndCleanup:true];
[self scheduleUpdate];
}
return self;
}
And this is where the screen gets updated:
-(void) update: (ccTime) dt
{
for (int i = 0; i < 5; i++) {
Apple = ((CCSprite *)[appleArray objectAtIndex:i]);
if (Apple.position.y > -250) {
Apple.position = ccp(Apple.position.x, Apple.position.y - (Apple.tag*dt));
}
}
}
Upvotes: 0
Views: 295
Reputation: 9079
a couple of things here. In setScore, your format is broken and will cause a crash (%@ requires an NSObject*). Try:
[scoreLabel1 setString:[NSString stringWithFormat:@"%i", score]];
also, the syntax of your for loop is odd. Try
for (Apple *anyApple in self.appleArray)
{
if (CGRectContainsPoint(anyApple.boundingBox, location))
{
if (anyApple.visible) {
[self addScore];
anyApple.visible = NO;
}
}
}
Upvotes: 1
Reputation: 7850
score = score + 1;
[scoreLabel1 setString:[NSString stringWithFormat:@"%@", score]];
Please read this: String Format Specifiers
%@ - Objective-C object, printed as the string returned by descriptionWithLocale:
if available, or description otherwise. Also works with CFTypeRef
objects, returning the result of the CFCopyDescription
function.
If your score
is an object, you can't "increment" its value that way. If it's an int or a float, you are using wrong format specifier.
Upvotes: 0