0

In my first scene I have a CGVector value that makes a ball bounce around the screen. When loading back to this screen (from a game over screen), the value has changed making the ball move much slower than I want.

First scene addBall method:

CGVector ballDirection = CGVectorMake (40,120);
[ball.physicsBody applyForce:ballDirection];

Moving back to the first scene from game over scene:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    MyScene *firstScene = [MyScene sceneWithSize:self.size];
    [self.view presentScene:firstScene transition:[SKTransition doorsOpenHorizontalWithDuration:0.5]];
    SKAction *playAgainSound = [SKAction playSoundFileNamed:@"opening.wav" waitForCompletion:NO];
    [self.scene runAction:playAgainSound];
}

Not sure why the CGVectorMake value appears to have changed as the addBall method will get called in initWithSize when transitioning back to the first scene.

I tried the following without success:

static CGFloat dx = 40;
static CGFloat dy = 120;
CGVector ballDirection = CGVectorMake(dx, dy);
[ball.physicsBody applyForce:ballDirection];
George_UK
  • 61
  • 6
  • you need to have a reference to to ballDirection because it's reinitialised taking a random value. or change it to static instead – Basheer_CAD May 19 '14 at 15:14
  • That does not make any sense. If you apply a set vector to your ball the first time, move to another scene, go back to the first scene and apply the same vector again - the ball will move at the same vector. Obviously your issue is not with the code you posted but something else. Review your code again step by step or post more relevant code. – sangony May 19 '14 at 23:32
  • If it is the same scene which you are coming back to, init will not get called. Also, where in the code are you calling applyForce? – ZeMoon May 20 '14 at 04:20
  • I found a solution. Instead of using [ball.physicsBody applyForce:BallDirection]; I used "applyImpulse" after testing, the ball reloads with the correct values. – George_UK May 20 '14 at 10:55

0 Answers0