3
votes

I'm trying to build a pinball game with spritekit. The ball is affected by gravity and reacts properly, but I can't figure out a way to move the flippers so that they affect the ball's physics. Right now I'm rotating the flippers around an anchor with an SKAction like so

//set anchor point to left edge for rotation
theLeftFlipper.anchorPoint = CGPointMake(0,0.5);

//rotate (done in touchesBegan)
SKAction *rotateLeft = [SKAction rotateByAngle: -1.5f duration:0.1];
[leftFlipper runAction:rotateLeft]

I'm using this to toggle the flipper as up or down around a pivot on its edge, as would be expected with a normal pinball machine's flippers. There are two problems with this. First, the physicsBody of the flipper isn't actually moved to match up with the new anchor of the flipper, so the physics body isn't in the same location as the rendered flipper. Second, when rotating with an action the ball's position is affected but not it's velocity so hitting the ball with the flipper doesn't shoot it upwwards. Is there a better way to achieve this?

1

1 Answers

3
votes

Based on the little bit of code you posted, I suggest the following:

  1. If you haven't already, add skView.showsPhysics = YES; in your project's View Controller viewDidLoad. This will allow you to see the physics bodies of your objects.

  2. If the physics body for your flipper is not right, then use CGMutablePathRef to create a custom physics body or you can use this utility.

  3. Make sure you have collisions setup correctly for your flipper and the ball.

  4. Set the ball's and flipper's physics body restitution property to a higher level. This will create the more of a bounce when the two object collide.