objective c - iOS Sprite Kit: How to make Sprite follow Touches -


i'm new sprite kit , i'm wondering how can make sprite follow touches. example, player sprite on bottom of screen. when tap on top of screen, player sprite should move touch point speed - , if move finger should pointing towards touch point. how tried implement it:

-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event {      (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 {     (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"];     }  } 

however, sprite moves laggy when moving finger. there way can make movement nice , smooth?

any appreciated!

edit: think have found solution modified touchesmoved function:

- (void)touchesmoved:(nsset*)touches withevent:(uievent*)event {     (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"];         }     }  } 

i'd bind uitouch sprite on touchesbegan, unbind on touchesended. on every update approach uitouch position 1 pole filter.

no need actions @ all, don't need implement touchesmoved way. whole stuff become more encapsulated.


or use skphysicsjointspring. create node touch, spring joint connects sprite , touch node. adjust touch node position only.


sprite

@interface approachingsprite : skspritenode @property (nonatomic, weak) uitouch *touch; @property (nonatomic) cgpoint targetposition; -(void)update; @end  @implementation approachingsprite  -(void)update {     // update target position if touch bound.     if (self.touch)     { self.targetposition = [self.touch locationinnode:self.scene]; }      // approach.     cgfloat filter = 0.1; // can fiddle speed values     cgfloat inversefilter = 1.0 - filter;     self.position = (cgpoint){         self.targetposition.x * filter + self.position.x * inversefilter,         self.targetposition.y * filter + self.position.y * inversefilter,     }; }  @end 

scene

-(void)touchesbegan:(nsset*)touches withevent:(uievent*) event { self.sprite.touch = [touches anyobject]; }  -(void)touchesended:(nsset*)touches withevent:(uievent*) event { self.sprite.touch = nil; }  -(void)update:(cftimeinterval) currenttime { [self.sprite update]; } 


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -