触摸期间未检测到自定义 SKSpriteNode [英] Custom SKSpriteNode not detected during touch

查看:21
本文介绍了触摸期间未检测到自定义 SKSpriteNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习SpriteKit的过程中,我正在尝试制作一个小型冒险游戏.我正在创建一个英雄,并将其添加到场景中,然后在 touchesBegan: 期间,我检测触摸是否源自英雄,并采取相应的行动.

In learning SpriteKit, I am trying to make a small adventure game. I am creating a hero, and adding it to the scene, and then later, during touchesBegan:, I detect if the touch originated on the hero, and take actions accordingly.

如果我将英雄添加为 SKSpriteNode,触摸会检测到他.如果我将他添加为 SKSpriteNode 的子类,则触摸不会!添加的区别:

If I add the hero as a SKSpriteNode the touch detects him. If I add him as a subclass of SKSpriteNode the touch does not! The difference in adding:

_heroNode = [SKSpriteNode spriteNodeWithImageNamed:@"hero.png"];

_heroNode = [[ADVHeroNode alloc] init];

初始化看起来像这样:

- (id) init {
    if (self = [super init]) {

        self.name = @"heroNode";
        SKSpriteNode *image = [SKSpriteNode spriteNodeWithImageNamed:@"hero.png"];
        [self addChild:image];
    }
    return self;
}

将英雄添加为 SKSpriteNode 的子类的工作原理是将其添加到场景中,但触摸不会检测到他.我的 touchesBegan: 看起来像这样:

Adding the hero as a subclass of SKSpriteNode works in the sense that it is added to the scene, but the touch doesn't detect him. My touchesBegan: looks like this:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    if (YES) NSLog(@"Node name where touch began: %@", node.name);

    //if hero touched set BOOL
    if ([node.name isEqualToString:@"heroNode"]) {
        if (YES) NSLog(@"touch in hero");
        touchedHero = YES;
    }
}

令人沮丧的是,当添加一个直接的 SKSpriteNode 时,这段代码工作得很好,而不是我自己的子类.有什么建议吗?

Frustratingly, this code works just fine when adding a straight up SKSpriteNode, and not my own subclass of it. Any suggestions?

推荐答案

这里有一些示例方法来继承你的 Hero

Here are some example ways to subclass your Hero

SKNode

子类化 SKNode 这个方法需要你的节点来监控触摸,因此需要 self.userInteractionEnabled 属性.

Subclass an SKNode this method requires your node to monitor for touches, hence the self.userInteractionEnabled property.

@implementation HeroSprite

- (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(@"Hit at, %@ %@", NSStringFromCGPoint(locationA), NSStringFromCGPoint(locationB));
}

@end

SKSpriteNode

另一种更简单"的方式,如果您只想继承 SKSpriteNode.这将基本上和你习惯的一样工作(在你想继承你的英雄之前).因此,您在问题中设置的 touchesBegan 将起作用.

The other "easier" way, if you just want to subclass a SKSpriteNode. This will work essentially the same as you are use to (before you wanted to subclass your Hero). So your touchesBegan, as set up in your question will work.

@implementation HeroSprite

- (id) init {
    if (self = [super init]) {
        self = [HeroSprite spriteNodeWithImageNamed:@"Spaceship"];
        [self setUpHeroDetails];

    }
    return self;
}

-(void) setUpHeroDetails {
    self.name = @"heroNode";
}

@end

这篇关于触摸期间未检测到自定义 SKSpriteNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆