我在一个教程中找到了这段代码,如果触摸开始于屏幕的特定一侧,则几乎将左侧或右侧 BOOL 设置为 YES,然后检查触摸何时移动以查看它是否在屏幕上改变侧面以使另一个 BOOL 是的。
所以我现在正在尝试实现多点触控,但我不确定它如何与以下代码一起使用?有谁知道我会怎么做?
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
touchStartPoint = [touch locationInView:self.view];
if (touchStartPoint.x < 160.0) {
touchLeftDown = TRUE;
}
else if (touchStartPoint.x > 160.0) {
touchRightDown = TRUE;
}
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentTouchPoint = [touch locationInView:self.view];
if (touchStartPoint.x < 160.0 && currentTouchPoint.x < 160.0) {
touchLeftDown = TRUE;
}
else if (touchStartPoint.x > 160.0 && currentTouchPoint.x > 160.0)
{
touchRightDown = TRUE;
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
touchLeftDown = touchRightDown = FALSE;
}
谢谢!
编辑1:这些是布尔在游戏循环中所做的事情,我几乎想要实现的是,如果双方同时触摸,TOUCH_INCREMENT 将为 0,因为每一侧的触摸都会取消互相出去。我将如何实现这一目标?无论如何,这是我正在谈论的代码:
if (touchLeftDown == TRUE) {
touchStep -= TOUCH_INCREMENT;
}
else if (touchRightDown == TRUE) {
touchStep += TOUCH_INCREMENT;
}
else {
touchStep = SLOWDOWN_FACTOR * touchStep;
}
touchStep = MIN(MAX(touchStep, -MAX_ABS_X_STEP), MAX_ABS_X_STEP);
pos.x += touchStep;