使用SKSpriteNode创建的按钮的右1/3未检测到触摸

时间:2013-11-01 17:12:07

标签: objective-c sprite-kit skspritenode

在我的游戏中,我创建了许多按钮。我发现许多按钮在用户触摸按钮的右侧时没有检测到。换句话说,按钮在按钮的左侧触摸时响应正确,但在右侧不响应。简而言之,如果您将按钮分成三个部分,则按钮的右侧1/3不响应,而按钮的另外两个按钮完全正常工作。下面是我用来实现我的按钮的代码。谁能告诉我我可能做错了什么?当我第一次开始使用Sprite Kit时,我注意到当SKSpriteNode :: zPosition属性未设置为1或更大时我的按钮根本不会响应...一旦我设置了这个属性,我的按钮开始工作。无论按钮精灵节点是否是场景中唯一的精灵节点,这个zPosition问题都会发生,这让我认为问题也不是重叠问题。有什么想法吗?

我已经将SKSpriteNode类扩展为另外一个方法GESpriteNode :: setGestureHandler:withKey:,它具有以下签名:

- (void) setGestureHandler: (void (^)(SKNode *node, GEGestureRecognizer *recognizer))handler withKey: (NSString *)key;
- (void) removeGestureHandlerWithKey: (NSString *)key;

并且,如此实现(其中_event是NSMutableDictionary):

- (void) setGestureHandler: (void (^)(SKNode *node, GEGestureRecognizer *recognizer))handler withKey: (NSString *)key {
    if (key != nil) {
        [_events setObject: [handler copy] forKey: key];
    }
}

- (void) removeGestureHandlerWithKey: (NSString *)key {
    if (key != nil) {
        [_events removeObjectForKey: key];
    }
}

- (void) touchesBegan: (NSSet *)touches withEvent: (UIEvent *)event {
    if (![self isHidden]) {
        void (^handler)(SKNode *, GEGestureRecognizer *);

        handler = [_events objectForKey: GETouchDownGestureHandlerKey];

        if (handler != nil) {
            GEGestureRecognizer *recognizer = [[GEGestureRecognizer alloc] initWithScene: [self scene] withTouches: touches];
            if (recognizer != nil) {
                handler(self, recognizer);
            }
        }
    }
}

- (void) touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
    if (![self isHidden]) {
    void (^handler)(SKNode *, GEGestureRecognizer *);

        handler = [_events objectForKey: GETouchUpGestureHandlerKey];

        if (handler != nil) {
            GEGestureRecognizer *recognizer = [[GEGestureRecognizer alloc] initWithScene: [self scene] withTouches: touches];
            if (recognizer != nil) {
                handler(self, recognizer);
            }
        }
    }
}

它们的用法如下:

GESpriteNode *spriteNode = [GESpriteNode spriteNodeWithImageNamed: NODE_BUTTON_1];
[spriteNode setAlpha: ALPHA_OPAQUE];
[spriteNode setGestureHandler: ^(SKNode *node, GEGestureRecognizer *recognizer) {
    [node setScale: SCALE_95];
} withKey: GETouchDownGestureHandlerKey];
[spriteNode setGestureHandler: ^(SKNode *node, GEGestureRecognizer *recognizer) {
[node setScale: SCALE_100];
} withKey: GETouchUpGestureHandlerKey];
[spriteNode setHidden: NO];
[spriteNode setName: NODE_BUTTON_1];
[spriteNode setPosition: CGPointMake(384.0f, 387.0f)];
[spriteNode setScale: SCALE_100];
[spriteNode setUserInteractionEnabled: YES];
[spriteNode setZPosition: 1];
[self addChild: spriteNode];

我的GEGestureRecognizer.h类定义如下:

#import <SpriteKit/SpriteKit.h>

extern NSString * const GETouchDownGestureHandlerKey;
extern NSString * const GETouchUpGestureHandlerKey;

@interface GEGestureRecognizer : NSObject

// public properties
@property (nonatomic, strong) SKScene *scene;

// public methods
- (instancetype) initWithScene: (SKScene *)scene withTouches: (NSSet *)touches;
- (CGPoint) locationInScene: (SKScene *)scene;

@end

并且,其GEGestureRecognizer.m定义为:

#import "GEGestureRecognizer.h"

#pragma mark -
#pragma mark Constants

NSString * const GETouchDownGestureHandlerKey = @"GETouchDownGestureHandlerKey";
NSString * const GETouchUpGestureHandlerKey = @"GETouchUpGestureHandlerKey";

@interface GEGestureRecognizer () {

    @private
        SKScene *_scene;
        NSSet *_touches;

}

@end

@implementation GEGestureRecognizer

@synthesize scene = _scene;

- (instancetype) initWithScene: (SKScene *)scene withTouches: (NSSet *)touches {
    if ((self = [super init])) {
        _scene = scene;
        _touches = touches;
    }
    return self;
}

- (CGPoint) locationInScene: (SKScene *)scene {
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat k = 0;

    for (UITouch *touch in _touches) {
        const CGPoint point = [touch locationInNode: scene];
        x += point.x;
        y += point.y;
        k++;
    }

    if (k > 0) {
        return CGPointMake(x/k, y/k);
    }

    return CGPointZero;
}

@end

2 个答案:

答案 0 :(得分:0)

这段代码究竟在做什么?为什么要增加x和y值?你为什么需要k值?我认为你正在抵消触摸的位置,尝试记录所有触摸位置并在屏幕上绘制它们 有一些本机方法可以检测节点中的触摸并将坐标转换为场景位置。

答案 1 :(得分:0)

在每个事件上重新创建GestureRecognizer似乎相当低效,locationInScene在多次触摸发生时都没有多大意义(不知道你想要在那里实现什么)

也就是说,如果您的按钮在更改zPosition时才开始工作,我认为您确实存在重叠问题。 由于在(默认)zPosition 0处涉及其他节点(无论是否可见),因此您必须显示更多代码才能获得帮助。