我试图通过在我的场景中显示一些精灵并将menuitemsprite显示为继续按钮来模拟cocos2d中的模态视图。在下面的代码中,我显示我的游戏模式,并使用CCMenuItemSprite设置菜单;它不响应触摸和CCMenuItemImage;这确实有用。
-(void) gameOver {
CGSize size = [[CCDirector sharedDirector] winSize];
self.menu.isTouchEnabled = NO;
CCLayer *modalLayer = [[CCLayer alloc] init];
[self addChild:modalLayer z:20];
CCSprite *spriteGameOver = [CCSprite spriteWithFile:@"game_over.png"];
spriteGameOver.position = ccp( size.width/2,size.height/2);
CCLabelTTF *lblGameOver = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Game Over!\nScore %d/%d",numCorrect,questionIdx] dimensions:CGSizeMake(380, 300) alignment:CCTextAlignmentCenter fontName:@"Trebuchet MS" fontSize:50.0f];
// position the label on the center of the screen
lblGameOver.position = ccp(size.width/2-200, size.height/2-100);
lblGameOver.color = ccc3(20, 20, 20);
lblGameOver.opacity = 0;
// add the label as a child to this Layer
[spriteGameOver addChild: lblGameOver];
spriteGameOver.opacity = 0;
[modalLayer addChild:spriteGameOver];
CCSprite *spriteGameOverBtn = [CCSprite spriteWithFile:@"mainButton.png" rect:CGRectMake(0,0,300,60)];
spriteGameOverBtn.position = ccp( size.width/2,size.height/2-100);
CCLabelTTF *lblGameOverBtn = [CCLabelTTF labelWithString:@"Continue" dimensions:CGSizeMake(300, 60) alignment:CCTextAlignmentCenter fontName:@"Trebuchet MS" fontSize:40.0f];
//lblGameOverBtn.position = ccp(size.width/2-200, size.height/2-300);
[lblGameOverBtn setAnchorPoint:ccp(0.0f,0.1f)];
lblGameOverBtn.color = ccc3(20, 20, 20);
lblGameOverBtn.opacity = 0;
// add the label as a child to this Layer
[spriteGameOverBtn addChild: lblGameOverBtn];
spriteGameOverBtn.opacity = 0;
CCMenuItemImage *itemH = [CCMenuItemImage itemFromNormalImage:@"backArrow.png" selectedImage:@"backArrowS.png" target:self selector:@selector(goToMain:)];
itemH.position = ccp( size.width/2,size.height/2-100);
CCMenuItemSprite *mGameOverBtn = [CCMenuItemSprite itemFromNormalSprite:spriteGameOverBtn selectedSprite:nil disabledSprite:nil target:self selector:@selector(goToMain:)];
CCMenu *menuGO = [CCMenu menuWithItems: itemH,mGameOverBtn, nil];
menuGO.position = ccp( 0, 0);
[modalLayer addChild:menuGO z:21];
[lblGameOverBtn runAction:[CCSequence actions:[CCDelayTime actionWithDuration: 1.75f],[CCFadeIn actionWithDuration: 1.75f],nil]];
[spriteGameOverBtn runAction:[CCSequence actions:[CCDelayTime actionWithDuration: 1.75f],[CCFadeIn actionWithDuration: 1.75f],[CCDelayTime actionWithDuration: 3.75f],nil]];
[lblGameOver runAction:[CCSequence actions:[CCDelayTime actionWithDuration: 1.75f],[CCFadeIn actionWithDuration: 1.75f],[CCDelayTime actionWithDuration: 3.75f],nil]];
[spriteGameOver runAction:[CCSequence actions:[CCDelayTime actionWithDuration: 1.75f],[CCFadeIn actionWithDuration: 1.75f],[CCDelayTime actionWithDuration: 3.75f],nil]];
//[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration: 2.75f],[CCCallFunc actionWithTarget:self selector:@selector(goToMain:)], nil]];
}
答案 0 :(得分:1)
我也遇到了与CCMenuItemSprite相同的问题。看起来CCMenuItemSprite类有点儿错误。在我的情况下,它没有回应触摸。但我发现它与CCMenuItemSprite中的多层Sprite有关。因此,当我使用单层Sprite时,它可以工作,但是当我使用包含多个嵌入式精灵的Sprite时,它不会。
我现在的“尚未准备好”的解决方案是在初始化之后立即将contentSize设置为CCMenuItemSprite的适当大小:
CCMenuItemSprite * menuItem = [CCMenuItemSprite itemWithNormalSprite:multiLayeredSprite selectedSprite:nil target:self selector:@selector(clickHandler:)];
[s setContentSize:backgroundSprite.contentSize];
CCMenu * menu = [CCMenu menuWithItems:
menuItem,
nil];
现在正在接收触摸事件。我现在唯一的问题是矩形的定位......它仍然在右上角。我会试着找出如何解决这个问题。 希望这能澄清一些问题。
答案 1 :(得分:0)
您是否已经通过触摸调度员注册了您的场景,并且您的选择器实际上做了什么?
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO]