代码之家  ›  专栏  ›  技术社区  ›  user1597438

忽略cocos2d中图像的透明区域时精灵走错了方向

  •  1
  • user1597438  · 技术社区  · 12 年前

    下列的 guru 的建议 previous question ,我在init方法上添加了这段代码,以防止在精灵的透明区域上发生触摸事件:

        path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, endTouch.x, endTouch.y);
        //250(y) is the height of the path, 50(x) is the width
        //set your width and height to be the same size as the non-transparent part of your sprite
        CGPathAddLineToPoint(path, NULL, 0, 250);
        CGPathAddLineToPoint(path, NULL, 50, 0);
        CGPathCloseSubpath(path);
    

    然后我修改了我的触摸事件,如下所示:

    -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    if(gameStat == TRUE)
    {
        UITouch *touch=[touches anyObject];
        CGPoint loc=[touch locationInView:[touch view]];
        loc=[[CCDirector sharedDirector] convertToGL:loc];
        beginTouch = loc;
    
        for(int i = 0; i < [sprArray count]; i++)
        {
            CCSprite *sprite = (CCSprite *)[sprArray objectAtIndex:i];
            if(CGRectContainsPoint([sprite boundingBox], loc))
            {
                selectedSprite = sprite;
                //test the path
                loc = [selectedSprite convertToNodeSpace:loc];
                if (CGPathContainsPoint(path, NULL, loc, NO) ) {
                    NSLog(@"inside");
                }
                else {
                    NSLog(@"outside");
                }
                break;
            }
        }
    }}
    
    -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint loc1 = [touch previousLocationInView:[touch view]];
    CGPoint loc2 = [touch locationInView:[touch view]];
    loc2 = [[CCDirector sharedDirector]convertToGL:loc2];
    endTouch = location;
    
    int goUp = loc2.y - loc1.y;
    if(goUp > 0){
    CGPoint locationPath  = [selectedSprite convertToNodeSpace:location];
            if (CGPathContainsPoint(path, NULL, locationPath, NO) ) {
                [selectedSprite setPosition:location];
                _spriteTouch = TRUE;
            }
    }}
    

    现在,这很有效,但我的问题是精灵往往会移动错误的方向。它应该跟随向上的触摸滑动,但无论我如何向上滑动,它都会一直向右滑动。我在这里做错了什么?请帮帮我。

    更新:我设法弄清楚了如何修复我的代码,所以这个问题已经解决了。我已经更新了我的代码,以防其他人遇到同样的问题。

    2 回复  |  直到 8 年前
        1
  •  2
  •   Ikmal Ezzani    12 年前

    你试过改变这个吗

     loc = [selectedSprite convertToNodeSpace:loc];
    

    到以下位置

     loc = [selectedSprite convertToWorldSpace:loc];
    

    [EDIT:更新样本]
    由于对convertToGL:基于点的调用被格式化为基于OpenGL,其中:

    协调

    y+           (0,0)                  |y+
    |            +------ y+      x-     |      x+
    |            |               ---- (0,0) ----
    |            |                      |
    + ---- x+    |                      |
    (0,0)        x+                     y-
    OpenGl       Cocoa Touch         Cartesian
    

    触摸来自屏幕的原点(Cocoa Touch)不知道OpenGL坐标,因此需要使用转换 Cocos2d CCDirector :

    • (CGPoint)convertToGL:(CG点)p
      将UIKit坐标转换为OpenGL坐标用于将(多)触摸坐标转换为当前布局(纵向或横向)

    同理,反之亦然

    • (CGPoint)convertToUI:(CGPoint)p
      将OpenGL坐标转换为UIKit坐标对于将节点点转换为glScissor等调用的窗口点很有用

    有时,我会感到困惑,并记录(而不是断点)该点决定了哪个坐标有效,这样我就可以弄清楚逻辑了。

    NSLog(@"%@", NSStringFromCGPoint(touchPoint.position));
    
        2
  •  0
  •   Tarun    12 年前

    在触摸移动方法中,您正在执行以下操作:-

    loc2 = [[CCDirector sharedDirector]convertToGL:loc2];
    

    但不相应地将loc1转换为GL坐标。我认为你应该试试这个:-

    loc1 = [[CCDirector sharedDirector]convertToGL:loc1];
    

    这可能会有所帮助。