I am making a simple game where the user swipes upward on an object (SKSpriteNode) sending it up and down in an arching motion (using a SKAction I assume). Here's an visual of what I'm hoping to accomplish (please forgive my basic Pixelmator skills):
I have created an SKAction which sends the object upwards and calculate the final point of the swipe based on velocity, etc. But, I cannot figure out how to make the arch motion occur once the final point is reached nor fall back to the bottom of the screen. I am hoping that I can add gravity to the object and Sprite Kit will handle the movement of the object, but that doesn't appear to be the case from my initial findings.
I've posted my current code here:
https://github.com/mikeberlin/Swipe-Arch-PoC
The app is based off this tutorial:
http://www.raywenderlich.com/44270/sprite-kit-tutorial-how-to-drag-and-drop-sprites
The code is written in Xamarin/C# but is almost identical to the Objective-C equivalent. If you need/want me to convert it to Objective-C I most certainly can.
Thank you all in advanced for your help and if I can provide any further information please let me know.
It sounds like right now, you're impasse wherein you can either use the physics engine in your game or not. If you use the physics engine, the movement in your game will look significantly more natural. The downside of using it is that it would take a more work to implement and the Sprite Kit physics engine can at times be sketchy. Here is a rough outline of what your two options look like:
If you want to stick with using an SKAction and not use the physics engine, you can create a UIBezierPath
and use [SKAction followPath:[curveName CGPath]
to move your sprite. Bezier curves look intimidating, but they are surprisingly simple to use once you get the hang of it. If you're going to go down that path, I'd recommend first looking at the pictures in the Wikipedia article.
If you chose to use this however, you will not be able to add gravity to the object (although you might be able to awkwardly simulate it if you fiddle with the action's timingMode
property.) To do this, you would have to use the physics engine (to avoid reinventing the wheel.) If you haven't used Sprite Kit's physics engine before, Apple's Sprite Kit Programming Guide has a simple example.
To accomplish what you want with the physics engine, you'll need to know a little trig and at least go through these steps:
- If you don't care about the exact angle and speed of the swipe, us
UISwipeGestureRecognizer
to catch the swipe.
- Otherwise, se the
touchesBegan
, touchesMoved
, and touchesEnded
functions to calculate the direction of thrust that has to be applied
- Use a timer between
touchesMoved
and touchesEnded
to figure out the velocity of the swipe.
- Use
applyForce
and possibly the applyTorque
to move your physics body.
- Tinker with the mass and shape of the physics body to get it to move how you want
Good luck with developing your game!
Ended up using the physics based approach. I went through the iOS Sprite Kit programming guide which helped quite a bit (go figure).
https://developer.apple.com/LIBRARY/IOS/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Introduction/Introduction.html
Per @LearnCocos2D and @doctorBroctor I came up with the solution that seems to be working well enough. I updated my github project if anyone stumbles upon this post and is looking for some code.
Thanks everyone!
PS - I wasn't sure how to mark this since I used pieces of two responses/answers. So I created this answer with explanation and up-voted both responses. If there's a better and more fair way to mark this question I'm all ears.