2014-02-10 103 views
0

我一直在解決這個問題幾天,現在我似乎無法弄清楚。我已經完成了大量搜索答案,並且我已經看到提示可能問題出在Sprite-Kit本身,所以我正在討論轉向Cocos2D並重新開始。但是,我希望這裏有人能幫助我。在Sprite-Kit中將點轉換爲節點

我有一個叫做_world的基本攝像機節點,我用它來平移世界和縮放。平移和縮放工作正常,但我一直試圖讓世界節點移動到發生夾點的中心。它有點類似於作品,但將這一點轉換爲世界節點的位置似乎是問題所在。這裏是我的代碼:

我使用此代碼的場景點在代碼中的其他地方轉換爲世界一點,它工作正常:

CGPoint positionInScene = [touch locationInNode:self]; 
CGPoint locationInWorld = [self.scene convertPoint:positionInScene toNode:_world]; 

不過後來我嘗試這樣做使用中點在捏發現了兩個點,它似乎並不正確地轉換:

originalLocationOne = [sender locationOfTouch:0 inView:self.view]; 
originalLocationTwo = [sender locationOfTouch:1 inView:self.view]; 
//I do this because SpriteKit doesn't have a ccpAdd method 
originalAddedMidPoint = CGPointMake(originalLocationOne.x + originalLocationTwo.x, originalLocationOne.y + originalLocationTwo.y); 
//same thing here but for ccpMidPoint 
originalMidPoint = CGPointMake(originalAddedMidPoint.x * 0.5f, originalAddedMidPoint.y * 0.5f); 
_newWorldPos = [self convertPoint:originalMidPoint toNode:_world]; 

我真的很感激,如果有人能在正確的方向指向我!非常感謝!

編輯:我一直在研究這個問題一些,當然有些奇怪的事情發生,但我仍然不能告訴什麼。 這裏是我的完整的touchesBegan方法:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    _myTouches = [[NSMutableArray alloc]init]; 
    for (UITouch *touch in touches) { 
     [_myTouches addObject:touch]; 
    } 
    if (_myTouches.count > 1) { 
     UITouch *touch1 = [_myTouches objectAtIndex:0]; 
     UITouch *touch2 = [_myTouches objectAtIndex:1]; 
     CGPoint touch1Location = [touch1 locationInView:self.view]; 
     CGPoint touch2Location = [touch2 locationInView:self.view]; 
     NSLog(@"tpoint1 = %@, tpoint2 = %@", NSStringFromCGPoint(touch1Location), NSStringFromCGPoint(touch2Location)); 
     CGPoint touchAddedPoint = CGPointMake(touch1Location.x + touch2Location.x, touch1Location.y + touch2Location.y); 
     CGPoint touchMidPoint = CGPointMake(touchAddedPoint.x * 0.5f, touchAddedPoint.y * 0.5f); 
     NSLog(@"touch mid point = %@", NSStringFromCGPoint(touchMidPoint)); 
     CGPoint newWorldPoint = [self convertTouchPointToWorld:touchMidPoint]; 

     //camera needs to be offset to work properly 
     CGPoint alteredWorldPoint = CGPointMake(-newWorldPoint.x * 0.75f, -newWorldPoint.y * 0.75f); 

     _firstTouch = alteredWorldPoint; 
     _tempWorldLocation = _firstTouch; 
     _worldMovedForUpdate = YES; 
    } 
} 

這裏是我提取到的位置轉換成世界節點的方法:

-(CGPoint) convertTouchPointToWorld:(CGPoint)touchLocation { 
    CGPoint firstLocationInWorld = [self.scene convertPoint:touchLocation toNode:_world]; 
    NSLog(@"inside converting method %@", NSStringFromCGPoint(firstLocationInWorld)); 
    return firstLocationInWorld; 
} 

這裏是放置一個建築物在遊戲中的整個方法根據其在世界地圖上的位置的地圖:

-(void)selectNodeForTouch:(CGPoint)touchLocation { 
    SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation]; 
    //NSLog(@"node name is = %@", touchedNode.name); 

    _selectedNode = touchedNode; 
    if ([[touchedNode name] isEqualToString:@"hudswitch1"]) { 
     if([self.theGame getMovesLeft] == 0){ 
      _hudNeedsUpdate = YES; 
      _turnNeedsUpdating = YES; 
     } 
    } 
    if ([self.theGame getMovesLeft] > 0) { 
     [self handleButtonsForTouch:touchedNode]; 
    } 
    //this inserts the tile at the location in world node 
    NSLog(@"touchLocation.x = %f, touchlocation.y = %f", touchLocation.x, touchLocation.y); 
    if ([[touchedNode name] isEqualToString:@"tile"] && _selectedBuildingType != 0) { 

     //CGPoint locationInWorld = [self.scene convertPoint:touchLocation toNode:_world]; 
     CGPoint locationInWorld = [self convertTouchPointToWorld:touchLocation]; 
     CGPoint gameLocation = [self convertWorldPointToGamePoint:locationInWorld]; 

     NSLog(@"locationWorld.x = %f, locationWorld.y = %f", locationInWorld.x, locationInWorld.y); 

     if(![self.theGame isBuildingThere:gameLocation] && [self.theGame isValidLocation:gameLocation]) { 
      [self updateActiveTilePos:locationInWorld]; 
     } 
    } 
} 

將瓷磚插入地圖工作正常。它獲取世界位置,然後將其除以瓦片大小以找出將瓦片放置在世界地圖中的位置。它總是在正確的位置插入一塊瓷磚。

但是,當我使用捏的兩個觸摸的中間點並將其轉換爲世界點時,它返回一個值,但該值大量關閉,並且取決於距離中心的距離...

也許我只需要弄清楚如何正確地偏移相機?再次感謝這個問題的任何幫助,這讓我瘋狂!

+0

您的數學正確,請嘗試檢查originalLocationOne和originalLocationTwo以確保這些值是您在轉換之前所期望的值。 – user688518

+0

感謝您的幫助! – bazola

回答

4

我有同樣的問題。
這是違反直覺的,但點轉換並不聰明。
它不會從一個節點轉換到另一個節點。
它可以從場景座標轉換到場景座標和節點座標。
它不能從一個節點工作到另一個節點。 你需要做的是從節點轉換點,然後將點轉換爲節點。在兩個這些調用座標將在您需要的節點中。