gpt4 book ai didi

ios - 拖动通过 SKPhysicsJoints 连接的 Sprite 的最佳方式?

转载 作者:行者123 更新时间:2023-11-29 03:31:38 25 4
gpt4 key购买 nike

我在移动通过 SKPhysicsJoint 连接的 Sprite 时遇到问题。我有一个如下所示的 touchesMoved 方法:

- (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]];
}

它有点管用,但如果我将重力设置得非常低,它似乎只能将 Sprite 移动到它应该移动的 25%,然后如果我将重力设置为默认。我很困惑,可能有很多事情,但我在想也许我只需要设置速度或其他东西,但如果是这样,什么是合适的设置?

最佳答案

这应该可以解决问题,我还建议您在抓取 Sprite 时关闭物理效果,并在您释放时再次打开。

- (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)];
}

如果你想再次开启物理学

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
_selectedNode.physicsBody.dynamic = YES;
}

关于ios - 拖动通过 SKPhysicsJoints 连接的 Sprite 的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19693507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com