2

I'm building a sprite kit game and have created a UIView with collection views and buttons to represent in-game inventory. From my past experience with UIView, if I add one on top of another, the top UIView would intercept touches, unless it's user interaction enabled is set to NO.

However, when I add my inventory UIView to Scene.view with user interaction set to YES, it appears that sprite nodes in my scene beneath the UIView still receive touches, or at least nodes that have user interaction enabled set to YES do.

As a result, it is possible for the user to interact with my inventory UIView, and still cause actions on the game board under the inventory. It appears that Kobold-Kit based button behaviors also act randomly with ones on one side of the screen being tappable through a UIView, while others are not. My other nodes that have touchesBegan: code do ignore the UIView on top of them.

I even tried to add another node on top of the existing scene, so it would capture all touches, but this does not seem to help in this case.

I would appreciate if someone explains how to properly combine UIViews with SKScene and SKSpriteNodes if I want some of my nodes to be interactive.

  • Do I need a separate scene for the inventory?
  • Is there a way to disable passing of touches to the scene while inventory is active?

Here's what I have so far:

-(void)didMoveToView:(SKView *)view {
    [super didMoveToView:view];

        messageConsole = [[MyTextView alloc] initWithFrame:CGRectMake(playerPortraitOffset, 768-75, 415, 75)];

    messageConsole.backgroundColor = [UIColor clearColor];
    messageConsole.font = [UIFont boldSystemFontOfSize:10];
    messageConsole.textColor = [UIColor lightGrayColor];
    messageConsole.editable = NO;
    [self.view addSubview:messageConsole];

    [self.view addSubview:inventoryViewController.view];


}

UPDATE: Tested Kobold Kit's Behavior with following results:

  • For Top level UI elements, like Label, collection View and UIButton, they do not pass touches through
  • For regular UIViews and UIImageViews, they pass touches to the scene EVEN IF there is an interactive UIButton beneath them. Removing UIImage and exposing the same UIButton seems to stop touches from passing through.
4

1 回答 1

2

对所有交互节点使用 KKButtonBehavior 可以解决这个问题。

userInteractionEnabled否则,在显示 UIKit 库存时尝试将 SKView 设置为 NO。它还应该阻止具有该属性集的其他节点接收触摸事件。如果不是,并且您不想使用 KKButtonBehavior,则必须将每个节点设置userInteractionEnabled为 NO 以忽略触摸事件,或者添加您自己的标志,以便节点知道在库存增加时忽略触摸事件。

-(void)preventUserInteraction:(BOOL)prevent
{
//Both seem to disable touches passed to Kobold Kit behaviors under the UIView
    self.userInteractionEnabled = !prevent;


    //self.scene.userInteractionEnabled = !prevent;

}
于 2013-12-19T16:02:33.670 回答