【问题标题】:Touch event Cocos2d iPhone触摸事件 Cocos2d iPhone
【发布时间】:2014-02-18 12:17:10
【问题描述】:

我对 cocos2d 中的触摸事件有疑问,特别是: 我有两层:

GameLayer.m



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

           NSLog(@"GAME");

           CCSprite*game=[CCSprite spriteWithFile:@"alien.png"];
           CGSize size=[CCDirector sharedDirector].winSize;
           self.contentSize=CGSizeMake(50,50);
           self.touchEnabled=YES;
           game.position=CGPointMake(40,40);
           [self addChild:game];
           //NSLog(@"%d",[[self children] count]);
           //NSLog(@"%f",self.position.x);
            }
            return self;
        }
    -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
       NSLog(@"GAME");
    }



UserInterfaceLayer.m


    -(id)init
            {

               if(self=[super init])
               {

                  NSLog(@"USER");
                  self.touchEnabled=YES;
                  CCSprite*game=[CCSprite spriteWithFile:@"spiders.png"];
                  CGSize size=[CCDirector sharedDirector].winSize;
                  self.contentSize=CGSizeMake(20,20);
                  game.position=CGPointMake(40,40);
                  [self addChild:game];
                  //NSLog(@"%d",[[self children] count]);
                  //NSLog(@"%f",self.position.x);

               }

               return self;

            }


        -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
        {
           NSLog(@"USER");
        }

两者都是一个场景的孩子:

MultiLayerScene.m

-(id)init
{
    if(self=[super init])
    {
       sharedMultiLayerScene = self;
       self.contentSize=CGSizeMake(30, 30);

       CGSize size=[CCDirector sharedDirector].winSize;


       // The GameLayer will be moved, rotated and scaled independently
       GameLayer* gameLayer = [GameLayer node];
       gameLayer.position=CGPointMake(250,250);
       [self addChild:gameLayer z:1 tag:1];
       //gamelayerposition = gameLayer.position;



       // The UserInterfaceLayer remains static and relative to the screen area.
       UserInterfaceLayer* uiLayer = [UserInterfaceLayer node];
       uiLayer.position=CGPointMake(-100, -100);
       [self addChild:uiLayer z:-1 tag:2];


    }
   return self;
}

我的问题是我在他们的 ccTouchesBegan 事件运行的任何位置触摸屏幕。 例如,如果我只在 GameLayer 上触摸,则同时运行 GameLayer 触摸事件和另一个。 我也尝试将这两层插入两个不同的位置(从代码中可以看出),但问题仍然存在。 我该如何解决这个问题? 我想,例如,如果我触摸 Gamelayer 只响应它的触摸事件。

【问题讨论】:

    标签: ios iphone cocos2d-iphone


    【解决方案1】:

    试试这个

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    

    优先级:数字越小 = 优先级越高。

    【讨论】:

    • 好的,谢谢Haroon,你解决了我的问题,但我不明白一件事:如果我在屏幕运行的任何一点触摸,你能解释一下为什么(场景是30x30(小))图层的触摸事件?(没有你的解决方案)
    • 我尝试检索 CCScene 的 boundingbox 并将其分层为 CGRectZero 而不是任何其他值,所以不知道为什么它是 30 X 30 因为我知道的是 CCSceneCCLayer两者都没有boundingbox
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多