gpt4 book ai didi

ios - 无法点击 SKSpriteNode - 未检测到触摸 ios

转载 作者:可可西里 更新时间:2023-11-01 03:26:55 25 4
gpt4 key购买 nike

我对 iOS、Objective 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 时,我在日志中什么也没有得到,甚至没有“Something was touched”;

知道为什么会这样吗?

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

感谢所有帮助!

最佳答案

您是否尝试过设置您的 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]];
}
}

关于ios - 无法点击 SKSpriteNode - 未检测到触摸 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21840042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com