简体   繁体   English

当图像以一定速度滚动时,如何在X方向上平铺图像?

[英]How can I tile an image in X direction while it is scrolling at a velocity?

Hey all i've been scowering the internet for days now, ive also been reading up on this etc and i am in desperate need of help. 嘿,我几天来一直在浪费互联网,我也一直在阅读此类内容,我迫切需要帮助。 I am writing a 2D game and I need the background to move and my character will be stationary and he'll jump over stuff. 我正在写一个2D游戏,我需要移动背景并且我的角色将保持静止并且他会跳过东西。

If anyone can push me in the right direction or even provide a snippet of code i would be more than greatful. 如果有人可以向正确的方向推动我,甚至提供一小段代码,我将不胜感激。

I think the easiest way to do this is to draw the background image twice as your character scrolls with something like this, but I'm not sure how to do it with HorizontalScrollView. 我认为最简单的方法是在角色滚动时像这样绘制两次背景图像,但是我不确定如何使用Horizo​​ntalScrollView绘制背景图像。

//x , y, width, height
Background b1 = new Background(0, screenHeight/2, screenWidth, screenHeight);
Background b2 = new Background(screenWidth, screenHeight/2, screenWidth, screenHeight);

//update loop
b1.x-=speed;
b2.x-=speed;

if(b1.x+b1.width/2<0){
   b1.x=b2.x+b2.width/2+b1.width/2;
}
if(b2.x+b2.width/2<0){
   b2.x=b1.x+b1.width/2+b2.width/2;
}

You could use this sort of method with multiple types of background images that you update at different speeds. 您可以将这种方法用于以不同速度更新的多种类型的背景图像。

For anyone who is still struggling with this I worked it out: 对于仍在为此奋斗的任何人,我都可以解决:

Download corona SDK and program with that its so much easier: 下载corona SDK和程序非常容易:

function scrollBackground(self,event)
if self.x < [Enter Background width in pixels] then
self.x = [Enter Background width in pixels]
else
            self.x = self.x - [Speed - low number like 5 is slow scrolling, high number like 18 is fast scrolling]
        end
    end 

Use the above function to for the scrolling background then add this underneath: 使用上面的函数作为滚动背景,然后在下面添加它:

function scene:enterScene(event)
background.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", background)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM