Reputation: 27
So i am trying to create a fixed movement for an image, but i can't seem to make it work correctly. The first block is activate by a timer but I don't know how to turn it off and activate another timer to continue the curve.
BMR = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(BallArchR) userInfo:nil repeats:YES];
LMR = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(BallArchL) userInfo:nil repeats:YES];
-(void)BallArchR{
if (Ball.center.x < 284){
Ball.center = CGPointMake(Ball.center.x + 2.72 , Ball.center.y - 2 );
}
if (Ball.center.x >= 284 && Ball.center.x < 488) {
Ball.center = CGPointMake(Ball.center.x + 2.72 , Ball.center.y + 2 );
}
}
-(void)BallArchL{
if (Ball.center.x > 284){
Ball.center = CGPointMake(Ball.center.x - 50 , Ball.center.y - 50 );
}
if (Ball.center.x <= 284 && Ball.center.x > 80) {
Ball.center = CGPointMake(Ball.center.x - 2.72 , Ball.center.y + 2 );
}
}
This is the code to make the ball move in a curve, and BallArchR is activated when the game starts up. But i can't turn it off and activate BallArchL. Is there another way to make this work?
Upvotes: 0
Views: 302
Reputation: 74
When you need to deactivate timer, first check if the timer is valid or not then deactivate by using "invalidate" method.
if ([BMR isValid])
{
[BMR invalidate];
}
Upvotes: 1
Reputation: 2451
i think one timer is enough for this
BMR = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(BallArchR) userInfo:nil repeats:YES];
-(void)BallArchR
{
if (Ball.center.x < 284)
{
Ball.center = CGPointMake(Ball.center.x + 2.72 , Ball.center.y - 2 );
}
if (Ball.center.x >= 284 && Ball.center.x < 488)
{
Ball.center = CGPointMake(Ball.center.x + 2.72 , Ball.center.y + 2 );
}
else
{
[self BallArchL ];
}
}
-(void)BallArchL
{
if (Ball.center.x > 284)
{
Ball.center = CGPointMake(Ball.center.x - 50 , Ball.center.y - 50 );
}
if (Ball.center.x <= 284 && Ball.center.x > 80)
{
Ball.center = CGPointMake(Ball.center.x - 2.72 , Ball.center.y + 2 );
}
else
{
[BMR invalidate];
}
}
i'm answered this based on what i have understand,if it not what you want comment here your requirement clearly...;)
Upvotes: 1
Reputation: 138
- (void)stopTimer
{
if ([_LMR isValid])
{
[_LMR invalidate];
}
}
You can use this for stop timer.
Also for animation you can use
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
//move your view
[UIView commitAnimations];
or
[UIView animateWithDuration:duration
animations:^{
//move your view
}
];
Upvotes: 1