问题
I'm having trouble moving sprites that are attached via an SKPhysicsJoint. I have a touchesMoved method that looks like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPoint previousPosition = [touch previousLocationInNode:self];
CGPoint translation = CGPointMake(positionInScene.x - previousPosition.x, positionInScene.y - previousPosition.y);
CGPoint newPosition = CGPointMake(_selectedNode.position.x + translation.x, _selectedNode.position.y + translation.y);
[_selectedNode runAction:[SKAction moveTo:newPosition duration:0]];
}
and it kind of works, but it seems to only move the sprite maybe 25% as far as it should if I set the gravity very low, and then hardly moves it on the y axis at all if I set gravity to the default. I'm so confused, it could be so many things, but I am thinking maybe I just need to set the velocity or something, but if so, what's an appropriate setting?
回答1:
This should fix issues, i also suggest turning off the physics when you grab the sprite, and turing on again when you release.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPoint previousPosition = [touch previousLocationInNode:self];
CGPoint translation = CGPointMake(positionInScene.x - previousPosition.x, positionInScene.y - previousPosition.y);
//_selectedNode.physicsBody.dynamic = NO;
[_selectedNode setPosition:CGPointMake(_selectedNode.position.x + translation.x, _selectedNode.position.y + translation.y)];
}
if you want to turn physics on again
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
_selectedNode.physicsBody.dynamic = YES;
}
来源:https://stackoverflow.com/questions/19693507/best-way-to-drag-sprites-that-are-attached-via-skphysicsjoints