我是 Sprite Kit 的新手,我想知道如何让精灵跟随触摸。例如,我的播放器精灵位于屏幕底部。当我点击屏幕顶部时,玩家精灵应该以一定的速度移动到触摸点 - 如果我移动手指,它应该始终指向触摸点。这就是我尝试实现它的方式:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
CGPoint diff = rwSub(location, self.player.position);
CGPoint norm = rwNormalize(diff);
SKAction *act = [SKAction moveByX:norm.x * 10 y:norm.y * 10 duration:0.1];
[self.player runAction:[SKAction repeatActionForever:act] withKey:@"move"];
}
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
CGPoint diff = rwSub(location, self.player.position);
CGPoint norm = rwNormalize(diff);
SKAction *act = [SKAction moveByX:norm.x * 10 y:norm.y * 10 duration:0.1];
[self.player runAction:[SKAction repeatActionForever:act] withKey:@"move"];
}
}
但是,当移动手指时,精灵的移动非常缓慢。有什么方法可以让动作变得流畅流畅吗?
任何帮助将不胜感激!
编辑:我想我找到了一个解决方案,我修改了 touchesMoved 函数:
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
for (UITouch *touch in touches) {
[self.player removeActionForKey:@"move"];
CGPoint location = [touch locationInNode:self];
CGPoint diff = rwSub(location, self.player.position);
CGPoint norm = rwNormalize(diff);
[self.player setPosition: rwAdd(self.player.position, rwMult(norm, 2))];
SKAction *act = [SKAction moveByX:norm.x * 10 y:norm.y * 10 duration:0.01];
[self.player runAction:[SKAction repeatActionForever:act] withKey:@"move"];
}
}
}