gpt4 book ai didi

ios - locationInNode : inaccurate after scaling the scene

转载 作者:行者123 更新时间:2023-11-28 22:21:58 25 4
gpt4 key购买 nike

我已经为此苦苦挣扎了一段时间,希望得到一些帮助!

我在 sprite kit 中有一个瓦片 map ,用户可以点击任何瓦片,事情就会发生。为了获得他们点击的图 block ,我使用了这样的东西:

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

UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:_mapLayer];
SKNode *tile = [_mapLayer nodeAtPoint:touchLocation];

// Do things with the tile...

}

但是,我也希望用户能够在需要时放大和缩小 map 以获得更好的 View 。这很简单,我设置了捏合识别器并使用以下方法缩放场景:

-(void)handlePinch:(UIPinchGestureRecognizer *)recognizer {
[self runAction:[SKAction scaleBy:recognizer.scale duration:0]];
}

一切都按预期工作,除了一旦场景的比例不是 1.0,locationInNode: 方法返回错误的坐标,导致 nodeAtPoint:退回错误的瓷砖。

例如,如果我在比例为 1.0 时点击一个图 block ,一切正常。但是,如果我将场景缩小到 0.9 比例并点击相同的图 block ,locationInNode: 会返回错误的坐标,因此将选择与我点击的图 block 不同的图 block 。

我做错了什么吗?

编辑:我创建了一张图片来说明我的问题以回应安德烈,这可能会有所帮助: http://i.imgur.com/N1XcyNx.png

最佳答案

试一试:

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

UITouch *touch = [touches anyObject];
CGPoint touchLocationInView = [touch locationInView:self.scene.view];
CGPoint touchLocationInScene = [self.scene convertPointFromView:
touchLocationInView];
CGPoint touchLocationInLayer = [_mapLayer convertPoint:touchLocationInScene
fromNode:self.scene];
SKNode *tile = [_mapLayer nodeAtPoint:touchLocationInLayer];

// Do things with the tile...

}

关于ios - locationInNode : inaccurate after scaling the scene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20114031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com