How to hit objects with finger movement in Sprite Kit, Objective C

后端 未结 3 1927
生来不讨喜
生来不讨喜 2021-01-03 12:13

I\'m trying to make a game in which I have some SKSpriteNodes and the User can hit them with finger movement, I\'m using apple\'s new Sprite Kit.

To

3条回答
  •  情书的邮戳
    2021-01-03 12:40

    After searching and making a lot of experiments, I finally have an answer!

    The first step in the solution was provided by AyatollahAndy - finding the point of impact:

    SKNode *node = [self nodeAtPoint:location];
    

    this line gets the node that we hit in the specified location, if it returns nil we didn't hit anything.

    secondly- we want to "Hit" the object, therefor we need to ApplyImpulse:

    [node.physicsBody applyImpulse:CGVectorMake(thrustV.x, thrustV.y) atPoint:location];
    

    Here you can see I applied the impulse with some vector that I created at the point of impact, That's it - pretty straight forward, I hope this will help others.

    Sharing is caring, Thanks for taking the time to read my post.

提交回复
热议问题