onEnabled属性在SpriteKit节点上是否可以正常工作

onEnabled属性在SpriteKit节点上是否可以正常工作

本文介绍了userInteractionEnabled属性在SpriteKit节点上是否可以正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单代码:

//
//  BGMyScene.m
//  Test1
//
//  Created by AndrewShmig on 3/10/14.
//  Copyright (c) 2014 Bleeding Games. All rights reserved.
//

#import "BGMyScene.h"

@implementation BGMyScene

- (id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15
                                               green:0.15
                                                blue:0.3
                                               alpha:1.0];

//      first label
        SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
//        myLabel.userInteractionEnabled = YES;
        myLabel.text = @"Hello, World!";
        myLabel.fontSize = 30;
        myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                       CGRectGetMidY(self.frame));
        [self addChild:myLabel];

//      second label
        SKLabelNode *myLabel2 = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
        // myLabel2.userInteractionEnabled = YES;
        myLabel2.text = @"Hello, World!";
        myLabel2.fontSize = 30;
        myLabel2.position = CGPointMake(100, 100);
        [self addChild:myLabel2];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];
    SKNode *touchedNode = [self nodeAtPoint:touchLocation];

    NSLog(@"touchLocation x: %f and y: %f", touchLocation.x, touchLocation.y);

    if (touchedNode != self) {
        NSLog(@"Removed from parent.");
        [touchedNode removeFromParent];
    }
}

- (void)update:(CFTimeInterval)currentTime
{
    /* Called before each frame is rendered */
}

@end

它的作用是创建两个SKLabelNodes并检查是否触摸了标签(如果是)-将其从父节点中删除.

What it does is creating two SKLabelNodes and checks if that labels were touched, if yes - remove them from parent node.

奇怪的是,当我将userInteractionEnabled设置为YES时,SKLabelNode将不会收到任何触摸事件.将userInteractionEnabled保留为NO即可.

The strange thing is that when I set userInteractionEnabled to YES SKLabelNode won't receive any touch event. Leaving userInteractionEnabled to NO works fine.

也许最好将此属性命名为userInteractionDisabled?我有什么想念的吗?

Maybe its better to name this property userInteractionDisabled? Am I missing something about it?

推荐答案

您的代码可以正常运行.

Your code is working as I expect it would.

据我所知,不是SKLabelNodes接收到触摸,而是具有-(void)touchesBegan的SKScene ...这意味着,如果添加SKLabelNodes并将它们设置为.userInteractionEnabled = YES,则它们将浸泡在它们到达场景之前先进行触摸,因为它们位于场景的顶部.

As far as I can see it is not the SKLabelNodes receiving touches but the SKScene which has the -(void)touchesBegan... This means that if you add the SKLabelNodes and set them to .userInteractionEnabled = YES then they will soak up the touches before they reach the scene, because they're on top of the scene.

否则,您应该子类化SKLabelNode并在自定义初始化程序中设置userInteractionEnabled.然后将touchesBegan放在SKLabelNode的子类中.

Otherwise you should subclass SKLabelNode and set userInteractionEnabled in your custom initialiser. And then have the touchesBegan in the subclass of the SKLabelNode.

这篇关于userInteractionEnabled属性在SpriteKit节点上是否可以正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:28