0

Here is a video I recorded with the problem visually https://www.youtube.com/watch?v=AC-4c4Qcyeo&feature=youtu.be

Currently facing a bug after when tocuhesMoved drag line path is completed I want the sprite to move to the position that the last place where the touch was. However you can touch anywhere on the UIView and it will move to that loaction without using the touchesMoved drag line. How do I only make the sprite move to the location only using the drag line mechanic to move the sprite?

- (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]];
  }
}
NWBDevelopment
  • 103
  • 1
  • 1
  • 7
  • it's been a while since i looked at this, but i believe you always get touchesBegan and touchesEnded on a touch, so, in your touchesMoved simply set a flag, in touchesEnd check if the flag was set, i.e. touchesMoved actually happened, and if so move your "blade". A better alternative, as your path appears to be a property, is simply to check the CGPath in touchesEnded to determine if it's empty. – MDB983 May 27 '15 at 22:59
  • I dont really understand what you mean when you say flag? but as for the alternative you said can you move the blade along the path using (SKAction *)followPath:(CGPathRef)path and set it to its new location then do [self.line2 removeFromParent]; – NWBDevelopment May 27 '15 at 23:15
  • no, i meant just as Julio has added below, test to see if the CGPath contains points – MDB983 May 27 '15 at 23:28

1 Answers1

0

All the content inside touchesEnded it´s being executed every time a touch event exists thanks to this conditional

if ([touches count])

Noticed that inside touchesMoved you add a path to self.line2 so inside touchesEnded you can do the following

- (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]];
    }
  }
}

so if self.line2 is not equal to nil the content only will be executed if the line path exists

Good Luck!!

Julio Montoya
  • 1,614
  • 1
  • 12
  • 14
  • Thanks for the reply But, it I can still touch and move the sprite to a location without doing the drag line path. I want the only touching the sprite to move it to a new location not anywhere else on the UIView. (sent you a email) – NWBDevelopment May 27 '15 at 23:36
  • your sprite movement action should be inside if (self.line2 != nil) that way your sprite will only move after the drag – Julio Montoya May 27 '15 at 23:44
  • I think I have must have done something else for it not to follow if (self.line2 != nil) I linked the source code to you. – NWBDevelopment May 27 '15 at 23:55