Cocos2d:一层在另一层之上 - 是否可以暂时停用特定层/区域的触摸?
我有一个场景,里面有一个按钮。单击按钮后,一个矩形精灵会从左侧滑入: http://img255 .imageshack.us/img255/9867/slidei.png
在这个形状下,有几个触摸感应按钮。我不希望当我触摸矩形时调用这些。因此,只要形状保留在屏幕上,这些触摸就不会响应。相反,在那个棕色形状的顶部还有几个其他按钮,可以对触摸做出反应。我该如何处理?
是否可以暂时停用场景中特定图层的触摸?矩形本身就是 CCLayer 对象吗?
我知道我可以为具有透明背景的形状创建一个新场景,但我仍然希望按钮对触摸做出反应:
当我单击该按钮时,形状会滑入。当我再次单击它时,它会滑动离开屏幕。
I have a scene where there is a button. Once I click the button, a rectangular sprite slides in from the left side: http://img255.imageshack.us/img255/9867/slidei.png
Under this shape, there are several touch sensitive buttons. I don't want these to be called when I touch on the rectangular shape. So, as long as the shape remains on the screen, those touches should not respond. Instead, there are several other buttons on top of that brown shape, that respond to touches. How can I manage that?
Is it possible to temporarily deactivate touches for a certain layer in a scene? Has the rectangular shape to be a CCLayer Object on its own?
I know I could create a new scene for that shape that has a transparent background, BUT I still want the button to react to touches:
When I click that button, the shape slides in. When I click it again, it slides off the screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取决于您是否使用您的 TouchesBegan 方法响应的实际按钮对象或 CGRect 区域。我同意 GamingHorror 的观点,即最干净的方法是根据需要直接启用/禁用按钮对象。
然而,只要滑动触摸区域是 UIView 子类,这就是一个可行的拼凑。
在它滑入之前,您可以使用以下命令禁用所有用户触摸:
[[[UIApplication sharedApplication] keyWindow] setUserInteractionEnabled:NO];
视图滑入后,您可能需要在该特定视图上 setUserInteractionEnabled:YES。当它滑出时,您可以使用
[[[UIApplication sharedApplication] keyWindow] setUserInteractionEnabled:YES] 将其全部放回去;
Depends on if you are using actual button objects or CGRect regions that your touchesBegan method responds to. I agree with GamingHorror that the cleanest approach would be to enable/disable the button objects directly as needed.
However, this is a workable kludge as long as the sliding touch region is a UIView subclass.
Before it slides in you can disable all user touches with:
[[[UIApplication sharedApplication] keyWindow] setUserInteractionEnabled:NO];
After your view slides in, you may need to setUserInteractionEnabled:YES on that particular view. When it slides out, you can put it all back with
[[[UIApplication sharedApplication] keyWindow] setUserInteractionEnabled:YES];
与任何用户界面一样,您必须让对象知道它们是否已启用。实际上,最好的选择是向按钮发送一条消息,告诉它自行打开或关闭。
理想情况下,您将使用全局触摸输入处理程序,而不是允许每个单独的按钮或幻灯片自行对输入做出反应。这意味着很多麻烦和额外的工作。相反,将所有应该对输入做出反应的对象放在同一层上,并将该层注册到触摸输入处理程序,然后该处理程序会将所有触摸事件转发到该特定层,而不是其他层。
Like with any user interface, you have to let the objects know whether they are enabled or not. Your best bet is actually to send a message to the button, telling it to turn itself on or off.
Ideally you'll be using a global touch input handler, instead of allowing each individual button or slide to react inputs by themselves. This spells a lot of trouble and extra work. Instead, put all objects that should react to input on the same layer, and register that layer with the touch input handler, which will then forward all touch events to that particular layer and no other.