Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping moving background objective-C

I am testing a background-loop animation where there will be to images both 1024x768 pixels in dimension, move leftwards, go offscreen, then jump back to the other side, and repeat.

I was able to do this by creating a constant speed for both background image to move (successful), and then I tried the following code to make it jump, but there was a problem:

    if((background.center.x) < -511){
        background.center = CGPointMake(1536, background.center.y);
    }

    if((background2.center.x) < -511){
        background2.center = CGPointMake(1536, background2.center.y);
    }

Somehow this is not working the way I expected. It leaves a few pixels of gap every time, and I am confused why. Does anyone know what's causing this to happen and how to fix it? Thanks!

like image 497
Ninjiangstar Avatar asked Nov 27 '25 01:11

Ninjiangstar


1 Answers

It seems like you have forgotten to take into account the distance moved. The greater than expression might have been triggered because you moved to far. I guess your movement is larger than 1 pixel/frame.

I am not sure what kind of values that are feeding your movement but I think to take into account the movement you should do something like...

 if ((background.center.x) < -511){
    CGFloat dist = background.center.x + 512;
    background.center = CGPointMake(1536+dist, background.center.y);
 }

 if ((background2.center.x) < -511){
    CGFloat dist = background2.center.x + 512;
    background2.center = CGPointMake(1536+dist, background2.center.y);
 } 
like image 162
epatel Avatar answered Nov 28 '25 16:11

epatel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!