Ios 将点转换为精灵套件中的节点
我已经研究这个问题好几天了,但我似乎无法解决它。我已经做了大量的搜索来寻找答案,并且我看到了一些提示,可能问题出在Sprite工具包本身,所以我正在讨论转移到Cocos2D并重新开始。但是,我希望这里有人能帮助我 我有一个叫做_world的基本摄影机节点,我用它来环游世界和缩放。平移和缩放效果很好,但我一直在尝试让世界节点移动到发生挤压的中心。这在某种程度上是可行的,但将点转换为世界节点中的位置似乎是个问题。这是我的密码: 我使用此代码将场景点转换为代码中其他位置的世界点,效果良好:Ios 将点转换为精灵套件中的节点,ios,objective-c,zooming,sprite-kit,Ios,Objective C,Zooming,Sprite Kit,我已经研究这个问题好几天了,但我似乎无法解决它。我已经做了大量的搜索来寻找答案,并且我看到了一些提示,可能问题出在Sprite工具包本身,所以我正在讨论转移到Cocos2D并重新开始。但是,我希望这里有人能帮助我 我有一个叫做_world的基本摄影机节点,我用它来环游世界和缩放。平移和缩放效果很好,但我一直在尝试让世界节点移动到发生挤压的中心。这在某种程度上是可行的,但将点转换为世界节点中的位置似乎是个问题。这是我的密码: 我使用此代码将场景点转换为代码中其他位置的世界点,效果良好: CGPoi
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];
如果有人能给我指出正确的方向,我将不胜感激!非常感谢你
编辑:我一直在研究这个问题,当然有些奇怪的事情正在发生,但我仍然不知道是什么。
以下是我完整的touchesbeent方法:
-(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];
}
}
}
将平铺插入地图效果良好。它获取世界位置,然后将其除以平铺大小,以确定在世界地图中放置平铺的位置。它总是在适当的位置插入瓷砖
但是,当我在夹点中使用两次接触的中点并将其转换为世界点时,它会返回一个值,但该值会被大量禁用,并且会根据距离中心的距离而不同
也许我需要弄清楚如何正确地偏移相机?再次感谢您对这个问题的帮助,这让我快发疯了 我也有同样的问题。
这是违反直觉的,但点转换并不聪明。
它不会从一个节点转换到另一个节点。
它可以从场景坐标转换为场景坐标和节点坐标。
从一个节点到另一个节点不起作用。
您需要做的是从节点转换点,然后将点转换为节点。两次调用后,这些坐标将位于您需要的节点中。我也遇到了同样的问题。
这是违反直觉的,但点转换并不聪明。
它不会从一个节点转换到另一个节点。
它可以从场景坐标转换为场景坐标和节点坐标。
从一个节点到另一个节点不起作用。
您需要做的是从节点转换点,然后将点转换为节点。两次调用后,这些坐标将位于您需要的节点中。我也遇到了同样的问题。
这是违反直觉的,但点转换并不聪明。
它不会从一个节点转换到另一个节点。
它可以从场景坐标转换为场景坐标和节点坐标。
从一个节点到另一个节点不起作用。
您需要做的是从节点转换点,然后将点转换为节点。两次调用后,这些坐标将位于您需要的节点中。我也遇到了同样的问题。
这是违反直觉的,但点转换并不聪明。
它不会从一个节点转换到另一个节点。
它可以从场景坐标转换为场景坐标和节点坐标。
从一个节点到另一个节点不起作用。
您需要做的是从节点转换点,然后将点转换为节点。两次调用后,这些坐标将位于您需要的节点中。您的计算正确,请尝试检查originalLocationOne和originalLocationTwo,以确保在转换之前这些值是您期望的值。感谢您迄今为止的帮助!您的计算是正确的,请尝试检查originalLocationOne和OriginAllocationSecond,以确保这些值是您在转换之前所期望的值。感谢您迄今为止的帮助!您的计算是正确的,请尝试检查originalLocationOne和OriginAllocationSecond,以确保这些值是您在转换之前所期望的值。感谢您迄今为止的帮助!您的计算是正确的,请尝试检查originalLocationOne和OriginAllocationSecond,以确保这些值是您在转换之前所期望的值。感谢您迄今为止的帮助!