Objective c 精灵套件-使用tochesMoved沿x移动节点

Objective c 精灵套件-使用tochesMoved沿x移动节点,objective-c,ios7,sprite-kit,Objective C,Ios7,Sprite Kit,我有一个长方体节点,我只想通过向左或向右滑动来左右移动它。 下面代码的问题是,一旦触摸发生,盒子就会跳到触摸的x位置,然后它会随着你的手指移动 不错,但这不是我想要的效果,相反,如果触摸发生,无论屏幕上的哪个位置,框都会保持在原来的位置,并且只会相应地左右移动-或者+触摸x位置的值,并且最初不会跳到手指的位置。所以boxnewposition.position.x实际上不应该等于touchLocation.x 只是想补充一点,如果我尝试,盒子会快速闪烁 CGPoint BoxNewPositio

我有一个长方体节点,我只想通过向左或向右滑动来左右移动它。 下面代码的问题是,一旦触摸发生,盒子就会跳到触摸的x位置,然后它会随着你的手指移动

不错,但这不是我想要的效果,相反,如果触摸发生,无论屏幕上的哪个位置,框都会保持在原来的位置,并且只会相应地左右移动-或者+触摸x位置的值,并且最初不会跳到手指的位置。所以boxnewposition.position.x实际上不应该等于touchLocation.x

只是想补充一点,如果我尝试,盒子会快速闪烁

CGPoint BoxNewPosition = CGPointMake(touchLocation.x - Box.postion.x, Box.position.y); 
看起来好像它延迟了计算和扣除x位置

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

        UITouch *touch = [[event allTouches] anyObject];

        CGPoint touchLocation = [touch locationInView:self.view];


        SKSpriteNode* Box = (SKSpriteNode*)[self childNodeWithName:@"BoxNode"];

        CGPoint BoxNewPosition = CGPointMake(touchLocation.x, Box.position.y);

        Box.position = BoxNewPosition;

}

您可以将下面的代码更改为仅在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

谢谢sangony,但是代码中的节点设置为跟随触摸的位置,这也可以通过运行SKAction moveByX:different.x y:0来实现。我需要的是触摸屏幕上的任何地方,然后向左移动,例如+10倍。盒子会根据其原始位置实时执行相同的操作。@adimona-也许我不理解你的问题。我发布的代码将精灵从开始到结束平滑地移动到触摸位置,并在触摸位置发生变化时更改其在中流中的路径。使用SKAction move无法执行此操作,因为除非取消操作并启动新的操作,否则操作将继续执行其原始设置的目标。如果我没弄错你的第一句话,你说你的物体跳到了第一个触摸的位置,然后顺利地被跟踪。我的代码从头到尾都很流畅。