0

我已经被这个问题困扰了好几天了。我拥有的是多个 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 中。

4

1 回答 1

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"); 
}
于 2015-03-28T22:46:31.837 回答