我有以下简单的代码:
//
// 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并检查是否触摸了标签,如果是,则从父节点中删除它们。
奇怪的是,当我将userInteractionEnabled设置为YES时,SKLabelNode将不会收到任何触摸事件。将userInteractionEnabled保留为NO可以正常工作。
也许最好将此属性命名为userInteractionDisabled?我错过了什么吗?
答案 0 :(得分:4)
您的代码正在按预期运行。
据我所知,不是SKLabelNodes接收触摸,而是SKScene有 - (void)touchesBegan ...这意味着如果你添加SKLabelNodes并将它们设置为.userInteractionEnabled = YES那么它们将会浸泡在他们到达现场之前触摸它们,因为它们在场景之上。
否则,您应该继承SKLabelNode并在自定义初始化程序中设置userInteractionEnabled。然后在SKLabelNode的子类中使用touchesBegan。