循环移动背景objective-C

发布于 2024-12-07 08:24:21 字数 471 浏览 0 评论 0原文

我正在测试一个背景循环动画,其中尺寸均为 1024x768 像素的图像,向左移动,离开屏幕,然后跳回另一侧,然后重复。

我能够通过为两个背景图像移动创建恒定的速度(成功)来做到这一点,然后我尝试使用以下代码使其跳跃,但出现了一个问题:

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

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

不知怎的,这并没有按照我预期的方式工作。它每次都会留下几个像素的间隙,我很困惑为什么。有谁知道是什么原因导致这种情况发生以及如何解决?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2024-12-14 08:24:21

您似乎忘记考虑移动的距离。由于您移动到较远的位置,可能会触发大于表达式。我猜你的运动大于 1 像素/帧。

我不确定什么样的价值观正在滋养你的运动,但我认为考虑到你的运动,你应该做一些事情......

 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);
 } 

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);
 } 
夜光 2024-12-14 08:24:21

我不会让两个图像(某种程度上)独立移动,而是跟踪一个 backgroundPosition 变量,然后不断更新两个图像相对于该位置的位置。这应该使一切保持整洁:

CGFloat const backgroundWidth = 1024;
CGFloat const backgroundSpeed = 2;

- (void)animateBackground {
    backgroundPosition -= backgroundSpeed;
    if (backgroundPosition < 0) {
        backgroundPosition += backgroundWidth;
    }
    background1.center.x = backgroundPosition - backgroundWidth/2;
    background2.center.x = backgroundPosition + backgroundWidth/2;
}

Rather than have the two images move (sort of) independently, I would keep track of a single backgroundPosition variable and then constantly update the position of both images relative to that one position. This should keep everything nice and tidy:

CGFloat const backgroundWidth = 1024;
CGFloat const backgroundSpeed = 2;

- (void)animateBackground {
    backgroundPosition -= backgroundSpeed;
    if (backgroundPosition < 0) {
        backgroundPosition += backgroundWidth;
    }
    background1.center.x = backgroundPosition - backgroundWidth/2;
    background2.center.x = backgroundPosition + backgroundWidth/2;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文