Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit 1 Dimensional Movement

Tags:

sprite-kit

I'm using apple's Sprite Kit and I need to move a SKSprite Node in horizontal movement only. I want the rest of the physics to apply but only in the horizontal component.

Context: This is for an object supposedly on a slider that can bounce back and forth. I have everything done but if it is hit the end the wrong way it simply floats off vertically, how can I simply make it ignore all forces in the vertical direction.

like image 484
Jordan Avatar asked Mar 05 '26 23:03

Jordan


1 Answers

By putting the node's position back at the desired Y coordinate every frame after physics has been simulated:

-(void) didSimulatePhysics
{
    CGPoint pos = horizontalMoveNode.position;
    pos.y = fixedVerticalPosY;
    horizontalMoveNode.position = pos;
}

Add this method to your scene class and apply it to whichever node(s) you want to lock in at a given Y coordinate.

like image 174
LearnCocos2D Avatar answered Mar 08 '26 20:03

LearnCocos2D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!