ios - unable to getChildByTag -


i'm following along lynda.com ios tutorial game development uses cocos2d (version 2). instructor uses following code make sprite image increase in size upon touch event. log statement in code below working (i.e. touch events enabled) image not getting larger when click on simulator. turns out mole in line nil

ccsprite *mole =  (ccsprite *)[self getchildbytag:1]; 

here's whole method

- (void)cctouchesbegan:(nsset *)touches withevent:(uievent *)event {     uitouch *touch = [touches anyobject];     cgpoint location = [touch locationinview: [touch view]];     location = [[ccdirector shareddirector] converttogl: location ];     cclog(@"touch happened @ x: %0.2f y: %0.2f",location.x, location.y );     ccsprite *mole =  (ccsprite *)[self getchildbytag:1];      if (cgrectcontainspoint([mole boundingbox], location)) {         [mole runaction:[ccsequence actions:                          [ccscaleto actionwithduration:.25 scale:1.1],                          [ccscaleto actionwithduration:.25 scale:1.0],                          nil]];     } } 

the mole appearing on screen init method below. i'm assuming line above ccsprite *mole = (ccsprite *)[self getchildbytag:1]; supposed reference mole child it's not working.

-(id) init {     // call "super" init     // apple recommends re-assign "self" "super" return value     if( (self=[super init])) {         self.istouchenabled = yes;         cgsize s = [[ccdirector shareddirector] winsize];         ccsprite *mole = [ccsprite spritewithfile:@"mole.png"];         mole.position = ccp(s.width/2,s.height/2);         [self addchild:mole];         mole.tag = 1;          ccsprite *bg = [ccsprite spritewithfile:@"bg.png"];         bg.anchorpoint = ccp(0,0);         [self addchild:bg z:-1];     }     return self; } 

can tell above code why mole here nil?

 ccsprite *mole =  (ccsprite *)[self getchildbytag:1]; 

update

helloworldlayer.h

#import <gamekit/gamekit.h>  // when import file, import cocos2d classes #import "cocos2d.h"  // helloworldlayer @interface helloworldlayer : cclayer <gkachievementviewcontrollerdelegate, gkleaderboardviewcontrollerdelegate> { }  // returns ccscene contains helloworldlayer child +(ccscene *) scene;  @end 

helloworldlayer.m

// import interfaces #import "helloworldlayer.h"  // needed obtain navigation controller #import "appdelegate.h"  #pragma mark - helloworldlayer  // helloworldlayer implementation @implementation helloworldlayer  // helper class method creates scene helloworldlayer child. +(ccscene *) scene {     // 'scene' autorelease object.     ccscene *scene = [ccscene node];      // 'layer' autorelease object.     helloworldlayer *layer = [helloworldlayer node];      // add layer child scene     [scene addchild: layer];      // return scene     return scene; }  // on "init" need initialize instance -(id) init {     // call "super" init     // apple recommends re-assign "self" "super's" return value     if( (self=[super init]) ) {          self.istouchenabled = yes;          // create , initialize label //      cclabelttf *label = [cclabelttf labelwithstring:@"hello world" fontname:@"marker felt" fontsize:64]; // //      // ask director window size //      cgsize size = [[ccdirector shareddirector] winsize]; // //      // position label on center of screen //      label.position =  ccp( size.width /2 , size.height/2 ); //       //      // add label child layer //      [self addchild: label];          cgsize s = [[ccdirector shareddirector] winsize];         [[ccspriteframecache sharedspriteframecache] addspriteframeswithfile:@"moles.plist"];         ccsprite *mole = [ccsprite spritewithspriteframename:@"a0010.png"]; //      ccsprite *mole = [ccsprite spritewithfile:@"mole.png"];         mole.position = ccp(s.width/2,s.height/2);         mole.scale = .25;         //between 0 255 //        mole.opacity = 100;         nsmutablearray *frames = [[nsmutablearray alloc] init];         (int = 1; <= 10; i++) {             nsstring *framename = [nsstring stringwithformat:@"a%04i.png",i];             [frames addobject:[[ccspriteframecache sharedspriteframecache] spriteframebyname:framename]];         }         ccanimation *a = [ccanimation animationwithframes:frames delay:1.0f/24.0f];         [mole runaction:[ccanimate actionwithanimation:a restoreoriginalframe:no]];         [self addchild: mole];           ccsprite *bg = [ccsprite spritewithfile:@"bg.png"];         //supposed fill whole screen not          //got next 3 lines     //stackoverflow.com/questions/12383228/how-to-set-background-image-for-the-entire-scene-in-cocos2d-xcode         cgsize imagesize = bg.contentsize;         bg.scalex = s.width/ imagesize.width;         bg.scaley = s.height/ imagesize.height;          bg.anchorpoint = ccp(0,0);         [self addchild:bg z:-1];         //         // leaderboards , achievements         // *  //      // default font size 28 points. //      [ccmenuitemfont setfontsize:28]; //       //      // avoid retain-cycle menuitem , blocks //      __block id copy_self = self; //       //      // achievement menu item using blocks //      ccmenuitem *itemachievement = [ccmenuitemfont itemwithstring:@"achievements" block:^(id sender) { //           //           //          gkachievementviewcontroller *achivementviewcontroller = [[gkachievementviewcontroller alloc] init]; //          achivementviewcontroller.achievementdelegate = copy_self; //           //          appcontroller *app = (appcontroller*) [[uiapplication sharedapplication] delegate]; //           //          [[app navcontroller] presentmodalviewcontroller:achivementviewcontroller animated:yes]; //           //          [achivementviewcontroller release]; //      }]; //       //      // leaderboard menu item using blocks //      ccmenuitem *itemleaderboard = [ccmenuitemfont itemwithstring:@"leaderboard" block:^(id sender) { //           //           //          gkleaderboardviewcontroller *leaderboardviewcontroller = [[gkleaderboardviewcontroller alloc] init]; //          leaderboardviewcontroller.leaderboarddelegate = copy_self; //           //          appcontroller *app = (appcontroller*) [[uiapplication sharedapplication] delegate]; //           //          [[app navcontroller] presentmodalviewcontroller:leaderboardviewcontroller animated:yes]; //           //          [leaderboardviewcontroller release]; //      }]; // //       //      ccmenu *menu = [ccmenu menuwithitems:itemachievement, itemleaderboard, nil]; //       //      [menu alignitemshorizontallywithpadding:20]; //      [menu setposition:ccp( size.width/2, size.height/2 - 50)]; //       //      // add menu layer //      [self addchild:menu];      }     return self; }  //- (void)accelerometer:(uiaccelerometer *)accelerometer didaccelerate:(uiacceleration *)acceleration //{ //    acceleration.x //}  - (void)cctouchesbegan:(nsset *)touches withevent:(uievent *)event {     uitouch *touch = [touches anyobject];     cgpoint location = [touch locationinview: [touch view]];     location = [[ccdirector shareddirector] converttogl: location ];     cclog(@"touch happened @ x: %0.2f y: %0.2f",location.x, location.y );     ccsprite *mole =  (ccsprite *)[self getchildbytag:1 ];     nslog(@"mole %@", mole); //    if (cgrectcontainspoint([mole boundingbox], location)) //    { //        [mole runaction: [ccscaleby actionwithduration:.25 scale:1.1]]; //         //    }     if (cgrectcontainspoint([mole boundingbox], location)) {         [mole runaction:[ccsequence actions:                          [ccscaleto actionwithduration:.25 scale:1.1],                          [ccscaleto actionwithduration:.25 scale:1.0],                          nil]];     } }  // on "dealloc" need release retained objects - (void) dealloc {     // in case have dealloc, in method     // in particular example nothing needs released.     // cocos2d automatically release children (label)      // don't forget call "super dealloc"     [super dealloc]; }  #pragma mark gamekit delegate  -(void) achievementviewcontrollerdidfinish:(gkachievementviewcontroller *)viewcontroller {     appcontroller *app = (appcontroller*) [[uiapplication sharedapplication] delegate];     [[app navcontroller] dismissmodalviewcontrolleranimated:yes]; }  -(void) leaderboardviewcontrollerdidfinish:(gkleaderboardviewcontroller *)viewcontroller {     appcontroller *app = (appcontroller*) [[uiapplication sharedapplication] delegate];     [[app navcontroller] dismissmodalviewcontrolleranimated:yes]; } @end 

  // set tag value mole sprite in init method (helloworldlayer.m).     ccsprite *mole = [ccsprite spritewithspriteframename:@"a0010.png"];     //      ccsprite *mole = [ccsprite spritewithfile:@"mole.png"];     mole.position = ccp(s.width/2,s.height/2);     mole.scale = .25;     mole.tag=1; 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -