How to move SpriteKit Node as Car driving experience? - ios

I want to move my car based on user touch but with curves. It's moving properly whenever I touch on the scene. But I need to move it with the perfect momentum of the car.
1. Touch anywhere in Front: It should move straight and stops when the user touched.
2. Touch anywhere in Front Right: It should move with a curve in turn right.
3. Touch anywhere in Front Left: It should move with a curve in turn Left.
4. Touch anywhere in Bottom/Back: It should move straight reverse.
5. Touch anywhere in Bottom Right: It should move reverse with a curve in turn right.
6. Touch anywhere in Bottom Left: It should move reverse with a curve in turn Left.
And it should behave the same even if it's rotated to horizontal as well.
I am new in SpriteKit so, Other solutions most welcome.
Please check image for an idea

What you’re going to have to do is to detect a touch on the sprite, work out where about on the sprite it is in relation to the centre and then move the sprite (maybe via an SKAction following your CGPath).
There’s quite a few different concepts involved so if you’re really new to Sprite Kit, I’d recommend some good SK tutorials. Personally I like the ones at raywenderlich.com but there are good ones on places like Udemy too.

Related

Swift/Xcode "Snake Game"

I am a student learning how to code in Xcode using swift. I am currently trying to make an app similar to the snake game that hopefully everyone knows.
I am currently on a windows computer so I can't show my code at the moment. I doubt you will need it for what I'm asking.
I am trying to make my Sprite Node (Just a while square) move with a swipe of the screen in the up, down, left, right fashion just like the original snake game. I have the Gesture Recognizer set up so it prints out what direction your swiping.
How can I make the sprite node move with a swipe? I want it to move infinitely in that direction until you either 1.) hit a will and you respawn/game over or 2.) swipe in a different direction.
I assume you add velocity to the node when you swipe so you just add the code into that swipe snippet but how do I make it apply ONLY to the "snake"?
You can use an SKAction to move the snakeHead toward the wall that corresponds to the direction you want to move. This can be done in the action parameter when you declare your gestures.
I suggest using .move(to: duration:) and to maintain a constant speed toward each wall you can use the distance, speed, time formula to calculate the duration for the .move(to: duration:) action.
Check out https://developer.apple.com/documentation/spritekit/skaction
https://developer.apple.com/documentation/spritekit/skaction/1417768-move

Draw circle and drag touch around SKSpriteNode

I am making a Sprite Kit game where the player (basically a stickman) has a running animation and a parallax scrolling background.
Now I have enemies that come near my player. To destroy these enemies sometimes I have to touch the enemies node to launch a rocket or attack them with an attack button or just jump over them.
Everything is working fine, but I want to add some extra moves to destroy them. I want some enemies that you can just destroy if you have drawn a whole circle around them. So imagine they come and you make a circle and then my player launch a laser or something. The problem is I have no idea where to start.
I haven't found anything on the internet. If it's too complicated or almost impossible how about touching my player node and dragging to the enemy?
EDIT: I think I have to create a custom GestureRecognizer that recognizes if a circle is drawn around a sprite and then runs the code. I don't know how this works ?
Yes, it's too complex. Not just from a coding point of view, but also from that of the player's experience.
Anything that requires complex gestures over a large amount of glass is annoying for the player because they're never going to have the same experience. Their finger's moisture and oil content always changes, as does the ambient temperature and cleanliness of their screen.
So big gestures required to be performed quickly (a gaming input like this) will sometimes be fun and smooth, and other times degrade as an experience based on the nature of the above properties.
Best to avoid them for a game's best possible experience.
If you must do it, there's two ways to research how.
Seek out "custom gesture" creation and utilisation through documentation and google, etc.
Think about using some kind of array to store all the points where the player's finger moves through during that circle gesture and attempt to discern if an enemy is within that space and then act accordingly.
--- probably other ways, too. But these jump to mind.

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:
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
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.
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!

How to create a swipe to hit in Sprite Kit

I am in the processs of creating a "shape flicking" game of sorts for the iOS platform. I am using Apple's Sprite Kit and I would like the most efficient and least performance costly approach to have the ability to "flick" the various shapes off the screen.
My first thought would be to have an invisible circular physics body that follows your finger whenever you swipe or touch the screen, but I think there may be an easier way by applying an impulse..any thoughts?
I found the solution I was looking for here in the link below..question was already answered!
How to hit objects with finger movement in Sprite Kit, Objective C

SKPhysicsJointPin pin position

I'm trying to understand SKPhysicsJointPin a bit better, specifically the anchor point parameter.
I understand that the anchor point is the position in the parent node of the two physics bodies concerned. What I don't understand at which point in the participating bodies does the pin go through and if there is a way to control that.
To make myself clear, say I'm making a clock with hands so the pin should go in the middle of the circle but for the hands is should go through the edge. So how can you control that.
Thanks a bunch.
Documentation is pretty lacking for these. To use the clock reference, and since you obviously know the code, I'll give you the English breakdown:
add 2 clock hand sprites to the scene
set both anchor points on the clock hand sprites to (.5,0)
position both sprites at (100,100)
create 2 physics bodies, add them to each clock hand
create the SKPhysicsJointPin, use anchor point (100,100)
add the joint to the scene
You should in theory, now have 2 clock hands, able to spin via their own anchor points around the point in the scene at (100,100).

Resources