【问题标题】:SpriteKit/Objective-C - Touch detection inside nodesSpriteKit/Objective-C - 节点内的触摸检测
【发布时间】:2016-03-02 22:37:53
【问题描述】:

我正在使用 Objective-C 在 SpriteKit 中制作游戏。我有一个继承 SKNode 的类:

@interface Card : SKNode

然后我在这个类中声明了 SKSpriteNodes 并将它们添加为子类:

cardSprite = [SKSpriteNode spriteNodeWithImageNamed:fileName]; //fileName corresponds with an image asset
[self addChild:cardSprite];

然后我创建一个 Card 对象并将其作为子对象添加到我的主 GameScene 中。我想知道如何在 Card 对象内的 SKSpriteNode 上进行触摸检测。通常我会为每个节点使用一个名称来进行触摸检测,但是当名称是从 Card 对象内部而不是在 GameScene 中设置时,这似乎不起作用。

【问题讨论】:

标签: ios objective-c sprite-kit


【解决方案1】:

我是这样做的:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    [self handleTouchedPoint:location]; // Cleans 'touchesBegan' method by carrying over needed code elsewhere.
}

/** Handle touches. */
- (void)handleTouchedPoint:(CGPoint)touchedPoint {
    SKNode *touchedNode = [self nodeAtPoint:touchedPoint];

    // Detects which node was touched by utilizing names.
    if ([touchedNode.name isEqualToString:@"God"]) {
        NSLog(@"Touched world");
    }
    if ([touchedNode.name isEqualToString:@"Tiles"]) {
        NSLog(@"Touched map");
    }
    if ([touchedNode.name isEqualToString:@"Player Character"]) {
        NSLog(@"Touched player");
    }
    if ([touchedNode.name isEqualToString:@"Test Node"]) {
        NSLog(@"Touched test node");
    }
}

附: “测试节点”是在我的 Player 类(也是 SKSpriteNode)中创建的 SKSpriteNode,用于测试触摸是否仍然有效。他们有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多