【问题标题】:Multitouch don't work in cocos2d多点触控在 cocos2d 中不起作用
【发布时间】:2024-01-18 19:12:01
【问题描述】:

这是我的 ccTouchesMoved 方法。 怎么了?我使用 cocos2d 框架。

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

CCNode *sprite = [self getChildByTag:kTagPlayer];
CCNode *sprite2 = [self getChildByTag:kTagEnemy];

CGPoint point;

//Собрать все касания.
NSSet *allTouches = [event allTouches];

for (UITouch *touch in allTouches)
{
    point = [touch locationInView:[touch view]];
    point = [[CCDirector sharedDirector] convertToGL:point];

    if (point.y > 384)
    {
        if (point.x > 992)
            sprite2.position = ccp(992, size.height - 100);
        else if (point.x < 32)
            sprite2.position = ccp(32, size.height - 100);
        else
            sprite2.position = ccp(point.x, size.height - 100);
    }
    else
    {
        if (point.x > 992)
            sprite.position = ccp(992, 100);
        else if (point.x < 32)
            sprite.position = ccp(32, 100);
        else
            sprite.position = ccp(point.x, 100);
    }
} 

}

【问题讨论】:

  • 你确定你的意思是 point.x > 992?这看起来相当大,而且只能在 iPad 屏幕上工作。即使这样,考虑到 1024 的最大宽度(横向模式),这仍然是一个非常小的触摸区域。

标签: iphone cocos2d-iphone multi-touch


【解决方案1】:

您是否在 glView 中启用了多次触摸?默认情况下,glView 在应用程序委托中实例化。代码如下。

[glView setMultipleTouchEnabled:YES];

【讨论】:

    【解决方案2】:

    如果您正在开发 Retina 显示屏应用,请注意所有坐标都以点为单位,而不是像素。因此,即使在 960x640 像素的 Retina 显示屏上,以点为单位的坐标(您的触摸位置)也将在 480x320 范围内。

    如果要使用像素,请使用所有坐标的“InPixels”版本,在这种情况下:

    sprite.positionInPixels = ccp(992, 100);
    

    如果这不是问题,您应该在帖子中添加预期结果是什么以及会发生什么。一点点上下文有很长的路要走。

    【讨论】:

      【解决方案3】:

      调试器说 allTouches 中的内容是什么?您可以尝试像这样获取视图的所有内容:

      UITouch* touch = [touches anyObject];
      NSSet* allTouches = [touches setByAddingObjectsFromSet:[event touchesForView:[touch view]]];
      

      【讨论】:

        最近更新 更多