Help to fix a simple error in my first project (STEP 3 - Getting started tutorial ) (SOLVED)

Hello guys…

I’m quite new on Defold and im trying to replay the Getting started tutorial, but now using my own images assets …

right now im stuck on “STEP 3 - Make the ground moving”.

my background image has 768x384
i create a collection with 7 GO and each GO has an sprite
here is each X sprite position: 384,1152,1916,2680,3444,4208,4972

now my update Script:

 for i, p in ipairs(pieces) do 
        local pos = go.get_position(p)
        
        if pos.x <= -384 then 
            
            pos.x = 4972  + (pos.x + 384)
            --print(pos)
        end
        
        pos.x = pos.x - self.speed
        go.set_position(pos, p) 
    end

but when i run the project after a time there is a long space without background images…

What am I doing wrong?

obs: if it is necessery i can share my project.

4972 sounds high if your background is 768 pixels wide.

Can you post some screenshots?

I’m guessing your screen width is 768, like your background. Is this what you are looking for?

if pos.x <= -384 then
    pos.x = pos.x + 768 * 2
end

“When the pos is half the screen to the left (-384), which means that the sprite is completely off screen, increment the x-value by two screens (2 * 768) so that it is completely off to the right, before moving it left again”

To improve the code further you should define variables for things like width etc so you don’t repeat naked constants (e.g. 768) everywhere in the code, it makes it easier if you want to change the width later on. A function could also make it easier to read.

local width = 768
local function wrap_right(pos)
    if pos.x <= (-0.5 * width) then
         pos.x = pos.x + width * 2
    end
end

And in the code you posted:

local pos = go.get_position(p)
wrap_right(pos)
pos.x = pos.x - self.speed
go.set_position(pos, p)

Edit: Oops, as I recall in that example, the ground-pieces are not as wide as the background, right? You need to factor in their widths as well if they are not 768. The code I posted might not work exactly as is, but should be close. Something like this?

local screen_width = 768
local piece_width = <some value>
local function wrap_right(pos)
    if pos.x <= (-0.5 * piece_width) then
         pos.x = pos.x + screen_width + piece_width
    end
end

I think jhonatanvinicius is scrolling backgrounds instead of the ground sprite, if so, remove the

  • (pos.x + 384) as this is adding half a screen again to the position of 4972 which you’ve already calculated in advance that is the position to move to.

Also if each background is 768 pixels then your values should be 384,1152,1920,2688,3456,4224,4992.
Ragnar’s advice about coding is spot on, but this is something that will come with time.

Hi Sicher as i said before, im just trying to rebuld the Getting started tutorial, but now using my own images assets …

As you ask follow my prints::

The black zone:

Thank you for your help!strong text

hi Ragnar, thankyou for your answer,

i did a test with your values but but the results was the same… :frowning:

if pos.x <= -384 then
    pos.x = pos.x + 768 * 2
end

But thanks anyway.

Hi Dajev,

you was right. i updated my sprites X positions.

And i modified my sript to, but didn’t work yet.

remove the

  • (pos.x + 384) as this is adding half a screen again to the position
function update(self, dt) 
    for i, p in ipairs(pieces) do 
        local pos = go.get_position(p)
        
        if pos.x <= -384 then 
            
            pos.x = 4992 + 384  -- + (pos.x + 384)
            --print(pos)
        end
        
        pos.x = pos.x - self.speed
        go.set_position(pos, p) 
    end
end

Yes, but you added back in the +384, 4992 is already the correct position, remove the +384

haha sorry man. i posted a old test code.

BTW i recorded a short video(2min) to show the problem: https://youtu.be/6rgHuj9n1bo

Ok, looking at the video, I would check the following, the go’s and sprites for cenario3 and onwards, also check their z orders are set to 0

If you haven’t found the issue yet I can take a look. Synchronize your files and add me (bjorn.ritzl@king.com) as a team member in the Defold Dashboard.

1 Like

ok britzl. i sent a invite for you. Thank you very much.

Ah, I see what you’ve done now. You have changed the position of the sprites, not the game objects. All game objects are on 0,0 and the sprites are offset. The sprites should all be at 0,0 and you should offset the entire game objects since those are the ones that you move and check positions against.

Oh man!!! that is true!. haha there are some many details that i got confused. Now it is everything working.

Thanks!