【问题标题】:Sprites going the wrong way when ignoring transparent areas of image in cocos2d忽略 cocos2d 中图像的透明区域时精灵走错路
【发布时间】:2013-02-19 03:07:31
【问题描述】:

根据我的previous questionguru 的建议,我在我的 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;
        }
}}

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

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

【问题讨论】:

    标签: objective-c cocos2d-iphone


    【解决方案1】:

    您是否尝试过更改此设置

     loc = [selectedSprite convertToNodeSpace:loc];
    

    到下面

     loc = [selectedSprite convertToWorldSpace:loc];
    

    [编辑:更新示例]
    由于对 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) 转换为GL:(CGPoint)p
      将 UIKit 坐标转换为 OpenGL 坐标 用于将(多)触摸坐标转换为当前布局(纵向或横向)

    反之亦然

    • (CGPoint)convertToUI:(CGPoint)p
      将 OpenGL 坐标转换为 UIKit 坐标 用于将节点点转换为窗口点,以便调用例如 glScissor

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

    NSLog(@"%@", NSStringFromCGPoint(touchPoint.position));
    

    【讨论】:

    • 感谢您的建议。我试过了,但很遗憾,它什么也没做。它一直在“外面”打印,因此我的精灵根本没有移动。
    • 您可以尝试记录该点并检查它是否返回正确的触摸点,如下所示: NSLog(@"%@", NSStringFromCGPoint(touchPoint));
    • 啊,请注意。你提到触摸滑动。所以我应该看看 touchMoved:.你试过在 touchMoved: 中转换吗?
    • 在 touchMoved 中转换到世界空间?是的,它也没有用。
    • 再次感谢您的帮助。我能够通过对我的代码进行一些调整来修复它。
    【解决方案2】:

    在 touchesMoved 方法中,你正在这样做:-

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

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

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

    这可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-08
      • 2023-01-17
      • 2011-03-22
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多