【问题标题】:locationInNode: inaccurate after scaling the scenelocationInNode:缩放场景后不准确
【发布时间】:2013-11-21 06:32:04
【问题描述】:

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

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

-(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...

}

但是,如果需要,我还希望用户能够放大和缩小以更好地查看地图。这很容易,我设置了一个捏识别器并使用以下方法缩放场景:

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

一切都按预期工作,除了场景比例为 1.0 以外的任何值时,locationInNode: 方法返回错误的坐标,导致 nodeAtPoint: 返回错误的图块。

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

我是不是做错了什么?

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

【问题讨论】:

    标签: ios objective-c ios7 sprite-kit


    【解决方案1】:

    试试这个:

    -(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...
    
    }
    

    【讨论】:

    • 感谢您的帮助伙伴。不幸的是,我遇到了同样的问题,这里有一张图片来说明:i.imgur.com/N1XcyNx.png
    • 我想我会添加更多信息以防万一:我有一个 SKScene 层,这是可以缩放的东西。场景内部只有一个名为 _worldNode 的子节点,_worldNode 内部是一个包含地图的 _mapLayer 节点(最终还会包含其他图层,但当我解决这个问题时会担心这个!)。
    【解决方案2】:

    什么是放大/缩小,你不应该缩放场景(就像你尝试 setScale 时所暗示的那样)。你应该调整它的大小。

    试试这个:

    初始化后:myScene.scaleMode = SKSceneScaleModeAspectFill;

    然后在缩放时:

    -(void)handlePinch:(UIPinchGestureRecognizer *)recognizer 
    {
       float newX = // calculate scaled X
       float newY = // calculate scaled Y
       self.size = CGSizeMake(newX, newY);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      相关资源
      最近更新 更多