菜鸟教程小白 发表于 2022-12-13 06:52:20

ios - 无法获取ChildByTag


                                            <p><p>我正在关注 Lynda.com iOS 教程,该教程使用 cocos2d(第 2 版)进行游戏开发。讲师使用以下代码使 Sprite 图像在触摸事件时增大。下面代码中的日志语句正在工作(即启用了触摸事件),但是当我单击模拟器时,图像并没有变大。原来这一行的痣是零</p>

<pre><code>CCSprite *mole =(CCSprite *);
</code></pre>

<p>这是整个方法</p>

<pre><code>- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = ;
    CGPoint location = ];
    location = [ convertToGL: location ];
    CCLOG(@&#34;touch happened at x: %0.2f y: %0.2f&#34;,location.x, location.y );
    CCSprite *mole =(CCSprite *);

    if (CGRectContainsPoint(, location)) {
      [mole runAction:[CCSequence actions:
                         ,
                         ,
                         nil]];
    }
}
</code></pre>

<p>通过下面的 <code>init</code> 方法,痣出现在屏幕上。我假设上面的行 <code>CCSprite *mole = (CCSprite *);</code> 应该得到对小时候的痣的引用,但它显然不起作用。</code> p>

<pre><code>-(id) init
{
    // always call &#34;super&#34; init
    // Apple recommends to re-assign &#34;self&#34; with the &#34;super&#34; return value
    if( (self=)) {
      self.isTouchEnabled = YES;
      CGSize s = [ winSize];
      CCSprite *mole = ;
      mole.position = ccp(s.width/2,s.height/2);
      ;
      mole.tag = 1;

      CCSprite *bg = ;
      bg.anchorPoint = ccp(0,0);
      ;
    }
    return self;
}
</code></pre>

<p>你能从上面的代码中看出为什么这里的mole是nil吗?</p>

<pre><code> CCSprite *mole =(CCSprite *);
</code></pre>

<p>更新</p>

<p>HelloWorldLayer.h</p>

<pre><code>#import &lt;GameKit/GameKit.h&gt;

// When you import this file, you import all the cocos2d classes
#import &#34;cocos2d.h&#34;

// HelloWorldLayer
@interface HelloWorldLayer : CCLayer &lt;GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate&gt;
{
}

// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;

@end
</code></pre>

<p>HelloWorldLayer.m</p>

<pre><code>// Import the interfaces
#import &#34;HelloWorldLayer.h&#34;

// Needed to obtain the Navigation Controller
#import &#34;AppDelegate.h&#34;

#pragma mark - HelloWorldLayer

// HelloWorldLayer implementation
@implementation HelloWorldLayer

// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
    // &#39;scene&#39; is an autorelease object.
    CCScene *scene = ;

    // &#39;layer&#39; is an autorelease object.
    HelloWorldLayer *layer = ;

    // add layer as a child to scene
    ;

    // return the scene
    return scene;
}

// on &#34;init&#34; you need to initialize your instance
-(id) init
{
    // always call &#34;super&#34; init
    // Apple recommends to re-assign &#34;self&#34; with the &#34;super&#39;s&#34; return value
    if( (self=) ) {

      self.isTouchEnabled = YES;

      // create and initialize a Label
//      CCLabelTTF *label = ;
//
//      // ask director for the window size
//      CGSize size = [ winSize];
//
//      // position the label on the center of the screen
//      label.position =ccp( size.width /2 , size.height/2 );
//      
//      // add the label as a child to this Layer
//      ;

      CGSize s = [ winSize];
      [ addSpriteFramesWithFile:@&#34;moles.plist&#34;];
      CCSprite *mole = ;
//      CCSprite *mole = ;
      mole.position = ccp(s.width/2,s.height/2);
      mole.scale = .25;
      //between 0 255
//      mole.opacity = 100;
      NSMutableArray *frames = [ init];
      for (int i = 1; i &lt;= 10; i++) {
            NSString *frameName = ;
             spriteFrameByName:frameName]];
      }
      CCAnimation *a = ;
      ];
      ;


      CCSprite *bg = ;
      //supposed to fill whole screen but not

      //got next three lines from SO
    //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);
      ;
      //
      // Leaderboards and Achievements
      // *

//      // Default font size will be 28 points.
//      ;
//      
//      // to avoid a retain-cycle with the menuitem and blocks
//      __block id copy_self = self;
//      
//      // Achievement Menu Item using blocks
//      CCMenuItem *itemAchievement = [CCMenuItemFont itemWithString:@&#34;Achievements&#34; block:^(id sender) {
//         
//         
//          GKAchievementViewController *achivementViewController = [ init];
//          achivementViewController.achievementDelegate = copy_self;
//         
//          AppController *app = (AppController*) [ delegate];
//         
//          [ presentModalViewController:achivementViewController animated:YES];
//         
//          ;
//      }];
//      
//      // Leaderboard Menu Item using blocks
//      CCMenuItem *itemLeaderboard = [CCMenuItemFont itemWithString:@&#34;Leaderboard&#34; block:^(id sender) {
//         
//         
//          GKLeaderboardViewController *leaderboardViewController = [ init];
//          leaderboardViewController.leaderboardDelegate = copy_self;
//         
//          AppController *app = (AppController*) [ delegate];
//         
//          [ presentModalViewController:leaderboardViewController animated:YES];
//         
//          ;
//      }];
//
//      
//      CCMenu *menu = ;
//      
//      ;
//      ;
//      
//      // Add the menu to the layer
//      ;

    }
    return self;
}

//- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
//{
//    acceleration.x
//}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = ;
    CGPoint location = ];
    location = [ convertToGL: location ];
    CCLOG(@&#34;touch happened at x: %0.2f y: %0.2f&#34;,location.x, location.y );
    CCSprite *mole =(CCSprite *);
    NSLog(@&#34;mole %@&#34;, mole);
//    if (CGRectContainsPoint(, location))
//    {
//      ];
//      
//    }
    if (CGRectContainsPoint(, location)) {
      [mole runAction:[CCSequence actions:
                         ,
                         ,
                         nil]];
    }
}

// on &#34;dealloc&#34; you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    // don&#39;t forget to call &#34;super dealloc&#34;
    ;
}

#pragma mark GameKit delegate

-(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
    AppController *app = (AppController*) [ delegate];
    [ dismissModalViewControllerAnimated:YES];
}

-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
    AppController *app = (AppController*) [ delegate];
    [ dismissModalViewControllerAnimated:YES];
}
@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>// set the tag value mole sprite in init method (HelloWorldLayer.m).

   CCSprite *mole = ;
    //      CCSprite *mole = ;
    mole.position = ccp(s.width/2,s.height/2);
    mole.scale = .25;
    mole.tag=1;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法获取ChildByTag,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22333445/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22333445/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法获取ChildByTag