问题描述
我是iOS的新手,目标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
方法:
-(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的名称
属性?
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!