Domanda

I am making a game and I am trying to get the Play button to slide onto the screen when the view loads and stop when it reaches the center (rather than the button just appearing on the screen already when the view loads).

I have the following code in my viewDidLoad function:

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

while(playButton.center.x != 71) 
    playButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.002 target:self selector:@selector(playButtonMove) userInfo:nil repeats:YES];

}

All that happens at the moment is when I run the app, the screen just stays black and when I close the app, a window appears in Xcode saying 'Terminated due to memory error'.

Edit: Here is my playButtonMove function:

-(void)playButtonMove {
playButton.center = CGPointMake(playButton.center.x - 1, playButton.center.y);
}
È stato utile?

Soluzione

Remove:

while(playButton.center.x != 71) 
    playButtonTimer = [NSTimer scheduledTimerWithTimeInterval:0.002 target:self selector:@selector(playButtonMove) userInfo:nil repeats:YES];

And add this to your file:

- (void)viewDidAppear:(BOOL)animated {
    [UIView animateWithDuration:.25 animations:^{
        playButton.center = CGPointMake(71, playButton.center.y);
    }];
}

Your app is running forever because you reallocate playbuttontimer before it has a chance to fire. Because of this, your while statement will always be true. Basically all your app is doing right now is creating thousands and thousands of NSTimers until it crashes. What you're doing should not be used for animation. Use apple's built in animation api's.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top