ShadowMario / FNF-PsychEngine

Engine originally used on Mind Games mod
Apache License 2.0
1.11k stars 2.18k forks source link

how do i make a scrolling background with lua #8418

Closed apleGitHub closed 2 years ago

apleGitHub commented 2 years ago

Describe your problem here.

i want to make my background infinitely scroll and loop with lua. how do i do that?

Are you modding a build from source or with Lua?

Lua

What is your build target?

Windows x64

Did you edit anything in this build? If so, mention or summarize your changes.

No response

Grape-Boy commented 2 years ago

You need to create a background sprite, then make an identical one positioned at the end of the first sprite, then you tween the two sprites together at the same time. When the sprites finish moving, you reset the positions of the sprites and tween them again so that it loops.

You can use this script :


local width = 0
local spriteName = "limoSunset" -- replace limoSunset with the name of your sprite

function onCreate()
    makeLuaSprite("bg1", spriteName, 0, 0)
    width = getProperty("bg1.width")

    makeLuaSprite("bg2", spriteName, width-20, 0)

    addLuaSprite("bg1", false)
    addLuaSprite("bg2", false)

    doTweenX("bg1Tween", "bg1", -width, 5, "linear")
    doTweenX("bg2Tween", "bg2", -20, 5, "linear")
end

function onTweenCompleted(tag)
    if tag == "bg1Tween" then
        setProperty("bg1.x", 0)
        setProperty("bg2.x", width-20)

        doTweenX("bg1Tween", "bg1", -width, 5, "linear")
        doTweenX("bg2Tween", "bg2", -20, 5, "linear")
    end
end

I used this image :

limoSunset