objective c - Score system in Cocos2d project -


i have game user collects different types of objects , label has value of 0 @ start. every time user collects object (by touching it) should make score = current score + 1; have tried following code crashes when click on object.

this code score label puts 0 on screen:

score = 0; scorelabel1 = [cclabelttf labelwithstring:@"0" fontname:@"times new roman" fontsize:33]; scorelabel1.position = ccp(240, 160); [self addchild:scorelabel1 z:1]; 

and void function call every time touch object:

- (void) addscore {     score = score + 1;     [scorelabel1 setstring:[nsstring stringwithformat:@"%@", score]]; } 

and actual part put code touching 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];  (apple in self.applearray) {     if (cgrectcontainspoint(apple.boundingbox, location))     {         [self addscore];          apple.visible = no;                 } } 

everything else works except score. there way make apple disappear instead of making invisible apple.visible = false? because way apple still there not visible, want rid of it.

hope 1 can help!

if have questions let me know.

thanks.

this draw apples:

    -(id) init     {         // call "super" init         // apple recommends re-assign "self" "super's" return value      if( (self=[super init]) ) {     istouchenabled_ = yes;     self.applearray = [ccarray arraywithcapacity:20];          (int = 0; < 5; i++) {              apple = [ccsprite spritewithfile:@"apple4.png"];             [self addchild:apple];             [applearray addobject:apple];         }      [apple removefromparentandcleanup:true];      [self scheduleupdate];       }      return self; } 

and screen gets updated:

-(void) update: (cctime) dt { (int = 0; < 5; i++) {      apple = ((ccsprite *)[applearray objectatindex:i]);     if (apple.position.y > -250) {         apple.position = ccp(apple.position.x, apple.position.y - (apple.tag*dt));     } } 

}

a couple of things here. in setscore, format broken , cause crash (%@ requires nsobject*). try:

[scorelabel1 setstring:[nsstring stringwithformat:@"%i", score]]; 

also, syntax of loop odd. try

for (apple *anyapple in self.applearray) {     if (cgrectcontainspoint(anyapple.boundingbox, location))     {         if (anyapple.visible) {             [self addscore];              anyapple.visible = no;          }                } } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -