当场景第二次出现时,风景中的SKScene会给出错误的UITouch坐标

时间:2014-07-22 01:28:56

标签: ios sprite-kit

UITouch第二次出现SKScene后,SKSceneUITouch获取正确的坐标时出现问题。我已经编写了一个简化的应用来展示这里的问题:

https://github.com/JenDobson/SKSceneCoordinatesIssue

在这个简化的应用程序中有1个场景。场景中只有一个SKNode。触摸场景时,如果SKNode位置与touchesEnded:withEvent相交,则SKNode方法会回调ViewController以再次显示场景。但是当第二次呈现场景时,当我尝试触摸SKNode时,UITouch的坐标被反转(即x值报告y值,反之亦然)。因此它们不与SKNode's相交,并且不再呈现场景。 (如果我触摸屏幕上x = y线上反射的ViewWillLayoutSubviews位置的位置,那么我可以再次显示场景。)

我已尝试使用" ViewWillLayoutSubviews",但在此示例应用中,视图仅加载一次,因此我认为touchesEnded:withEvent未被重复调用,因此无法影响场景坐标。

为了说明问题,这是第一次出现场景时2014-07-21 17:57:06.908 TestTouchCoordinatesInSKScene[5476:60b] scene frame size:1024.000000,768.000000 2014-07-21 17:57:06.911 TestTouchCoordinatesInSKScene[5476:60b] location:685.000000,105.500000 2014-07-21 17:57:06.912 TestTouchCoordinatesInSKScene[5476:60b] navNode Position:700.000000,100.000000 的输出:

2014-07-21 17:57:14.724 TestTouchCoordinatesInSKScene[5476:60b] scene frame size:1024.000000,768.000000
2014-07-21 17:57:14.725 TestTouchCoordinatesInSKScene[5476:60b] location:107.000000,632.500000
2014-07-21 17:57:14.726 TestTouchCoordinatesInSKScene[5476:60b] navNode Position:700.000000,100.000000

这是touchesEnded的输出:withEvent第二次出现场景:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    SKView* skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    CGSize contentSize = CGSizeMake(skView.bounds.size.width,skView.bounds.size.height);

    _mainMenuScene = [MyMainMenuScene sceneWithSize:contentSize];
    _mainMenuScene.controller = self;
    _mainMenuScene.scaleMode = SKSceneScaleModeAspectFill;


    [skView presentScene:_mainMenuScene];

}


-(void)loadView
{
    CGRect viewFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
    SKView* view = [[SKView alloc] initWithFrame:viewFrame];
    self.view = view;
}

-(void)returnToMainMenu
{
    [(SKView*)self.view presentScene:_mainMenuScene transition:[SKTransition moveInWithDirection:SKTransitionDirectionRight duration:.3]];

}
@end

请注意"位置"对于第二个列表,x和y坐标相反。

以下是我认为的相关代码。

在" MyViewController.m"

-(CGPoint)navNodePosition
{
    return CGPointMake(700,100);
}

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

        _navNode = [SKLabelNode node];
        _navNode.position = [self navNodePosition];
        _navNode.text = @"Return to Main Menu";
        _setupLabel.fontColor = [UIColor whiteColor];
        _setupLabel.fontSize = 24;

        [self addChild:_navNode];

    }
    return self;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    BOOL shouldSegueToMainMenu = NO;

    NSLog(@"scene frame size:%f,%f",self.frame.size.width,self.frame.size.height);


    for (UITouch* touch in touches) {
        CGPoint touchLocation = [touch locationInNode:self];

        NSLog(@"location:%f,%f",touchLocation.x,touchLocation.y);
        NSLog(@"navNode Position:%f,%f",self.navNode.position.x,self.navNode.position.y);


        if ([self.navNode containsPoint:touchLocation]) {
            shouldSegueToMainMenu = YES;
            break;
        }
    }

    if (shouldSegueToMainMenu)
    {
        [self.controller returnToMainMenu];
    }

}

在" MyMainMenuScene.m"

{{1}}

非常感谢!

1 个答案:

答案 0 :(得分:0)

在玩了更多游戏之后,我相信这是SKView方法presentScene:transition中的错误。在returnToMainMenu的{​​{1}}方法中,如果我使用普通的MyViewController方法而不是presentScene,问题就会消失。我已经向Apple提交了这个错误17770198。