Question

I want a ball-sprite jump off the ground each time to the same height. But with every jump the maximum height position of the ball increases.

-(id)initWithSize:(CGSize)size {    
  if (self = [super initWithSize:size]) {

      SKNode *ground = [SKNode node];
      ground.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointZero toPoint:CGPointMake(CGRectGetMaxX(self.frame), 0)];
      [self addChild:ground];

      SKShapeNode *ball = [[SKShapeNode alloc] init];
      CGMutablePathRef myPath = CGPathCreateMutable();
      CGPathAddArc(myPath, NULL, 0, 0, 30, 0, M_PI*2, YES);
      ball.path = myPath;
      ball.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame)-100);
      ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];
      ball.physicsBody.linearDamping = 0.0;
      ball.physicsBody.restitution = 1.0;
      [self addChild:ball];

  }
  return self;

}

Any suggestions?

Was it helpful?

Solution

Look at setting both the restitution and friction on both bodies, not just the ball. If it's growing and not shrinking though, there is likely an error with the physics bodies. SKShapeNode is notoriously unreliable in my testing - I would look into using SKSpriteNode instead to maintain your sanity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top