我已经为这个问题研究了几天了,但我似乎想不出答案。我已经做了大量的寻找答案的工作,并且我看到了一些提示,可能是雪碧-套件本身的问题,所以我正在辩论移到Cocos2D重新开始。但我希望这里有人能帮我。
我有一个名为_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;
}
}下面是我提取的将位置转换为world节点的方法:
-(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];
}
}
}在地图上插入一块瓷砖效果很好。它得到世界位置,然后除以瓷砖大小,以确定在世界地图中放置瓷砖的位置。它总是在适当的地方插入瓷砖。
但是,当我在夹点中使用两个点的中间点并将其转换为一个世界点时,它会返回一个值,但是这个值会有很大的偏移量,不同的数量取决于离中心的距离。
也许我只是需要弄清楚如何正确地偏移摄像机?再次感谢你在这个问题上的帮助,这让我发疯了!
发布于 2014-03-19 19:02:13
我也有同样的问题。
这是违反直觉,但点转换是不明智的。
它不会从一个节点转换到另一个节点。
它可以从场景坐标转换为场景坐标和节点坐标。
它不能从一个节点工作到另一个节点。您需要做的是从节点转换点,然后将点转换为节点。经过两个调用之后,坐标将位于所需的节点中。
https://stackoverflow.com/questions/21686526
复制相似问题