【问题标题】:Test is touchesMoved: still inside SKSpriteNode测试是 touchesMoved:仍在 SKSpriteNode 内
【发布时间】:2014-07-25 17:47:25
【问题描述】:

更新
我已经用以下代码解决了这个问题:

- (BOOL) isInside:(NSSet *)touches
{
    //NSLog(@"%s", __PRETTY_FUNCTION__);

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];
    BOOL isInside = NO;

    //NSLog(@"touchLocation %@", NSStringFromCGPoint(touchLocation) );
    //NSLog(@"self.position %@", NSStringFromCGPoint(self.position) );
    //NSLog(@"self.frame    %@", NSStringFromCGRect(self.frame) );
    //NSLog(@"self.size     %@", NSStringFromCGSize(self.size) );

    CGFloat atlX = fabsf(touchLocation.x);
    CGFloat atlY = fabsf(touchLocation.y);

    CGFloat lenX = self.size.width  / 2;
    CGFloat lenY = self.size.height / 2;

    if ( (atlX <= lenX) && (atlY <= lenY) ) {
        isInside = YES;
    }

    return isInside;
}

原始问题

这段代码用于我的 UIView 子类:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self];
    BOOL isInside = [self pointInside:touchLocation withEvent:event];

    if (isInside)
    {
        if (NO == _isAlreadySelected)
        {
            [self setAppearanceSelected];
        }
        else
        {
            [self removeAppearanceSelected];
        }

       // more code
    }
}

现在我的类是 SKSpriteNode 的子类,所以我想使用相同的逻辑:

CGPoint touchLocation = [touch locationInView:self];
BOOL isInside = [self pointInside:touchLocation withEvent:event];

但它不起作用,我无法编译。 (因为SKSpriteNode 不存在这些方法)。
我设法将第一行更改为:

CGPoint touchLocation = [touch locationInNode:self];

问题
但是如何解决:

BOOL isInside = [self pointInside:touchLocation withEvent:event];

SKSpriteNode

更新 在@duci9y 的帮助下当前的解决方案

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s", __PRETTY_FUNCTION__);

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];
    BOOL isInside = [self containsPoint:touchLocation];

    NSLog(@"touchLocation %@", NSStringFromCGPoint(touchLocation) );
    NSLog(@"self.position %@", NSStringFromCGPoint(self.position) );
    NSLog(@"self.frame    %@", NSStringFromCGRect(self.frame) );

    if (isInside)
    {
        NSLog(@"INSIDE");
        //self.texture = [SKTexture textureWithImage:_onImage];
    }
    else
    {
        NSLog(@"OUTSIDE");
        //self.texture = [SKTexture textureWithImage:_offImage];

    }
}

日志:

-[WOC_OnOffImageButton touchesBegan:withEvent:]
touchLocation {9.5, 18}
self.position {160, 440}
self.frame    {{138, 414.5}, {44, 51}}
OUTSIDE

在我看来,SpriteKit 和 UIView 之间的坐标系存在一些问题。例如它们不是从同一点开始(左下角与左上角)。

【问题讨论】:

    标签: ios objective-c uiview sprite-kit


    【解决方案1】:

    Read the docs.

    BOOL isInside = [self containsPoint:touchLocation];
    

    【讨论】:

    • 这个方法存在,当我添加它时,代码可以编译,但我发现了新问题。使用当前代码,每次触摸都在SKSpriteNode之外,即使它在里面。我认为问题出在[self containsPoint:touchLocation],因为之前对 UIView 的调用是[self pointInside:touchLocation withEvent:event];,它使用了这个event。知道如何检测吗?
    • @WebOrCode 记录节点的尺寸和位置以及locationInNode: 返回的点,并使用结果更新您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    相关资源
    最近更新 更多