Drag Touch Based SKSpriteNode

I'm trying to drag the sprite along the y axis, but I’ll make the sprite a stick on the user's fingers, depending on where the strokes on the node started. The sprite is being dragged at the moment, but it seems to bind the sprite binding to the contact in the node.

Assuming this has something to do with getting the location inside the node by doing [touch locationInNode:selectedNode]; but I'm not sure where to go from there.

Here is my current code.

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { CGPoint location = [touch locationInNode:self]; SKNode *node = [self nodeAtPoint:location]; CGPoint newPosition = CGPointMake(node.position.x, location.y); if ([node.name isEqualToString:self.selectedNode] ) { if (newPosition.y > 230) { newPosition.y = 230; } node.position = newPosition; } } } 
+6
source share
2 answers

You need to compensate for newPosition based on the current touch position on the node.

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch *touch in touches) { CGPoint location = [touch locationInNode:self]; SKNode *node = [self nodeAtPoint:location]; if ([node.name isEqualToString:self.selectedNode] ) { CGPoint previousLocation = [touch previousLocationInNode:self]; float diff = location.y - previousLocation.y; CGPoint newPosition = CGPointMake(node.position.x, node.position.y + diff); if (newPosition.y > 230) { newPosition.y = 230; } node.position = newPosition; } } } 
+3
source

There are several ways to do this. The code sample below tracks the user's location and moves the sprite to this position during the update method. You can only change the code to move along the y axis or x axis.

 @implementation MyScene { SKSpriteNode *object1; CGPoint destinationLocation; } -(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { [self createObject]; destinationLocation = CGPointMake(300, 150); } return self; } -(void)createObject { object1 = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)]; object1.position = CGPointMake(300, 150); [self addChild:object1]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; destinationLocation = [touch locationInNode:self.scene]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; destinationLocation = [touch locationInNode:self.scene]; } -(void)update:(CFTimeInterval)currentTime { float x = fabs(object1.position.x - destinationLocation.x); float y = fabs(object1.position.y - destinationLocation.y); float divider = 0; if(x > y) { divider = x; } else { divider = y; } float xMove = (x/divider)*8; // change number to increase or decrease speed float yMove = (y/divider)*8; // change number to increase or decrease speed if(object1.position.x > destinationLocation.x) object1.position = CGPointMake(object1.position.x-xMove, object1.position.y); if(object1.position.x < destinationLocation.x) object1.position = CGPointMake(object1.position.x+xMove, object1.position.y); if(object1.position.y > destinationLocation.y) object1.position = CGPointMake(object1.position.x, object1.position.y-yMove); if(object1.position.y < destinationLocation.y) object1.position = CGPointMake(object1.position.x, object1.position.y+yMove); } @end 
+1
source

All Articles