I want to have a path that my node can travel around on demand.
For example, I want to have a circle in the middle of the screen and when i tap the left side of the screen, my SKSpriteNode will move in a counter-clockwise direction on the circle (and the same if i tap the right side of the screen).
From what I've researched, I can create the circular path using SKShapeNode, CGPathRef, or even UIBezierPath. Creating the path isn't the issue. In all the instances I've seen researching this topic, most people just use [SKAction followPath:path duration:1.0] and this makes the node go around the the circular path ENTIRELY in 1.0 seconds. I want to be able to tap left/right and only move incremental amount of space per tap. (If anyone's played Super Hexagon, think of the fluid circular motion of that game)
*Note: I only use a circular path as an example so I don't particularly need specific pointers on how to move around a circle per se, but more of ANY path of any shape.
You can create any kind path shape you want using CGPath. This gives you the ability to create only the path for which you need to move your node. To use your circle example, create a 90 degree arc path and your node will move 4 separate times to complete a full circle.
If you do not want to use a path, you can also use the SKAction moveBy x,y,duration command. Tie a couple of those together and use them in a block sequence and you have yourself a generic path function.
Related
I've used CGPath to create a race track by stroking the path and offsets of it to draw the road and lines. My hope was that I could use this CGPath in SpriteKit so that I could make a car sprite follow the track. I can get a sprite to follow the entire path from start to finish using SKAction.followPath (screenshot added at bottom of post.)
However, my issue is I sometimes want a car sprite to move from the start to the half-way point (or 25% or some fraction of the path.)
I don't see a way of doing with with an SKAction so I was looking into a way of taking a CGPath and splitting it to whatever fraction of the path I needed but I've yet to find anything that seems to do that. I'm reasonably hopeful it can be done since in a CAShapeLayer you can set strokeStart and strokeEnd to specify where you want the path stroked from percent start to percent end so it looks like something like this has already been implemented.
Is there any way of either getting SpriteKit to set the start and end point of followPath or modifying a CGPath so that it returns a subpath?
car race screenshot
I'm developing a SpriteKit game using Swift and Xcode 6.3. I want a sprite node to follow along a defined path. I also want the position of the node following the defined path to be relative to the, in this case y coordinate change, of the other node.
The pink one is the one I move with my finger. Its "path" is from the origin to the horizontal pink line. If I drag it up 20% of its path I want the other two to move 20% in the same time. If I drag it back it will make the others go backwards on their path as well. The black dots represent this.
I'm sorry for not posting any code. I read about following a path in SpriteKit but nothing of what i found would have helped here. Thanks for your help.
I need a animation like revolving electrons on three orbiter path i the middle of nucleus. I am currently using cocos2d 3.2V for developing my game. I tried to make revolving animation using bezier path but this work for quadrant of circle not make a complete circle animation. How can i achieve this kind of animation using cocos 2d?
Thanks in advance
The center is your parent node, you add the orbiting nodes to the parent with their position at an offset like 40x0, then rotate the parent and the child will rotate around it.
If you need different rotation speeds simply add multiple parent nodes to the center, one for each "planet".
If you want to make the movement an ellipsis you can cheat to some extent. You can have the parent move slightly up and down (or between any other two opposing points) synchronized with the rotation.
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).
I have a line, lets say a rounded rect, drawn on screen (SKShapeNode drawn from a CGPath). Now I want a node to follow that rounded rect, either clockwise or counter clockwise. I've done this with SKAction's followPath:asOffset:orientToPath:duration: method, which works fine if the node starts at the path's origin.
The thing is, the node can be placed anywhere on the rounded rect, and when I press and hold a button I want it to start following the rect from the point it's currently located. When I release the button the node stops where it is. Right now if I stop the node and start it again it follows a new rounded rect starting at the nodes point.
Is there an easy way of achieving this?
It seems that you may make use of newly introduced SKConstraint class (https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKConstraint_Ref/).
For a quick introduction take a look at session 606 from WWDC2014: "What's new in SpriteKit".
It's really quick and simple way to adjust position/rotation/distance of the sprite in relation to another sprite.
What you exactly need is
+ (instancetype)orientToNode:(SKNode *)node
offset:(SKRange *)radians
This is a difficult problem. I can think of two solutions:
Each time you press and hold, construct a new CGPath for the node to follow which starts at the point your node is currently stopped.
Keep a reference to the SKAction object that you're running. When you stop pressing on the screen, instead of stopping the action, set it's speed property to 0.0. This will effectively pause the animation. When you press down again, set the speed back to 1.0.