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(@"touch happened at x: %0.2f y: %0.2f",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 "super" init
// Apple recommends to re-assign "self" with the "super" 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 <GameKit/GameKit.h>
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>
{
}
// 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 "HelloWorldLayer.h"
// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
#pragma mark - HelloWorldLayer
// HelloWorldLayer implementation
@implementation HelloWorldLayer
// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = ;
// 'layer' is an autorelease object.
HelloWorldLayer *layer = ;
// add layer as a child to scene
;
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" 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:@"moles.plist"];
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 <= 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:@"Achievements" 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:@"Leaderboard" 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(@"touch happened at x: %0.2f y: %0.2f",location.x, location.y );
CCSprite *mole =(CCSprite *);
NSLog(@"mole %@", mole);
// if (CGRectContainsPoint(, location))
// {
// ];
//
// }
if (CGRectContainsPoint(, location)) {
[mole runAction:[CCSequence actions:
,
,
nil]];
}
}
// on "dealloc" 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't forget to call "super dealloc"
;
}
#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]