【发布时间】:2015-06-23 23:01:37
【问题描述】:
我希望我的一些 Sprite Kit 节点表现得像 UIButtons。我尝试了两种方法:
1) 使用touchesBegan: - 如果用户很小心,这会起作用,但似乎会触发多次,比我禁用交互的速度要快,导致按钮可以被多次激活:
spriteNode.userInteractionEnabled = YES;
//causes the following to fire when node receives touch
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.userInteractionEnabled = NO;
DLog(@"Do node action");
self.userInteractionEnabled = YES;
}
2)我切换到Kobold-Kit 作为 iOS Sprite Kit 之上的层。它允许我做的一件事是将button - like behavior 添加到任何节点。但是,我遇到了一个问题,如果我有 2 个按钮堆叠在一起,点击顶部按钮会同时激活这两个按钮。在这种情况下,布尔标志可以防止重复交互。我在 KKScene 中跟踪了堆叠按钮一起触发此调用的问题:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
for (id observer in _inputObservers)
{
if ([observer respondsToSelector:@selector(touchesBegan:withEvent:)])
{
[observer touchesBegan:touches withEvent:event];
}
}
}
场景只是向场景中的所有节点发送通知。这会导致堆叠在一起的按钮行为一起触发。
有没有一种方法或示例可以显示如何正确排列精灵节点以允许类似于 UIButton 的行为,其中我只能激活顶部按钮,并且每次激活都会短暂禁用按钮之后的时间?
【问题讨论】:
-
使用选项 1,如果您使用本地布尔值而不是 userInteractionEnabled,会发生什么,可能会更快?
-
我都试过了,似乎touchesBegan: 可以忽略两者。
标签: ios objective-c uibutton sprite-kit kobold-kit