Question

I need to make run this animation from left to right continuously. Now Is working, but show 1 time only (when the game is loaded). I want to make this animation run continously, left to right continuously. Timer and speed is already working. Here my current code:

- (void)airplane1Code {
    airplane1.center = CGPointMake(airplane1.center.x + 10, airplane1.center.y);
}

Any suggestion? Thanks

Était-ce utile?

La solution

You can create a UIView animation with the UIViewAnimationOptionRepeat

airplane1.center = CGPointMake(startX, startY);
[UIView animateWithDuration:10.0 //10seconds
                      delay: 0
                    options: UIViewAnimationOptionRepeat
                 animations:^{
                     airplane1.center = CGPointMake(endX, endY);
                 }
                 completion:nil];

That will make it do the same animation from start to end over and over. In order to stop the animation, call

[airplane1.layer removeAllAnimations];

Autres conseils

If you want the airplane to show up on the start side after it disappears from the end side, just reset the center off screen on the start side once it passes the window's frame. If you want another plane to show up and start flying after that one has left, then add another plane object and move them at the same time.

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

    -(void) airplane1Code{
        airplane1.center = CGPointMake(airplane1.center.x + 10, airplane1.center.y);
        if(airplane1.center > screenWidth + halfTheImageSize)
           // if the plane is completely off the screen, move the image to other side
           // and keep running this method with your loop
           airplane1.center = CGPointMake(0 - halfTheImageSize, airplane1.center.y);
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top