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

如何检测触摸动作是否在缩放状态下接触精灵?

  •  1
  • Sadat  · 技术社区  · 15 年前

    首先,我将包含精灵的层放大。 现在我需要感觉到一个精灵的触摸。 我试过以下方法,但达不到目标-

    CGRect tRect= [[aSprite displayedFrame] rect];
        if(CGRectContainsPoint(tRect, touchedPosition))
    {
        NSLog(@"touched:>> touch at (%f,%f)",touchedPosition.x,touchedPosition.y);
        // Do something, maybe return kEventHandled;
    }
    else{
        NSLog(@"NOT touched: touch at (%f,%f)",touchedPosition.x,touchedPosition.y);
    }
    

    仅供参考:我使用了cocos2d框架

    3 回复  |  直到 15 年前
        1
  •  1
  •   Frank Mitchell    15 年前

    首先,您需要确保从 UITouch 正确地。

    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    

    第二,你需要在精灵的边界框上测试你的触摸。

    if (CGRectContainsPoint([sprite boundingBox], location)) {
        // The sprite is being touched.
    }
    
        2
  •  0
  •   cc.    15 年前

    弗兰克·米切尔是对的。另一种方法是将您的监听代码添加到sprite本身,以便cocos为您完成工作。它只会发送sprite cctouchesbegan事件,如果它实际被触摸的话。

        3
  •  0
  •   Sadat    15 年前

    最后我找到了解决方案:),这是代码

    CGPoint location = [touch locationInView: [touch view]];
    CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
    
    CGPoint tapPosition = [self convertToNodeSpace:convertedLocation];
    

    其中“self”是我前面指定的sprite holder层。这一层正在监听触摸事件。

    推荐文章