简体   繁体   中英

How do I detect if object has been swiped in sprite kit

I was wondering how I would get a node to be swiped in one of eight direction and the node to travel in that direction. I would only want it to be able to be swiped in the following directions: swiped up, top right, right, bottom right, down, bottom left, left and top left. I am new to sprite kit and swift so I am not sure how I would do this. Any help would be appreciated!

So I want to have it so the sigle sprite can be swiped in any of the directions listed, and it glide in the direction swiped for a shot distance and then for it to return back to the center.

This is actually several questions. I'm on my mobile now so I can't add sample code right now but you'll need to do a few things once you've set up your node you want to move.

  1. Set the physicsBody property of your node.

  2. Add another node where your first node is and add a physics body to it as well.

  3. Connect them with SKPhysicsJoint or similar class.

  4. As far as moving them you can set the velocity directly by changing the physicsBody.velocity property.

If all you need is what you explained to me in the comments then you won't need to detect which direction it swiped if you set up the joint node correctly.

Edit: Here is some sample code to get you started. It's probably not exactly what you want but you should be able to get started from here. It took a bit of time to do it so I'm sure it should help quite a bit.

First up, set your SKView to show physics in the game controller by using yourSKView.showsPhysics = YES;

{
    // active node will be the one you are touching now. you will see it is set in the touches methods
    SKSpriteNode *_activeNode;
    // this is the middle node
    SKSpriteNode *_fixedNode;
}

-(void)didMoveToView:(SKView *)view {
    // set up the physics world
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    self.physicsWorld.gravity = CGVectorMake(0, 0);
    // this node is made so you can pin something to it
    _fixedNode = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(1, 1)];
    _fixedNode.position = CGPointMake(self.frame.size.width /2, self.frame.size.height / 2);
    _fixedNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1];
    _fixedNode.physicsBody.dynamic = NO;
    [self addChild:_fixedNode];

    // this is the only node i made, you can make several and pin them to the same fixedNode
    SKSpriteNode *node = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(50, 50)];
    node.position = CGPointMake(self.frame.size.width /2, self.frame.size.height / 2);
    [self addChild:node];
    node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(50, 50)];
    _activeNode = node;

    //this is the skjoint i was telling you about
    SKPhysicsJointLimit *rope = [SKPhysicsJointLimit jointWithBodyA:_fixedNode.physicsBody bodyB:node.physicsBody anchorA:_fixedNode.position anchorB:node.position];

    // set how far you want it to go at max
    rope.maxLength = 150;
    [self.physicsWorld addJoint:rope];}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // make the node touched the active node
    _activeNode = (SKSpriteNode*)[self nodeAtPoint:[[touches anyObject] locationInNode:self]];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    //when touches end there is no active node, clear it
    _activeNode = nil;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    // the first like makes it so that if there is no active node nothing will be done, and if you are clicking on the scene nothing will be done as well. just a safeguard
    if (!_activeNode || [_activeNode isKindOfClass:[SKScene class]]) return;
    // where is the last touch?
    CGPoint touchPoint = [[touches anyObject] locationInNode:self];
    // from this point on in the method, a basic trig understanding is required
    // find the offset between fixedNode and moved position
    CGPoint offset = CGPointMake(touchPoint.x - _activeNode.position.x, touchPoint.y - _activeNode.position.y);
    // set the velocity directly (20 is an arbitrary number)
    _activeNode.physicsBody.velocity = CGVectorMake(offset.x * 20, offset.y * 20);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM