2013-11-21 91 views
0

我一直在努力这一段时间,并会喜欢一些帮助!locationInNode:缩放场景后不准确

我在精灵套件中有一个瓷砖地图,用户可以点击任何瓷砖和发生的事情。为了得到他们敲敲瓷砖,我用的是这样的:

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

回答

0

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

试试这个:

初始化后:myScene.scaleMode = SKSceneScaleModeAspectFill;

然后一边变焦:

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

阿!非常感谢你的配偶,那就是我做错了。 – Koonga

2

试试这个:

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

} 
+0

感谢您的帮助队友。不幸的是,我得到同样的问题,这里是一个图像来说明:http://i.imgur.com/N1XcyNx.png – Koonga

+0

我想我会添加更多的信息,以防万一它有帮助:我有一个SKScene图层,其中是被缩放的东西。在场景中只有一个名为_worldNode的子节点,_worldNode内部是一个包含地图的_mapLayer节点(并且最终还会包含其他图层,但是当我通过这个问题时会担心这个问题!)。 – Koonga