这是我以视觉方式录制的问题视频https://www.youtube.com/watch?v=AC-4c4Qcyeo&特性= youtu.be
当tocuhesMoved拖动线路径完成后,当前面临一个bug,我希望精灵移动到最后触摸的位置。然而你可以触摸UIView上的任何地方它会移动到那个位置而不需要使用touchesMoved拖拽线。我如何让精灵移动到只使用拖拽线机制来移动精灵的位置?
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Moving");
if ([touches count]) {
UITouch* touch = [touches anyObject];
CGPoint position = [touch locationInNode:self];
path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, position.x, position.y);
CGPathAddLineToPoint(path, NULL, blade.position.x, blade.position.y);
//test points
//NSLog(@"%f", position.x);
//NSLog(@"%f", position.y);
self.line2.path = path;
}
}
TouchesEnded ~ ~ ~ ~ ~
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.line2 removeFromParent];
if ([touches count]) {
[blade runAction:[SKAction moveTo:[[touches anyObject]locationInNode:self]duration:0.21]];
}
}
中的所有内容touchesEnded由于这个条件
每次触摸事件存在时它都会被执行if ([touches count])
注意到,在touchesMoved中,您添加了一个路径到self。line2所以在touchesEnded中你可以做如下操作
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.line2 != nil) {
[self.line2 removeFromParent];
if ([touches count]) {
[blade runAction:[SKAction moveTo:[[touches anyObject]locationInNode:self]duration:0.21]];
}
}
}
所以如果self。line2不等于nil只有当行路径存在时才会执行内容
祝你好运! !