当用户将两根手指放在屏幕上并抬起一根手指时,我不希望发生任何事情
够了就够了。
正如标题所说,我有点陷入困境。
这是我的问题的概述。我有这艘船。它位于屏幕底部中央 (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”的控件(如果有帮助的话)。 如果你能帮忙,我会永远爱你。
只是为了澄清,我希望这种情况发生:
- 用户触摸屏幕的右侧
- ,船向右移动,
- 用户用第二根手指触摸屏幕的左侧,
- 而船仍在向右移动,因为用户尚未抬起第一次触摸,然后用户抬起第一次触摸
- 然后船立即改变航向向左移动到第二次触摸
现在我启用了多点触摸,所以现在它的工作原理如下:
- 用户触摸屏幕的右侧
- 船向右移动
- 用户用第二次触摸屏幕的左侧手指
- 船向左移动,然后用户抬起第一个触摸,
- 即使用户没有抬起第二次触摸,手指船也会停止在轨道上
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:
- user touches the right side of screen
- ship moves right
- user touches left side of screen with second finger
- while the ship is still moving right because the user hasn't lifted their first touch, the user then lifts the first touch
- 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:
- user touches the right side of screen
- ship moves right
- user touches left side of screen with second finger
- ship moves left then user lift first touch
- ship is stopped in its tracks even though the user hasn't lifted the second touch
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,如果 rightTouch 未释放,则不激活 leftTimer。
您可以设置两个全局接触。所以,如果 [touch locationInView:self.view] 位置 blah 是 > 250 全局RightTouch = 触摸。左边也一样。
然后,当您击中右侧时,您将触摸分配给全局右侧,船开始向右移动,然后您同时触摸左侧,因此另一个触摸开始,事情开始了,因此您将其分配给全局左侧触摸。然后你可以这样做: if(!globalRightTouch){start leftTimer) 反之亦然。
在你的 touhcesEnded 函数中 - 如果触摸结束时 leftTimer 不为空,您将告诉 leftTimer 终止 - 因此即使您的左侧触摸仍然按下 - 您也会告诉两个计时器在任何触摸释放时终止。您只需要在触摸结束时更加具体一点即可。
然后在您的触摸结束代码中,您需要执行以下操作:
所以本质上您想说 - 如果我放开触摸 - 检查它是哪一个,如果它不为空并且如果有则终止该触摸的计时器是当前屏幕上的另一个触摸 - 启动该侧的计时器。
希望这有帮助,
干杯,
迈克尔
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:
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
从用户开始触摸到停止触摸,每个 UITouch 都是唯一的。所以你的问题实际上是这句话:
你不想只是任何触摸。你想要的触感与你一直在追求的触感相同(如果存在的话)。在这组触球中寻找那个触球,如果不存在,就开始转向另一个触球。
Each UITouch is unique from the time the user starts touching until they stop. So your problem is really this line:
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.
这是一个真实/伪代码版本 - 希望它有所帮助......
干杯,
Michael O'Brien
Here is a real/pseudo code version - hope it helps...
cheers,
Michael O'Brien