【问题标题】:cocos2d enable touch in second scenecocos2d 在第二个场景中启用触摸
【发布时间】:2011-08-03 09:51:26
【问题描述】:

我有带按钮的菜单 触摸播放按钮时 游戏去另一个场景 但是触摸在那里不起作用 我在写 self.isToucheEnabled=YES; 在初始化方法中 并添加 onEnter 方法 [[CCTouchDispatcher sharedDispatcher] setDispatchEvents:YES];

但这不起作用 请帮助我为什么可以启用触摸

【问题讨论】:

  • 提供您的代码。并具体问你的问题。您的菜单按钮不起作用或您的图层对触摸没有响应??
  • 我的图层对触摸没有响应

标签: cocos2d-iphone touch layer scene


【解决方案1】:

您必须输入以下代码:

-(void) onEnter { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; }

在您要启用 TouchDispatcher 的场景中,然后确保在同一场景中输入以下内容:

-(void) onExit { [[CCTouchDispatcher sharedDispatcher] removeDelegate: self]; }

并且触摸应该在您拥有上述代码的每个场景中注册。

【讨论】:

    【解决方案2】:

    这是一个如何在 cocos2d (source) 中定义菜单的示例:

    CCMenuItem *starMenuItem = [CCMenuItemImage  temFromNormalImage:@"ButtonStar.jpg" selectedImage:@"ButtonStarSel.jpg" target:self selector:@selector(starButtonTapped:)];
    starMenuItem.position = ccp(60, 60);
    CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil];
    starMenu.position = CGPointZero;
    [self addChild:starMenu];
    

    如果您需要更多帮助,请提供您用于创建菜单的代码。

    【讨论】:

    • - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ NSLog(@"touch");返回是; } 我有这个方法,但不起作用。
    【解决方案3】:

    您还必须在您的 CCLayer 的 header 中添加 UIGestureRecognizerDelegate 接口!

    例如:

    @interface YourScene : CCLayer <UIGestureRecognizerDelegate>  {
         }
    

    【讨论】:

      【解决方案4】:

      在图层中使用:

      -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

      不是:

      -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 2021-03-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多