简体   繁体   中英

Drag and launch type thing in Swift Sprite Kit?

If I have a sprite node, just a white circle somewhere on the screen, how am I able to make it so when I drag, let's say downwards and slightly to the left, the circle sprite would launch upwards and to the right and then gradually come down, like a golf shot.

Another way of explaining the mechanic is the Angry Birds game, where you launch the birds of the slingshot, the birds move in the opposite direction of your drag and gradually come down.

For another live example of the mechanics of the circle, look at the app, Desert Golfing.

Thanks, and if you don't know what I mean just comment and I'll try to explain it better.

OPTIONAL: If you do know how to do the slingshot type mechanic for the circle, do you also know how to add an arrow to the screen so users know which way the circle will launch?

Ill try to break your problem down into small steps that you could then solve yourself:

  1. Detect the swipe:
    use a UIPanGestureRecognizer . You will be able to implement a method that is called whenever a user drags their finger in a certain direction.
    Here are some good references:
    - Pan Gesture Official Documentation
    - A very useful question that can serve you as a guide

  2. Detect the magnitude of the swipe in order to impart an impulse
    Check out the second link above. What you have to do is in the method for the gesture recognizer you will detect certain flags such as when the user starts the pan or ends the pan. Then, you can check for the location at those moments. With the Pythagorean theorem you should be able to get the distance and use that as the magnitude.

  3. Apply impulse:
    Create a physics body for your sprite and then make sure that you have gravity set inside your physics world. This allows the sprite to move in a parabolic motion. Then, use applyImpulse: on your physics body with your magnitude.

Regarding the arrow, you can easily do some delegation from within your pan gesture handler that gets the magnitude of the swipe and projects a reflection that your arrow will then show. Your question is pretty loaded so going into more detail is impossible, but best of luck. Hope this helps!

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