How to implement snap to center in SpriteKit? - sprite-kit

Basically, I have many SKNodes in my SKScene. I can't use SKAction because I want the physics engine to handle collisions for me so my only option left is to use use physics. How would I go about this? Ex: when a node is tapped, it should snap to the center of the screen
while also having other nodes bounce off during collisions.

Ok I've figured it out. All I had to do was calculate the vector from my current node position to the center and apply a magnified velocity to my node towards that direction. In the update method of my SKScene, I constantly check to see if I'm within a certain radius of the center and if I am, I just set the velocity of my node to 0. That stops it at the center. The beauty of this too is that for my other nodes that aren't centered, if I drag them into the center node and move the center node elsewhere, after the drag finishes, the center node snaps back.
You can check out my solution here: https://github.com/ThasianX/ElegantColorPalette

Related

Keeping a center point when scene is moving

I am new to swift and making a game where the SKCamera is moving on the scene but I wanna keep the center of the scene updated when skcamera moves along. Just wondering what can I use.
thank you
What do you mean "keep the center of the scene updated when skcamera moves along" If you are using a camera, the camera is always the the center of the screen, and the entire scene is always updating. I am going to take a stab in the dark here and say you are not setting scene.camera = SKCameraNode. Other than this, you will need to clarify what you are asking for.
Use this to set the camera to the ball after it passes a certain point:
camera.y = ball.position.y > scene.height/2 ? ball.position.y : scene.height / 2
If you add a SKSpriteNode as child of your camera node, and you set it to the center of the camera node, it will stay at the center of the screen, even when the camera moves.

Spritekit - Moving Physics world issue

I am building a tower stacking game. My structure is as follows:
I have a world node which has a physics body attached to it.
I add SKShapeNodes with physics bodies in it and they stack over one another.
When a certain height is reached, I move the worldNode down.
Now here is where the problem occurs. When I move the worldNode down, it disturbs the already placed physics bodies and it makes the tower fall quite number of times.
Coming from a Cocos2D background, I used to move the whole scene there and it did not create any problem there.
Help will be appreciated. Thank you.
Instead of moving the world node you should move the SKCameraNode into the opposite direction.

Using CCParticleFire with CCFollow

I use CCFollow to keep player position center of screen.
And, I also use CCParticleFire as player's child node.
Problem is, when player runnning, particle don't represent particle's afterimage.
(Particles was not affected by player's move.)
(Particles remain several seconds in position where player was standing if I don't use CCFollow.)
I want to affect player's move to particles.
Anyone knows how to do this?

To follow a ball character like monkey kick off game

I am working on a game which has working similar to game monkey kick off. I have one ball bouncing on a place at the left side of screen and depending upon the position of the ball I apply linearImpulse to the ball on user touch so that it appears that the ball is kicked.
But what happens is when i apply impulse the ball goes out of the screen bounds. I dont want the ball to go out of the horizontal screen bounds whereas it can go out of the screen vertically.I tried using CCFollow but it doesn't give the realistic feel to the game flow.
I tried THIS tutorial, but dint help much.I managed to scroll the background,Only this part remains.
Any Ideas on how the ball should not move out of the horizontal screen boundary..? but in case of vertical boundary on the other hand it can go out of the bounds.
If you're not using physics, it's a simple bounds check. Assuming the screen width is 320 points this will keep the ball inside the screen horizontally:
CGPoint ballPos = ball.position;
ball.position.x = fmaxf(0, fminf(320, ballPos.x));
ball.position = ballPos;
UPDATE: I noticed you mentioned linear impulse. So you're using a physics engine. In that case, create a horizontal wall on both sides. See the cocos2d Box2D or Chipmunk templates how to enclose the screen with a collision border, then use only the left and right side borders in your game.

mouse joint is not working to restrict the ball in the half part of the screen

Hi guys I Am developing the application in cocoas2d using the box 2d frame work but unfortunately
i'm not able to restrict the gray ball in the half screen area of the image shown here
i want that ball not to go opposite part of the screen
I Have Used The b2Mousejoint For to move the ball around the screen
b2PrismaticJointDef restrict on any particular axis
But
i want to restrict on the particular rect area of the screen
You could create your custom distance joint which will restrict global axes of the ball. But it will be hard if you never write your own physics engine.
There are 2 easier ways to implement what you want.
Create 4 static "border" boxes around the area where the ball must stay. Then place the ball and the boxes into one collision group.
However, the response of the "border" boxes will not be instant. Therefore, the ball at high speed will sometimes "sink" into the boxes, then be popped out.
You can correct the position and reset the speed of the ball manually in code when it crosses the bounds of the desired area. But it may lead to the unstable physics simulation.