我用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中设置名称时,这似乎不起作用。
答案 0 :(得分:0)
以下是我的表现方式:
- (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");
}
}
P.S。 '测试节点'是在我的Player类(也是一个SKSpriteNode)中创建的SKSpriteNode,用于测试触摸是否仍在其上工作。他们这样做。