为什么不通过sprite工具包中的touchesBegan允许我访问精灵名称?

时间:2022-04-10 16:44:50

When I check my touch event with a breakpoint I notice the touched node that I detect with the following code doesn't have a .name. Any help? Any other way I can possibly check which node I've touched. I want my nodes to represent parts of the body and believe creating physics bodies for each body part might slow the app down.

当我用断点检查我的触摸事件时,我注意到我用以下代码检测到的触摸节点没有.name。有帮助吗?我可以用任何其他方式检查我触摸过哪个节点。我希望我的节点能够代表身体的各个部分,并且相信为每个身体部位创建物理身体可能会减慢应用程序的速度。

The following is my code. Not very complex:

以下是我的代码。不是很复杂:

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];


    //if touched node equal to one of these then ...
    if([touchedNode.name isEqualToString:@"head.png"]){
        [self alert:@"User has touched the head: " Message:@"Thank You."];
    }

}

1 个解决方案

#1


2  

SKNode's name property is not set by call SKSpriteNode initWithImageNamed: Name's are purely for game logic. From SKNode reference guide.

SKNode的name属性不是通过调用SKSpriteNode initWithImageNamed来设置的:Name是纯粹用于游戏逻辑的。来自SKNode参考指南。

This property is used to identify a node in other parts of your game logic. For example, you might use this name as part of collision testing. You can also search for nodes in a tree by their name.

此属性用于标识游戏逻辑的其他部分中的节点。例如,您可以将此名称用作碰撞测试的一部分。您还可以按名称搜索树中的节点。

If you haven't explicitly set the name property of an SKNode it will not be set.

如果您没有显式设置SKNode的name属性,则不会设置它。

#1


2  

SKNode's name property is not set by call SKSpriteNode initWithImageNamed: Name's are purely for game logic. From SKNode reference guide.

SKNode的name属性不是通过调用SKSpriteNode initWithImageNamed来设置的:Name是纯粹用于游戏逻辑的。来自SKNode参考指南。

This property is used to identify a node in other parts of your game logic. For example, you might use this name as part of collision testing. You can also search for nodes in a tree by their name.

此属性用于标识游戏逻辑的其他部分中的节点。例如,您可以将此名称用作碰撞测试的一部分。您还可以按名称搜索树中的节点。

If you haven't explicitly set the name property of an SKNode it will not be set.

如果您没有显式设置SKNode的name属性,则不会设置它。