2013-11-21 33 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