当用户将两根手指放在屏幕上并抬起一根手指时,我不希望发生任何事情

发布于 2024-09-26 14:23:33 字数 1865 浏览 10 评论 0原文

够了就够了。

正如标题所说,我有点陷入困境。

这是我的问题的概述。我有这艘船。它位于屏幕底部中央 (240,0)。当用户向右触摸大于 240 的点时,船向右移动。如果用户触摸的点小于 240,则向左。但是,当屏幕上一次只有一根手指时,这非常有用。我需要它,如果用户触摸右边,不抬起手指,然后触摸左边(哦,糟糕,屏幕上有两个手指),船会一直向右移动,直到他们抬起右手手指,然后突然船向左移动第二次触摸。

我使用计时器从当前 x 坐标中添加/减去一个数字以使其移动。

简而言之,这就是我的想法:(不要笑或恨我是菜鸟)

-(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;
}

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

只是为了澄清,我希望这种情况发生:

  1. 用户触摸屏幕的右侧
  2. ,船向​​右移动,
  3. 用户用第二根手指触摸屏幕的左侧,
  4. 而船仍在向右移动,因为用户尚未抬起第一次触摸,然后用户抬起第一次触摸
  5. 然后船立即改变航向向左移动到第二次触摸

现在我启用了多点触摸,所以现在它的工作原理如下:

  1. 用户触摸屏幕的右侧
  2. 船向右移动
  3. 用户用第二次触摸屏幕的左侧手指
  4. 船向左移动,然后用户抬起第一个触摸,
  5. 即使用户没有抬起第二次触摸,手指船也会停止在轨道上

enough is enough.

like the title says, I'm in a bit of a pickle.

so here's the outline of my problem. I have this ship right. It rests in the center bottom of the screen (240,0). When the user touches to the right at a point greater than 240 the ship moves right. And left if the user touches at a point less than 240. HOWEVER this is great when there's only one finger on the screen at a time. I need it to where if the user touches right, doesn't lift their finger, then touches left (OH CRAP THERE'S TWO FINGERS ON THE SCREEN) the ship keeps moving right until they lift their right finger, then suddenly the ship moves left towards the second touch.

I'm using timers to add/subtract a number from the current x coordinate to get it to move.

This is what I have in a nutshell: (Don't laugh or hate I'm a noob)

-(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;
}

I'm trying to fully replicate the controls for the game "Radiant" if its any help.
I will seriously love you forever if you help.

Just to clarify, I want this to happen:

  1. user touches the right side of screen
  2. ship moves right
  3. user touches left side of screen with second finger
  4. while the ship is still moving right because the user hasn't lifted their first touch, the user then lifts the first touch
  5. THEN the ship instantly changes course to move left towards the second touch

right now I have multi touch enabled, so now it works like this:

  1. user touches the right side of screen
  2. ship moves right
  3. user touches left side of screen with second finger
  4. ship moves left then user lift first touch
  5. ship is stopped in its tracks even though the user hasn't lifted the second touch

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情泪▽动烟 2024-10-03 14:23:33

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

您可以设置两个全局接触。所以,如果 [touch locationInView:self.view] 位置 blah 是 > 250 全局RightTouch = 触摸。左边也一样。

然后,当您击中右侧时,您将触摸分配给全局右侧,船开始向右移动,然后您同时触摸左侧,因此另一个触摸开始,事情开始了,因此您将其分配给全局左侧触摸。然后你可以这样做: if(!globalRightTouch){start leftTimer) 反之亦然。

在你的 touhcesEnded 函数中 - 如果触摸结束时 leftTimer 不为空,您将告诉 leftTimer 终止 - 因此即使您的左侧触摸仍然按下 - 您也会告诉两个计时器在任何触摸释放时终止。您只需要在触摸结束时更加具体一点即可。

然后在您的触摸结束代码中,您需要执行以下操作:

  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
   }
 }
}

所以本质上您想说 - 如果我放开触摸 - 检查它是哪一个,如果它不为空并且如果有则终止该触摸的计时器是当前屏幕上的另一个触摸 - 启动该侧的计时器。

希望这有帮助,

干杯,

迈克尔

First off, about not activating the leftTimer if the rightTouch is not released.

You could set up two Global Touches. So, if [touch locationInView:self.view] position blah is > 250 globalRightTouch = touch. Same for left.

THen when you hit the right side - you assign the touch to the global Right, and the ship starts to move right -- then you touch the left side simultaneously -- so another touches began thing starts - so you assign it to the globalLeftTouch. Then you can go something like: if(!globalRightTouch){start leftTimer) and vise versa.

In your touhcesEnded function - you are telling the leftTimer to kill if it is not null on touches end - so even if you have your left touch down still -- you are telling both timers to kill on any touch release. You just need to be a little more specific with your touches ended.

Then in your touches ended code you need to go something along the following:

  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
   }
 }
}

so essentially you want to say - If I let go of a touch - check which one it is and kill the timer for that touch if it is not null and if there is another touch currently on the screen - start the timer for that side.

Hope this helps,

Cheers,

Michael

羞稚 2024-10-03 14:23:33

从用户开始触摸到停止触摸,每个 UITouch 都是唯一的。所以你的问题实际上是这句话:

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

你不想只是任何触摸。你想要的触感与你一直在追求的触感相同(如果存在的话)。在这组触球中寻找那个触球,如果不存在,就开始转向另一个触球。

Each UITouch is unique from the time the user starts touching until they stop. So your problem is really this line:

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

You don't want just any touch. You want a touch that is the same touch you've been moving toward, if it exists. Look for that touch in the set, and if it's not there start moving towards another touch.

过期以后 2024-10-03 14:23:33

这是一个真实/伪代码版本 - 希望它有所帮助......

-(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;
        }
    }
}

干杯,

Michael O'Brien

Here is a real/pseudo code version - hope it helps...

-(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;
        }
    }
}

cheers,

Michael O'Brien

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文