Search code examples
iosobjective-csprite-kitskphysicsbody

Best way to manually move/rotate a physicsBody in SpriteKit iOS


What is the best way to manually move or rotate a physicsBody in SpriteKit? The move will be invoked when the user touches the screen.?

An important requirement is that the physicsBody must move to and stay in the new position/rotation until the touch has ended. That being said, simply applying an impulse will not suffice.

Help would be appreciated.


Solution

  • two ways you can rotate and move a body

    1)

    ontouch for rotation

    sprite.physicsBody.allowsRotation=TRUE;

    ontouch for change x

    -(void)didSimulatePhysics {

    sprite.physicsBody.velocity=CGVectorMake(50, 0);
    

    }

    and second way you can do it by skAction but make sure your physics body is not dynamic

    [SKAction moveBy:<#(CGVector)#> duration:<#(NSTimeInterval)#>];
    [SKAction rotateByAngle:<#(CGFloat)#> duration:<#(NSTimeInterval)#>];
    

    give a name to every action and remove it when a touch ended