在ios游戏中将SKSpriteNode移动到实际接触点

在ios游戏中将SKSpriteNode移动到实际接触点,ios,objective-c,sprite-kit,game-physics,skspritenode,Ios,Objective C,Sprite Kit,Game Physics,Skspritenode,我有一个SKSpriteNode,它是一个球。我的观点没有重力。我可以通过在开始时对球施加一个脉冲来移动球,我想让它移动到用户接触点,我们可以在-(无效)接触开始:(NSSet*)接触事件:(UIEvent*)事件方法中得到它,而不影响它的运动 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { CG

我有一个SKSpriteNode,它是一个球。我的观点没有重力。我可以通过在开始时对球施加一个脉冲来移动球,我想让它移动到用户接触点,我们可以在-(无效)接触开始:(NSSet*)接触事件:(UIEvent*)事件方法中得到它,而不影响它的运动

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

    for (UITouch *touch in touches)
        {
            CGPoint location = [touch locationInView:self.view];
            SKAction *moveToPoint = [SKAction moveByX:location.x y:location.y duration:2];

            [ball runAction:moveToPoint];
        }
}

它似乎不起作用。伙计们,帮帮我。

如果要将节点直接发送到某个点,请使用以下方法:

[SKAction moveTo:location duration:2];
SKAction moveBy:
方法通过提供的x和y值移动节点,而
moveTo
方法将节点移动到节点父节点中的点

编辑:

另外,更改行:

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

UIView的坐标系与场景的坐标系不同


编辑2:当触摸存在时,您可以移除物理影响

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

    for (UITouch *touch in touches)
        {
            ball.physicsBody.dynamic = NO;

            CGPoint location = [touch locationInNode: self];
            SKAction *moveToPoint = [SKAction moveTo:location duration:2];

            [ball runAction:moveToPoint];
        }
}

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

使用moveTo:durationBall代替moveByX:y:duration,它并没有移动到接触点,而是移动到其他地方。你的代码也会影响球的运动轨迹。好吧,那么球也在物理模拟中?是的,没有重力,没有摩擦,恢复=1,没有线性阻尼。由于施加了初始脉冲,球在移动。当接触存在时,可以消除物理对球的影响。请参见编辑2现在的问题是,球在触点处反弹,即使触点处并没有任何东西
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    for (UITouch *touch in touches)
        {
            ball.physicsBody.dynamic = NO;

            CGPoint location = [touch locationInNode: self];
            SKAction *moveToPoint = [SKAction moveTo:location duration:2];

            [ball runAction:moveToPoint];
        }
}

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