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

当用户有两个手指在屏幕上,并举起一个,我不想发生任何事情

  •  0
  • Anonymous  · 技术社区  · 15 年前

    就像书名说的,我有点麻烦。

    下面是我问题的概要。我把这艘船弄对了。它位于屏幕的中间底部(240,0)。当用户在大于240的点向右触碰时,飞船向右移动。如果用户在小于240的点上接触,则离开。然而,当屏幕上一次只有一个手指时,这是很棒的。我需要它到这样一个地方,如果用户右碰,不举起手指,然后碰左(哦,天哪,屏幕上有两个手指)飞船一直向右移动直到他们举起右手,然后突然飞船向左移动到第二次触摸。

    简而言之,这就是我所拥有的:(别笑也别恨我是个笨蛋)

    -(void)moveShipLeft:(id)sender {
    
     shipView.center = CGPointMake(shipView.center.x - 2, 320 - (shipView.image.size.height / 2));
    
    }
    -(void)moveShipRight:(id)sender { 
    
     shipView.center = CGPointMake(shipView.center.x + 2, 320 - (shipView.image.size.height / 2));
    
    }
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    
     UITouch *touch = [[event allTouches] anyObject];
     CGPoint location = [touch locationInView:touch.view];
    
     if (240 < location.x){rightTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveShipRight:) userInfo:nil repeats:YES];}
     if (240 > location.x){leftTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveShipLeft:) userInfo:nil repeats:YES];}
    
     [shipView setCenter:shipView.center];
     }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
    
      if (rightTimer != nil)               //Kill the timer that goes right upon touch removal
       [rightTimer invalidate];
      rightTimer = nil;
    
      if (leftTimer != nil)                //Kill the timer that goes left on touch removal
       [leftTimer invalidate];
      leftTimer = nil;
    }
    

    我正在尝试完全复制游戏“辐射”的控制,如果有帮助的话。 如果你能帮助我,我会永远爱你。

    为了澄清一下,我希望这一切发生:

    1. 船向右移动
    2. 然后飞船立即改变航向向左移动到第二个触碰点

    现在我已经启用了多点触控,所以现在它是这样工作的:

    1. 用户触摸屏幕右侧
    2. 船向右移动
    3. 用户用第二个手指触摸屏幕的左侧
    4. 即使用户还没有解除第二次触碰,飞船也会停在它的轨道上
    3 回复  |  直到 15 年前
        1
  •  0
  •   Michael O'Brien    15 年前

    首先,关于如果rightTouch未释放,则不激活leftTimer。

    你可以设置两个全球接触。所以,如果ionInView:self.view]position blah is>250 globalRightTouch=触摸。左边也是。

    然后当你点击右边-你把触摸分配到全局右边,飞船开始向右移动-然后你同时触摸左边-所以另一个触摸开始-所以你把它分配给globalLeftTouch。然后你可以这样说:如果(!globalRightTouch){启动leftTimer),反之亦然。

    在你的touhceseded函数中,如果leftTimer在touchEnd上不为null,你就告诉leftTimer终止它,所以即使你的left touch down仍然存在,你也在告诉两个定时器在任何一个touch release上都要终止。你只需要更具体一点,你的接触结束。

      UITouch *touch = [[event allTouches] anyObject];
    
      if (rightTimer != nil && touch == globalRightTouch){               //Kill the timer that goes right upon touch removal
       [rightTimer invalidate];
      rightTimer = nil;
       if(globalLeftTouch)
         leftTimerStartFunction //I dont know what your variables are sorry
      }
    
      if (leftTimer != nil && touch == globalLeftTouch){                //Kill the timer that goes left on touch removal
       [leftTimer invalidate];
      leftTimer = nil;
       if(globalRightTouch){
          rightTimerStartFunction //I dont know what you variables are sorry
       }
     }
    }
    

    干杯,

    迈克尔

        2
  •  0
  •   Kendall Helmstetter Gelner    15 年前

    从用户开始触摸到停止,每个UITouch都是独一无二的。所以你的问题是这条线:

     UITouch *touch = [[event allTouches] anyObject];
    

    你不想 任何 触摸。你想要的是你一直在追求的触摸,如果它存在的话。在布景中寻找那个触感,如果没有,就开始转向另一个触球。

        3
  •  0
  •   Michael O'Brien    15 年前

    这是一个真实/伪代码版本-希望它能帮助。。。

    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)even{
    UITouch *touch = [touches anyObject];
    
    if(touch.x < 240){
        globalTouchLeft = touch;
        if(!globalTouchRight){
            startLeftTimer; //again I don't know what your timere/variable is
        }
    }else if(touch.x  >= 240){
    
        globalTouchRight = touch;
        if(!globalTouchLeft){
            startRightTimer; //again I don't know what your timere/variable is
        }
    
        }
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
        UITouch *touch = [touches anyObject];
    
        if(touch == globalLeftTouch){
            stopLeftTimer
            globalLeftTouch = nil;
    
            if(globalRightTouch){
                startRightTimer;
            }
    
        }
    
        if(touch == globalTouchRight){
            stopRightTimer
            globalRightTouch = nil;
    
            if(globalLeftTouch){
                startLeftTimer;
            }
        }
    }
    

    干杯,

    迈克尔·奥布赖恩

    推荐文章