在我正在开发的游戏中,您可以通过触摸屏幕的左侧/右侧将角色从左向右移动。我想要做的是,如果您先触摸左侧然后触摸右侧,角色就会开始向右移动而不是向左移动;向左移动的 Action 被覆盖。我目前已经做到了,所以游戏是单点触摸的,但是覆盖以前触摸的方法是我坚持的地方。我的代码:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if (location.x < screenWidth/2){
leftMovement = YES;
[player runLeft];
}
else {
rightMovement = YES;
[player runRight];
}
self.userInteractionEnabled = NO;
}
- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self playerStop];
self.userInteractionEnabled = YES;
}
- (void) update:(NSTimeInterval)currentTime {
if (isTouched && leftMovement){
player.physicsBody.velocity=CGVectorMake(-PLAYERSPEED,
player.physicsBody.velocity.dy);
}
else if (isTouched && rightMovement){
player.physicsBody.velocity=CGVectorMake(PLAYERSPEED,
player.physicsBody.velocity.dy);
}
}
最佳答案
您应该考虑的一件事是同时更改 leftMovement
的值和 rightMovement
当用户触摸屏幕的另一侧时,否则向右移动 update
方法永远不会被调用。
if (location.x < screenWidth/2){
leftMovement = YES;
rightMovement = NO;
[player runLeft];
}
else {
rightMovement = YES;
leftMovement = NO;
[player runRight];
}
关于ios - 覆盖 Spritekit 中的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34518352/