【问题标题】:how do you assign touches to two CCColorLayer?您如何将触摸分配给两个 CCColorLayer?
【发布时间】:2012-03-08 23:53:21
【问题描述】:

我创建了两个颜色层并将它们显示在屏幕上:

if( (self=[super init] )) {


        self.isTouchEnabled = YES;
        CGSize s = [[CCDirector sharedDirector] winSize];
        CCLayerColor* layer1 = [CCLayerColor layerWithColor: ccc4(255, 255, 0, 80)
                                                 width: 100 
                                                height: 300];
        layer1.position = ccp(s.width/3, s.height/2);
        layer1.isRelativeAnchorPoint = YES;
        [self addChild: layer1 z:1];

        CCLayerColor* layer2 = [CCLayerColor layerWithColor: ccc4(0, 0, 255, 255)
                                                 width: 100 
                                                height: 300];
        layer2.position = ccp((s.width/3)*2, s.height/2);
        layer2.isRelativeAnchorPoint = YES;
        [self addChild: layer2 z:1];

         }
       return self;

     }

我想为每一层启用触摸,第 1 层和第 2 层,例如,如果用户触摸第 1 层(做某事),如果您触摸第 2 层(做其他事情)。我已经设置了触摸调度器,

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

之后我不知道该怎么办,我试图搜索 cocos2d 文档,但找不到任何东西谢谢!

【问题讨论】:

    标签: ios cocoa-touch cocos2d-iphone


    【解决方案1】:

    如果您希望它们都注册到触摸调度程序,您需要对它们进行子类化并直接将registerWithtouchDispatcher 添加到每个子类中,然后在主图层或场景中使用子类。

    如果您不希望图层管理其所有精灵的触摸,这也是您注册单个精灵与他们自己的触摸的方式。

    执行此操作时,您需要设置不同的优先级,以便最重要的层有机会在其他层之前响应触摸,这样您就不会得到多个层处理的相同触摸。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-14
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 2016-12-02
      相关资源
      最近更新 更多