循环移动背景objective-C
我正在测试一个背景循环动画,其中尺寸均为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎忘记考虑移动的距离。由于您移动到较远的位置,可能会触发大于表达式。我猜你的运动大于 1 像素/帧。
我不确定什么样的价值观正在滋养你的运动,但我认为考虑到你的运动,你应该做一些事情......
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...
我不会让两个图像(某种程度上)独立移动,而是跟踪一个
backgroundPosition
变量,然后不断更新两个图像相对于该位置的位置。这应该使一切保持整洁: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: