将userInteractionEnabled设置为YES的sprite在正常精灵覆盖时不会接收到触摸

Jon*_*nny 8 objective-c sprite-kit

我放了一个精灵A(子类接收命中(userInteractionEnabled为YES)),然后是一个正常的精灵B,它不会在其上面点击(userInteractionEnabled默认为NO),完全覆盖精灵A.

点击精灵B,我认为精灵A会得到触摸,但没有任何反应.关于此事的文档部分如下.

我觉得这里有些东西不清楚,因为似乎精灵B仍然接受了触摸,但却抛弃了它.或者,spriteA从可能的触摸接收器中移除,因为它不可见.

来自文档:https: //developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html#//apple_ref/doc/uid/TP40013043-CH3-SW7

对于在命中测试期间要考虑的节点,其userInteractionEnabled属性必须设置为YES.除场景节点外,任何节点的默认值均为NO.想要接收事件的节点需要从其父类(iOS上的UIResponder和OS X上的NSResponder)实现适当的响应方法.这是您必须在Sprite Kit中实现特定于平台的代码的少数几个地方之一

有任何解决这个问题的方法吗?只要某个用户的userInteractionEnabled为NO,它就不会干扰其他触摸接收器.

更新:即使将精灵B的alpha设置为0.2,使精灵A非常可见,也不会使精灵A可触摸.Sprite B完全"吞下"了触摸,尽管没有启用交互.

bob*_*off 10

这是我的解决方案,直到Apple以正确的行为更新SpriteKit,或者有人正确地想出如何使用它.

https://gist.github.com/bobmoff/7110052

将文件添加到项目中并将其导入前缀标头中.现在接触应该按照预期的方式工作.


Dog*_*fee 1

这是获取触摸等的示例。

现场

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        HeroSprite *newHero = [HeroSprite new];
        newHero.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        [self addChild:newHero];

        SKSpriteNode *overLapSprite = [SKSpriteNode spriteNodeWithColor:[UIColor orangeColor] size:CGSizeMake(70, 70)];
        overLapSprite.position = CGPointMake(CGRectGetMidX(self.frame) + 30, CGRectGetMidY(self.frame));
        overLapSprite.name = @"overlap";
        [self addChild:overLapSprite];
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if ([node.name isEqualToString:@"heroNode"]) {
        NSLog(@"Scene detect hit on hero");
    }
    if ([node.name isEqualToString:@"overlap"]) {
        NSLog(@"overlap detect hit");
    }

    // go through all the nodes at the point
    NSArray *allNodes = [self nodesAtPoint:location];
    for (SKNode *aNode in allNodes) {
        NSLog(@"Loop; node name = %@", aNode.name);
    }
}
Run Code Online (Sandbox Code Playgroud)

子类精灵/英雄

- (id) init {
    if (self = [super init]) {
        [self setUpHeroDetails];
        self.userInteractionEnabled = YES;
    }
    return self;
}

-(void) setUpHeroDetails
{
    self.name = @"heroNode";
    SKSpriteNode *heroImage = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
    [self addChild:heroImage];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint locationA = [touch locationInNode:self];
    CGPoint locationB = [touch locationInNode:self.parent];
    NSLog(@"Hero got Hit at, %@ %@", NSStringFromCGPoint(locationA), NSStringFromCGPoint(locationB));
}

@end
Run Code Online (Sandbox Code Playgroud)

尝试了很多方法,但触摸并没有穿过重叠的精灵。我想您可以使用场景类通过循环节点来检测触摸,然后直接调用该节点。

当粒子发射器覆盖按钮时我遇到了这个问题......