我试图在 y 轴上拖动 Sprite ,但根据节点上开始触摸的位置,使 Sprite “粘附”到用户的手指上。 Sprite 目前正在拖动,但它似乎正在将 Sprite 的 anchor 捕捉到节点内的触摸位置。
我假设它与通过执行 [touch locationInNode:selectedNode];
获取节点内的位置有关,但我不确定从那里去哪里。
这是我当前的代码。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
CGPoint newPosition = CGPointMake(node.position.x, location.y);
if ([node.name isEqualToString:self.selectedNode] ) {
if (newPosition.y > 230) {
newPosition.y = 230;
}
node.position = newPosition;
}
}
}
最佳答案
您需要根据节点上的当前触摸位置来偏移newPosition。
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:self.selectedNode] )
{
CGPoint previousLocation = [touch previousLocationInNode:self];
float diff = location.y - previousLocation.y;
CGPoint newPosition = CGPointMake(node.position.x, node.position.y + diff);
if (newPosition.y > 230) {
newPosition.y = 230;
}
node.position = newPosition;
}
}
}
关于ios - 根据触摸位置拖动 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23436988/