无法点击SKSpriteNode - 没有触摸检测到的ios

时间:2014-02-17 21:55:05

标签: ios objective-c sprite-kit skspritenode sknode

我是iOS的新手,目标C和使用Xcode。我只做了一个简单的新闻类型的应用程序,但现在我想创建一个我在另一个平台上发布的游戏。

基本上,我有一个随机出现然后逐渐淡出的对象,其目的是点击对象使其消失。

然而,即使我设置mySKSpriteNode.userInteractionEnabled = YES;

,我似乎也无法点按该对象

以下是我在touchesBegan方法中的内容:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    NSLog(@"Something was touched");
    UITouch *touch = [touches anyObject];
    NSLog(@"%@", touch);
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    if(node == mySKSpriteNode)
    [node runAction:[SKAction fadeOutWithDuration:0]];

}

在我的日志中,当我点击屏幕时(不是我有对象的地方),我得到了这个:

2014-02-17 23:18:30.870 BubbleChallenge[48541:70b] Something was touched
2014-02-17 23:18:30.875 BubbleChallenge[48541:70b] <UITouch: 0x1130ea530> phase: Began tap count: 1 window: <UIWindow: 0x109c2ab60; frame = (0 0; 320 568); autoresize = W+H; gestureRecognizers = <NSArray: 0x109c274d0>; layer = <UIWindowLayer: 0x109c26810>> view: <SKView: 0x109c2e5e0; frame = (0 0; 320 568); autoresize = RM+BM; layer = <CAEAGLLayer: 0x109c0e180>> location in window: {147.5, 128.5} previous location in window: {147.5, 128.5} location in view: {147.5, 128.5} previous location in view: {147.5, 128.5}

当我触摸mySKSpriteNode时,我在日志中什么都没有,甚至没有“触及某些东西”;

知道为什么会这样吗?

我发现的其他问题说答案是设置userInteractionEnabled = YES ....也许在代码中我应该设置一个特定的点?...

感谢所有帮助!

4 个答案:

答案 0 :(得分:10)

您是否尝试过设置spritenode的name属性?

mySKSpriteNode的初始化中:

mySKSpriteNode = [[SKSpriteNode alloc] initWithTexture:sometexture];
mySKSpriteNode.name = @"thisIsMySprite"; // set the name for your sprite
mySKSpriteNode.userInteractionEnabled = NO; // userInteractionEnabled should be disabled

然后:

-(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:@"thisIsMySprite"]) {
        NSLog(@"mySKSpriteNode was touched!");
        [node runAction:[SKAction fadeOutWithDuration:0]];
    }
}

答案 1 :(得分:5)

您的代码基本上是正确的。只需要进行一些小的改动。

您已在场景类中实现了-touchesBegan:方法。如果要处理场景类中的触摸,则所有子节点都应将userInteractionEnabled属性设置为NO。精灵节点的userInteractionEnabled属性只有在能够自己处理触摸时才应设置为YES

因此,在您的情况下,您应该进行以下更改:

1 - 将spriteNode的userInteraction设置为NO。

mySKSpriteNode.userInteractionEnabled = NO;

2 - 更改检查自己的精灵的方式。

if([node isEqual: mySKSpriteNode])

您会发现您的代码按预期运行。

答案 2 :(得分:1)

在Swift 2.0中!

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

  let touch = touches.first!
  let location = touch.locationInNode(self)
  let node = self.nodeAtPoint(location)

  if node.name == "spriteName" {
    // Present new game scene
  }
}

答案 3 :(得分:0)

我从未使用过SKSprite框架,但我会尝试提供帮助。 首先,你的函数&#34 ;-(void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *)event&#34;必须在您的自定义SKSpriteNode类中设置,否则它是正常的,如果它不起作用。

如果已经完成,你必须搜索点击处理的位置(通过其他对象)然后你可以使用:&#34; - (BOOL)gestureRecognizer:(UIPanGestureRecognizer )gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer )otherGestureRecognizer&#34;

希望这会有所帮助!

相关问题