几天来我一直在研究这个问题,但我似乎无法弄清楚。我已经做了很多寻找答案的工作,而且我已经看到一些提示,可能问题出在 Sprite-Kit 本身,所以我正在考虑转向 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;
}
}
这是我提取的将位置转换为世界节点的方法:
-(CGPoint) convertTouchPointToWorld:(CGPoint)touchLocation {
CGPoint firstLocationInWorld = [self.scene convertPoint:touchLocation toNode:_world];
NSLog(@"inside converting method %@", NSStringFromCGPoint(firstLocationInWorld));
return firstLocationInWorld;
}
下面是根据建筑物在世界地图中的位置在游戏 map 中放置建筑物的完整方法:
-(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];
}
}
}
将图 block 插入 map 效果很好。它获取世界位置,然后将其除以图 block 大小,以确定在世界地图中放置图 block 的位置。它总是在适当的位置插入一个磁贴。
但是,当我在紧要关头使用两个触摸的中间点并将其转换为世界点时,它会返回一个值,但该值有很大偏差,并且根据与中心的距离而有所不同。 ..
也许我只需要弄清楚如何正确地偏移相机?再次感谢您对这个问题的帮助,这让我发疯!
最佳答案
我遇到了同样的问题。
这是违反直觉的,但点转换并不聪明。
它不会从一个节点转换为另一个节点。
可以将场景坐标转换为场景坐标,也可以将场景坐标转换为节点坐标。
它不适用于从一个节点到另一个节点。
您需要做的是从节点转换点,然后将点转换为节点。两次调用后,坐标将位于您需要的节点中。
关于ios - 在 Sprite-Kit 中将点转换为节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21686526/