我正在使用Lynda.com iOS游戏开发教程,该教程使用cocos2d(版本2)。教师使用以下代码使精灵图像在触摸事件时增大。下面代码中的日志语句正在工作(即启用了触摸事件),但是当我点击模拟器时图像不会变大。事实证明,这一行中的痣是零
CCSprite *mole = (CCSprite *)[self getChildByTag:1];
这是整个方法
- (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 at 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]];
}
}
使用下面init
方法,鼹鼠出现在屏幕上。我假设上面CCSprite *mole = (CCSprite *)[self getChildByTag:1];
的那条线应该是一个孩子的痣,但它显然不起作用。
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "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;
}
你能从上面的代码中看出为什么这里的痣是零吗?
CCSprite *mole = (CCSprite *)[self getChildByTag:1];
更新
HelloWorldLayer.h
#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
HelloWorldLayer.m
// 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 = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// 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=[super init]) ) {
self.isTouchEnabled = YES;
// create and initialize a Label
// CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
//
// // ask director for the window size
// CGSize size = [[CCDirector sharedDirector] 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
// [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];
for (int i = 1; i <= 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 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);
[self addChild:bg z:-1];
//
// Leaderboards and Achievements
// *
// // Default font size will be 28 points.
// [CCMenuItemFont setFontSize:28];
//
// // 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 = [[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 the menu to the 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 at 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" 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"
[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
答案 0 :(得分:4)
// set the 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;
答案 1 :(得分:0)
to check null value:
if(mole!=NULL) //check null value
{
if (CGRectContainsPoint([mole boundingBox], location))
{
[mole stopAllActions];
[mole runAction:[CCSequence actions:
[CCScaleTo actionWithDuration:.25 scale:1.1],
[CCScaleTo actionWithDuration:.25 scale:1.0],
nil]];
}
}
答案 2 :(得分:0)
做KARTHIK RA说的话。
或者,您可以子类化CCSprite并在子类中添加触摸检测和处理程序。当我有几个可触摸的精灵时,我这样做是因为:
祝cocos2d好运。这是一个伟大的引擎。如果您确实迁移到版本3,请记住“标记”不再是CCNode的属性。它已被“name”取代,它实际上是一个字符串类型。标签是一种int类型。