本文介绍了无法点击 SKSpriteNode - 未检测到触摸 ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 iOS、Objective C 和 Xcode 还是很陌生.我只做了一个简单的新闻类应用,但现在我想创建一个我在另一个平台上发布的游戏.

I am pretty new to iOS, objective C and working with Xcode. I only did one simple news type app, but now I would like to create a game I had published on another platform.

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

Basically, I have one object that will appear randomly and then fade out and the purpose is to tap on the object to make it go away.

但我似乎无法点击对象,即使我设置了 mySKSpriteNode.userInteractionEnabled = YES;

However I can't seem to tap the object, even though I set mySKSpriteNode.userInteractionEnabled = YES;

这是我的 touchesBegan 方法中的内容:

Here is what I have in my touchesBegan method:

-(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]];

}

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

In my log I get this when i tap the screen (not where I have objects):

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 时,我在日志中什么也没有得到,甚至没有某物被触摸";

When I touch mySKSpriteNode I get nothing in the log, not even "Something was touched";

知道为什么会发生这种情况吗?

Any idea why this could be happening?

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

The other questions I found said the answer is to set userInteractionEnabled = YES.... maybe there's a specific point in the code where I should set this?..

感谢所有帮助!

推荐答案

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

Have you tried setting the name property of your spritenode?

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]];
    }
}

这篇关于无法点击 SKSpriteNode - 未检测到触摸 ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 22:00