拖动通过SKPhysicsJoints连接的Sprite的最佳方法? [英] Best way to drag Sprites that are attached via SKPhysicsJoints?

查看:109
本文介绍了拖动通过SKPhysicsJoints连接的Sprite的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法移动通过SKPhysicsJoint连接的精灵.我有一个touchesMoved方法,如下所示:

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

可以工作,但是如果我将重力设置得很低,它似乎只能将精灵移动到可能的最大距离25%,然后如果将重力设置为,它几乎就不会在y轴上移动.默认.我很困惑,可能有很多东西,但是我在想,也许我只需要设置速度或其他什么东西,但是如果是这样,什么是合适的设置呢?

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?

推荐答案

这应该可以解决问题,我还建议您在抓取精灵时关闭物理学,并在释放时再次尝试.

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

这篇关于拖动通过SKPhysicsJoints连接的Sprite的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆