将SKSpriteNode移动到ios Game中的实际触摸点

时间:2015-04-13 12:07:08

标签: ios objective-c sprite-kit game-physics skspritenode

我有一个SKSpriteNode,这是一个球。我的看法没有引力。我可以通过在开始时对它施加冲动来移动球,我想让它移动到我们可以进入的用户触摸点 - (void)touchesBegan:(NSSet *)触及withEvent: (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];
        }
}

它似乎不起作用。帮助我们。

1 个答案:

答案 0 :(得分:1)

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

[SKAction moveTo:location duration:2];

SKAction moveBy:方法通过提供的x和y值移动节点,而moveTo方法移动到节点的父节点。

编辑:

另外,更改行:

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

CGPoint location = [touch locationInNode:self];

UIView的坐标系与SKScene不同。

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