我目前使用的游戏手柄可以在触摸开始和移动时移动玩家。问题是:如果我触摸Joypad并继续触摸它而不移动我的手指或释放它,我怎样才能让玩家继续前进? 这是一些代码:
- (void)updateVelocity:(CGPoint)point {
// Calculate distance and angle from the center.
CGRect frame = _player.image.frame;
float dx = point.x - padCenter.x;
float dy = point.y - padCenter.y;
frame.origin.x += dx/10;
frame.origin.y += dy/10;
if (!(frame.origin.x < 0 || frame.origin.y < 0 || frame.origin.x > SCREEN_SIZE_X - frame.size.width || frame.origin.y > SCREEN_SIZE_Y - frame.size.height)) //if the players won't exit the screen then move it
_player.image.frame = frame;
}
这是为了移动玩家。
当触摸开始/移动时
-(void)touchesBegan :(CGPoint )point{ //same as moved
if (isPointInCircle(point, padCenter , JOYSTICK_RADIUS)){
isPressed = YES;
[self updateVelocity:point];
}
}
这是viewcontroller的touchesBegan方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //same as Moved
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: [touch view]];
[joypad touchesBegan:point];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[joypad touchesEnded];
}
就像我说的,如果我在没有移动手指的情况下触摸Joypad的区域,玩家只会移动一次。有什么想法让它一直移动直到我松开手指?