我创建了一个自定义类LevelScoreCard:CCNode,它有一些CCMenuItemImages。
以下是界面和实施细节
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface LevelScoreCard : CCNode {
@private CCSprite *levelScoreBackGround;
@private CGSize winSize;
}
-(id)initForLevelNo:(int)levelNo withWiningStatus:(BOOL)won;
@end
和
#import "LevelScoreCard.h"
#import "TestLayer3.h"
@implementation LevelScoreCard
-(id)initForLevelNo:(int)levelNo withWiningStatus:(BOOL)won
{
if( (self=[super init]) ) {
winSize=[CCDirector sharedDirector].winSize;
//Adding the BackGround
levelScoreBackGround=[CCSprite spriteWithFile:@"levelScoreBackGround.png"];
self.contentSize=levelScoreBackGround.contentSize;
[self addChild:levelScoreBackGround ];
//Adding the Game Level-Wining Status
CCSprite *winingStatus;
if (won) {
winingStatus=[CCSprite spriteWithFile:@"levelCompleted.png"];
}
else{
winingStatus=[CCSprite spriteWithFile:@"levelFailed.png"];
CCSprite *skull=[CCSprite spriteWithFile:@"skull.png"];
skull.position=ccp(0,-self.contentSize.height*0.07);
[self addChild:skull];
}
winingStatus.position=ccp(0,self.contentSize.height*0.32);
[self addChild:winingStatus];
CCMenuItem *homeButton = [CCMenuItemImage itemWithNormalImage:@"home1.png"
selectedImage:@"home1.png"
block:^(id sender) {
printf("\nHome button clicked");
}];
CCMenuItem *replayButton = [CCMenuItemImage itemWithNormalImage:@"replay.png"
selectedImage:@"replay.png"
block:^(id sender) {
printf("\nReplay button clicked");
}];
CCMenuItem *nextButton;
CCMenu *menu;
NSDictionary *nextLevelInfo=[self gameInformationForLevel:levelNo+1];
//Create a menu from the button and center it on the screen
if (![[nextLevelInfo objectForKey:@"isLocked"] intValue]) {
nextButton = [CCMenuItemImage itemWithNormalImage:@"next.png"
selectedImage:@"next.png"
block:^(id sender) {
printf("\nNext button clicked");
}];
menu = [CCMenu menuWithItems:homeButton, replayButton,nextButton, nil];
[menu alignItemsHorizontallyWithPadding:60];
}
else
{
menu = [CCMenu menuWithItems:homeButton, replayButton, nil];
[menu alignItemsHorizontallyWithPadding:20];
}
menu.position = ccp(0,-self.contentSize.height*0.35);
//Add the menu as a child to this layer
[self addChild:menu];
}
return self;
}
-(NSDictionary*)gameInformationForLevel:(int)levelNo
{
//---Retrieving Information About Levels----
NSMutableDictionary *gameInfo=[[NSUserDefaults standardUserDefaults] objectForKey:@"gameInfo"];
NSString *levelKey=[NSString stringWithFormat:@"level%dInfo",levelNo];
return [[gameInfo objectForKey:levelKey] copy];
}
@end
当我使用直接从IntroLayer加载的简单CCLayer中的 LevelScoreCard时,与MenuItems相关联的块工作正常。
但是当我在另一个CCLayer 中使用它时,它是从第二个CCLayer有条件加载的,具有以下代码的StartLayer,MenuItems没有响应
-(void)loadLevelAfterDelay:(ccTime)dt
{
#warning Game has 4 levels, implement game layer 2,3,4 after fixing the story
switch (levelSelected) {
case 1:
{
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.50 scene:[Level1 scene] ]];
break;
}
case 2:
{
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.50 scene:[TestLayer2 scene] ]];
break;
}
case 3:
{
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.50 scene:[Level1 scene] ]];
break;
}
case 4:
{
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.50 scene:[Level1 scene] ]];
break;
}
default:
break;
}
}
答案 0 :(得分:0)
问题是前一层StartLayer:CCLayer有一个SettingsMenu:CCNode,它已注册到touchDispatcher
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:INT_MIN+2 swallowsTouches:YES];
这是在将它们交付给CCMenuItemImages之前吞下触摸的罪魁祸首。因此,我学到了一个很好的教训,即从未注册任何可以吞下触摸的CCNode类型项目。 使用以下代码注册touchDispatcher时,不要吞下触摸是个好习惯
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:INT_MIN+2 swallowsTouches:NO];