0

我有一个乒乓球比赛,我用手指移动桨。当有一根手指时,一切都很顺利。但是当我想控制两个玩家,两个桨时,一个桨移动得很好,但另一个桨移动很慢,如果有的话。当第二个桨开始移动时,我的第一个桨冻结。如何让两个动作都感觉流畅且反应灵敏?

我在我的 Director 中启用了多点触控。

这是我的触摸代码:

- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    CGRect leftTouchZone = CGRectMake(0, 0, 50, 320);
    CGRect rightTouchZone = CGRectMake(430, 0, 50, 320);

    if (CGRectContainsPoint(leftTouchZone, location))
    {
        CGPoint tempLoc = location;
        tempLoc.x = paddle1.position.x;
        paddle1.position = tempLoc;
    }

    if (CGRectContainsPoint(rightTouchZone, location))
    {
        CGPoint tempLoc = location;
        tempLoc.x = paddle2.position.x;
        paddle2.position = tempLoc;
    }
4

1 回答 1

1

您不应该查看所有触摸对象而不是抓住任何对象吗?如果您同时移动 2 次触摸,则只有一次会获得 touches 移动事件。

- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch* myTouch in touches)
    {
        CGPoint location = [myTouch locationInView:[myTouch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];
        CGRect leftTouchZone = CGRectMake(0, 0, 50, 320);
        CGRect rightTouchZone = CGRectMake(430, 0, 50, 320);

        if (CGRectContainsPoint(leftTouchZone, location))
        {
            CGPoint tempLoc = location;
            tempLoc.x = paddle1.position.x;
            paddle1.position = tempLoc;
        }

        if (CGRectContainsPoint(rightTouchZone, location))
        {
            CGPoint tempLoc = location;
            tempLoc.x = paddle2.position.x;
            paddle2.position = tempLoc;
        }
    }
于 2012-08-05T16:13:14.357 回答