我已经被这个问题困扰了好几天了。我拥有的是多个 SKSpriteNode,一个用于左箭头、右箭头和上箭头。当我按住右箭头时,我希望我的角色在被按住的同时继续向右移动,另一方面,如果你按下向上箭头,那么无论你是否按住它,你都只会跳一次。所以我的问题例如是当我按住右箭头然后按向上箭头时,会调用 touchesEnded 并且它会阻止我的角色向右移动,即使我的手指仍然在右箭头上
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
for (UITouch *touch in touches){
CGPoint location = [touch locationInNode:self];
if (CGRectContainsPoint(rightArrow.frame, location)){
[wizard setTexture:[SKTexture textureWithImageNamed:@"wizardRight"]];
didTouchRightArrow = YES;
isLookingRight = YES;
isLookingLeft = NO;
rightArrow.alpha = 0.5;
NSLog(@"Touching right");
}
if (CGRectContainsPoint(leftArrow.frame, location)){
[wizard setTexture:[SKTexture textureWithImageNamed:@"wizardLeft"]];
isLookingRight = NO;
isLookingLeft = YES;
didTouchLeftArrow = YES;
leftArrow.alpha = 0.5;
NSLog(@"Touching left");
}
if (CGRectContainsPoint(upArrow.frame, location)){
didTouchUpArrow = YES;
upArrow.alpha = 0.5;
NSLog(@"Touching up");
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
if (rightArrow.alpha != 1.0){
rightArrow.alpha = 1.0;
}
if (leftArrow.alpha != 1.0){
leftArrow.alpha = 1.0;
}
if (upArrow.alpha != 1.0){
upArrow.alpha = 1.0;
for (UITouch *touch in touches){
CGPoint location = [touch locationInNode:self];
if (CGRectContainsPoint(rightArrow.frame, location)){
NSLog(@"Touching right");
didTouchRightArrow = YES;
} {
NSLog(@"Not touching right");
didTouchRightArrow = NO;
}
if (CGRectContainsPoint(leftArrow.frame, location)){
NSLog(@"Touching Left");
didTouchLeftArrow = YES;
} else {
NSLog(@"not touching left");
didTouchLeftArrow = NO;
}
didTouchUpArrow = NO;
}
这可能不是解决问题的正确方法,但在 touchesEnded 中,我试图查看触摸是否仍在所需的 Rect 中。