Ios 根据触摸位置拖动SKSpriteNode

Ios 根据触摸位置拖动SKSpriteNode,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,我试图在y轴上拖动一个精灵,但根据节点上触摸的起始位置,使精灵“粘”到用户的手指上。 精灵当前正在拖动,但它似乎正在将精灵的锚点捕捉到节点内的触摸位置 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { CGPoint location = [touch locationInNode:self];

我试图在y轴上拖动一个精灵,但根据节点上触摸的起始位置,使精灵“粘”到用户的手指上。 精灵当前正在拖动,但它似乎正在将精灵的锚点捕捉到节点内的触摸位置

-(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;
        }
    }
}
我假设这与通过执行
[触摸位置Innode: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;
        }
    }
}

有几种方法可以做到这一点。下面的示例代码跟踪用户的触摸位置,并在更新方法期间将精灵移向该位置。可以修改代码,使其仅在y轴或x轴上移动

@implementation MyScene
{
    SKSpriteNode *object1;
    CGPoint destinationLocation;
}

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        [self createObject];
        destinationLocation = CGPointMake(300, 150);
    }
    return self;
}

-(void)createObject
{
     object1 = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
     object1.position = CGPointMake(300, 150);
    [self addChild:object1];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    destinationLocation = [touch locationInNode:self.scene];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    destinationLocation = [touch locationInNode:self.scene];
}

-(void)update:(CFTimeInterval)currentTime
{
    float x = fabs(object1.position.x - destinationLocation.x);
    float y = fabs(object1.position.y - destinationLocation.y);

    float divider = 0;
    if(x > y)
    {
        divider = x;
    } else
    {
        divider = y;
    }

    float xMove = (x/divider)*8; // change number to increase or decrease speed
    float yMove = (y/divider)*8; // change number to increase or decrease speed

    if(object1.position.x > destinationLocation.x)
        object1.position = CGPointMake(object1.position.x-xMove, object1.position.y);

    if(object1.position.x < destinationLocation.x)
        object1.position = CGPointMake(object1.position.x+xMove, object1.position.y);

    if(object1.position.y > destinationLocation.y)
        object1.position = CGPointMake(object1.position.x, object1.position.y-yMove);

    if(object1.position.y < destinationLocation.y)
        object1.position = CGPointMake(object1.position.x, object1.position.y+yMove);
}

@end
@实现MyScene
{
SKSpriteNode*object1;
CGPoint目标定位;
}
-(id)initWithSize:(CGSize)大小
{
if(self=[super initWithSize:size])
{
[自创建对象];
destinationLocation=CGPointMake(300150);
}
回归自我;
}
-(void)createObject
{
object1=[SKSpriteNode spriteNodeWithColor:[SKColor redColor]大小:CGSizeMake(50,50)];
object1.position=CGPointMake(300150);
[self-addChild:object1];
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
destinationLocation=[touch locationInNode:self.scene];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
destinationLocation=[touch locationInNode:self.scene];
}
-(无效)更新:(CFTimeInterval)currentTime
{
float x=fabs(object1.position.x-destinationLocation.x);
float y=fabs(object1.position.y-destinationLocation.y);
浮点数=0;
如果(x>y)
{
除法器=x;
}否则
{
除法器=y;
}
浮点xMove=(x/除法器)*8;//更改数字以提高或降低速度
浮点yMove=(y/除法器)*8;//更改数字以增加或降低速度
if(object1.position.x>destinationLocation.x)
object1.position=CGPointMake(object1.position.x-xMove,object1.position.y);
if(object1.position.xdestinationLocation.y)
object1.position=CGPointMake(object1.position.x,object1.position.y-yMove);
if(object1.position.y
您需要根据节点上的当前触摸位置偏移新位置

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

谢谢sangony,不过我想我应该用“拖拽”这个词而不是移动。如果我没弄错的话,你的代码会把物体移动到我想要拖动被触摸物体的地方。这就是将锚点捕捉到接触点的问题所在。更新了我的问题以更好地反映这一点。@justMike-我仍然不明白您所说的“到节点内的触摸位置”是什么意思?什么节点?你说的是视图吗?被触摸的节点。阿卡什一针见血。谢谢你抽出时间。我也有同样的问题,但是对于斯威夫特,我试着去改变它,但它并没有像预期的那样起作用。我还不太熟悉斯威夫特。请发布一个新问题,包括您翻译的代码和此答案的链接。@ZeMoon这样做的意义是什么??CGPointMake(node.position.x,node.position.y+diff);