我已经坚持这个问题好几天了。我所拥有的是多个SKSpriteNode,一个用于左箭头,右箭头和向上箭头。当我按下右箭头时,我希望我的角色在被按下时继续向右移动,另一方面,如果按下向上箭头,那么无论你是否按住它,你都只会跳一次。 所以我的问题就是当我按住向右箭头,然后我按下向上箭头,触摸被调用并且即使我的手指仍然在右箭头上,它也会阻止我的角色向右移动
-(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中。
答案 0 :(得分:0)
您需要一种方法来识别正在注册触摸的不同节点。有不止一种方法可以做到这一点,但我总是发现使用节点的name属性是最简单和最容易使用的。
通过使用BOOL注册触摸状态,您已经有了正确的想法。
我写了一些代码来处理你想要完成的事情:
#import "GameScene.h"
@implementation GameScene {
SKSpriteNode *node0;
SKSpriteNode *node1;
BOOL node0touch;
BOOL node1touch;
}
-(void)didMoveToView:(SKView *)view {
self.backgroundColor = [SKColor blackColor];
node0 = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(100, 100)];
node0.name = @"node0";
node0.position = CGPointMake(100, 300);
[self addChild:node0];
node1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(100, 100)];
node1.name = @"node1";
node1.position = CGPointMake(400, 300);
[self addChild:node1];
node0touch = false;
node1touch = false;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:touchLocation];
if([node.name isEqualToString:@"node0"])
node0touch = true;
if([node.name isEqualToString:@"node1"])
node1touch = true;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:touchLocation];
if([node.name isEqualToString:@"node0"])
node0touch = false;
if([node.name isEqualToString:@"node1"])
node1touch = false;
}
}
-(void)update:(CFTimeInterval)currentTime {
if(node0touch)
NSLog(@"node0 touch");
if(node1touch)
NSLog(@"node1 touch");
}