## Discuss Scratch

- GreenExplosion08
- Scratcher

27 posts

### Scrolling forever with 1 sprite

I'm making a scrolling platformer and I need the background to scroll. The levels vary in size so I just want it to scroll forever.

This is the script that I need as the base:

But I need to somehow change the values so that it scrolls forever, but I don't know what to do. Can someone hep please?(It's 384 instead of 480 because I have barriers at the sides of the screen)

This is the script that I need as the base:

when green flag clicked

create clone of [myself v]

switch costume to [Forest 1 v]

forever

set x to ((ScrollX) + ((384) * (0)))

end

when I start as a clone

switch costume to [Forest 2 v]

forever

set x to ((ScrollX) + ((384) * (0)))

end

But I need to somehow change the values so that it scrolls forever, but I don't know what to do. Can someone hep please?(It's 384 instead of 480 because I have barriers at the sides of the screen)

*Last edited by GreenExplosion08 (Sept. 13, 2017 13:15:47)*

- shoresbeep
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

_x will need to be a local variable

when green flag clicked

set [Gameover variable v] to [0]

hide

set [_x v] to [0]

create clone of [myself v]

set [_x v] to [384]

create clone of [myself v]

when I start as a clone

show

switch costume to [costume you want v]

repeat until <(Gameover variable) = [1]>

go to (((ScrollX) mod (384)) - (_x)) y: [y-position you want]

end

*Last edited by shoresbeep (Sept. 13, 2017 14:13:33)*

- deck26
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

Each clone just needs an offset value. If you use a counter N then for clone N the offset is 384 * N.

See if this helps https://scratch.mit.edu/projects/92682141/

See if this helps https://scratch.mit.edu/projects/92682141/

- shoresbeep
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

what I did would allow for infinite scrolling, correct? Each clone just needs an offset value. If you use a counter N then for clone N the offset is 384 * N.

See if this helps https://scratch.mit.edu/projects/92682141/

- deck26
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

Your first clone would always have x position between 0 and 384, your second between -384 and 0. Not sure how that would appear in terms of scrolling.what I did would allow for infinite scrolling, correct? Each clone just needs an offset value. If you use a counter N then for clone N the offset is 384 * N.

See if this helps https://scratch.mit.edu/projects/92682141/

- shoresbeep
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

It works fine in my project for infinite scrolling, ultra player collab.Your first clone would always have x position between 0 and 384, your second between -384 and 0. Not sure how that would appear in terms of scrolling.what I did would allow for infinite scrolling, correct? Each clone just needs an offset value. If you use a counter N then for clone N the offset is 384 * N.

See if this helps https://scratch.mit.edu/projects/92682141/

The reason it works is because scroll x is not always equal to _x (i think)

- deck26
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

But you need to decide what you mean by scrolling forever. Does it repeat after a certain number of backdrops? Do you need consistency if you move so many screens right and then come back that number of screens? If not 2 or 3 clones might be enough but you need to decide whether all costumes match at the edge or only certain combinations make sense.

- deck26
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

But scrollx mod 384 has a fixed range 0 to 383.9999 so you must get the effect I described. One clone subtracts 0 so no effect and the other subtracts 384 giving a new x range of -384 to 0 (or just below 0 really).It works fine in my project for infinite scrolling, ultra player collab.Your first clone would always have x position between 0 and 384, your second between -384 and 0. Not sure how that would appear in terms of scrolling.

See if this helps https://scratch.mit.edu/projects/92682141/

The reason it works is because scroll x is not always equal to _x (i think)

I'm finding it hard to visualise how that would actually look. It might be fine for backdrops created with that in mind but doesn't feel like it would work generally.

- deck26
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

For two sprites covering from x=-200 to x=200 and costume width 380 you need the centre to cover the x range from -380 to 380. A total of 760 which suggests mod 760 is more appropriate. One sprite needs to be offset by 380 from the other so something like

All approximate values but the general idea works. If the width of the costumes is N then 2N is the value for the mod function and one costume needs offsetting by N and in either case the calculated value has N subtracted to give a full range for x of -N to N.

(((scrollx) mod (760)) - [380] ) //since mod gives 0 to 760

((((scrollx) + (380)) mod (760)) - [380] ) //offset by 380 from above

All approximate values but the general idea works. If the width of the costumes is N then 2N is the value for the mod function and one costume needs offsetting by N and in either case the calculated value has N subtracted to give a full range for x of -N to N.

- GreenExplosion08
- Scratcher

27 posts

### Scrolling forever with 1 sprite

For two sprites covering from x=-200 to x=200 and costume width 380 you need the centre to cover the x range from -380 to 380. A total of 760 which suggests mod 760 is more appropriate. One sprite needs to be offset by 380 from the other so something like(((scrollx) mod (760)) - [380] ) //since mod gives 0 to 760

((((scrollx) + (380)) mod (760)) - [380] ) //offset by 380 from above

All approximate values but the general idea works. If the width of the costumes is N then 2N is the value for the mod function and one costume needs offsetting by N and in either case the calculated value has N subtracted to give a full range for x of -N to N.

Ok, but how would using:

(((scrollX) mod (382)) - (384))

((((scrollX) + (384)) mod (382)) - (384))

Effect my scrolling usage of:

((scrollX) + ((382) * (0)))

((scrollX) + ((382) * (1)))

*Last edited by GreenExplosion08 (Sept. 13, 2017 20:50:50)*

- GreenExplosion08
- Scratcher

27 posts

### Scrolling forever with 1 sprite

(((scrollx) mod (760)) - [380] ) //since mod gives 0 to 760

((((scrollx) + (380)) mod (760)) - [380] ) //offset by 380 from above

All approximate values but the general idea works. If the width of the costumes is N then 2N is the value for the mod function and one costume needs offsetting by N and in either case the calculated value has N subtracted to give a full range for x of -N to N.

If the sprite was positioned in the center it would cover -191 to 191, not -384 to 384, so 382 would be appropriate but, I see what your trying to tell me but, how would using:(((scrollX) mod (382)) - (384))

((((scrollX) + (384)) mod (382)) - (384))

Effect my scrolling usage of:((scrollX) + ((382) * (0)))((scrollX) + ((382) * (1)))

Ok, I tried what you said and, no avail, is there anything I'm missing with the code you gave me?

- GreenExplosion08
- Scratcher

27 posts

### Scrolling forever with 1 sprite

Sorry, I accidentally had a + instead of a - .

- GreenExplosion08
- Scratcher

27 posts

### Scrolling forever with 1 sprite

It kinda works, If you count it just appearing when it all scrolls of screen as working.

- shoresbeep
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

Hmm, could you share the project so I can see? It kinda works, If you count it just appearing when it all scrolls of screen as working.

- shoresbeep
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

Nevermind this.

*Last edited by shoresbeep (Sept. 13, 2017 21:14:31)*

- deck26
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

My post referred to N and 2N as being the only numbers required so there is no reason for you to have 382 and 384 as @shoresbeep points out.

Your code using scrollx + n * 382 just means each clones only appears on screen if scrollx is within a particular range. Did you look at the link in my first post? If you understand the idea there of how when scrolling you're viewing a specific area of the ‘real world’. Unless you keep resetting scrollx to keep it within a confined range you're going to end up outside that viewing area most of the time. The mod option avoids this.

And @shoresbeep - the mod value is twice the width of one costume or you end up with too limited a range of values. If you had 3 sprite costumes for backdrop it would be 3 x the width of each. If a (properly centred) costume has width 380 (say) then for it to just appear at the left edge it would have an x-position of around -360 so the rightmost 20 pixels are on screen. On the right it would need to go up to 360 so only the leftmost 20 pixels show. That gives an x range of around 720 and if you use mod 380 and add/subtract a fixed value you just cannot get that range. You want the two costumes to cover the width indicated by the mod value.

Here's a demo https://scratch.mit.edu/projects/174415011/ - feel free to remix and show how mod 380 still works

Your code using scrollx + n * 382 just means each clones only appears on screen if scrollx is within a particular range. Did you look at the link in my first post? If you understand the idea there of how when scrolling you're viewing a specific area of the ‘real world’. Unless you keep resetting scrollx to keep it within a confined range you're going to end up outside that viewing area most of the time. The mod option avoids this.

And @shoresbeep - the mod value is twice the width of one costume or you end up with too limited a range of values. If you had 3 sprite costumes for backdrop it would be 3 x the width of each. If a (properly centred) costume has width 380 (say) then for it to just appear at the left edge it would have an x-position of around -360 so the rightmost 20 pixels are on screen. On the right it would need to go up to 360 so only the leftmost 20 pixels show. That gives an x range of around 720 and if you use mod 380 and add/subtract a fixed value you just cannot get that range. You want the two costumes to cover the width indicated by the mod value.

Here's a demo https://scratch.mit.edu/projects/174415011/ - feel free to remix and show how mod 380 still works

*Last edited by deck26 (Sept. 14, 2017 09:21:40)*

- GreenExplosion08
- Scratcher

27 posts

### Scrolling forever with 1 sprite

My post referred to N and 2N as being the only numbers required so there is no reason for you to have 382 and 384 as @shoresbeep points out.

Your code using scrollx + n * 382 just means each clones only appears on screen if scrollx is within a particular range. Did you look at the link in my first post? If you understand the idea there of how when scrolling you're viewing a specific area of the ‘real world’. Unless you keep resetting scrollx to keep it within a confined range you're going to end up outside that viewing area most of the time. The mod option avoids this.

And @shoresbeep - the mod value is twice the width of one costume or you end up with too limited a range of values. If you had 3 sprite costumes for backdrop it would be 3 x the width of each. If a (properly centred) costume has width 380 (say) then for it to just appear at the left edge it would have an x-position of around -360 so the rightmost 20 pixels are on screen. On the right it would need to go up to 360 so only the leftmost 20 pixels show. That gives an x range of around 720 and if you use mod 380 and add/subtract a fixed value you just cannot get that range. You want the two costumes to cover the width indicated by the mod value.

Here's a demo https://scratch.mit.edu/projects/174415011/ - feel free to remix and show how mod 380 still works

Can you please visualize it? I saw both projects and couldn't understand the math. In my project, would I write:

((scrollx) mod (384))

(((scrollx) mod (384)) + (384))

- GreenExplosion08
- Scratcher

27 posts

### Scrolling forever with 1 sprite

Tried the code in the demo and it worked!!! Thank you thank you thank you!!!

*Last edited by GreenExplosion08 (Sept. 14, 2017 12:10:58)*

- deck26
- Scratcher

1000+ posts

### Scrolling forever with 1 sprite

I can only repeat my previous post. Take the width of one background costume to get the value N. Then use 2*N as the value on the right hand side of the mod function. For one costume you just use scrollx mod 2N minus N and for the second you need to add or subtract a correction of N to scrollx before using the mod function.

scrollx mod 2N and (scrollx + N) mod 2N will always differ by N so the clones centres are separated by right distance.

The range of values for scrollx mod 2N is roughly 0 to 2N (we can't actually get 2N). But we need a clone to have the option of positive or negative x position with equal range either side which we achieve by subtracting N.

Let's assume, for ease of calculation, the costumes are 400 pixels wide. So 2N is 800 and N is 400.

If scrollx mod 800 - 400 = 0 we have the first clone at the centre of the screen. (scrollx - 400) mod 800 - 400 gives us -400 for the x position of the second clone so it is off-screen. As the first clone moves left the second jumps to the right ready to scroll in - if clone 1 has x=-20 then clone 2 has x=380 (still 400 apart).

if the first clone keeps moving left so does the second until the position is reversed - clone 2 is at x=0 and clone 1 is about to jump to the right.

((((scrollx) + (380)) mod (760)) - [380] ) //offset by 380 from above

All approximate values but the general idea works. If the width of the costumes is N then 2N is the value for the mod function and one costume needs offsetting by N and in either case the calculated value has N subtracted to give a full range for x of -N to N.

scrollx mod 2N and (scrollx + N) mod 2N will always differ by N so the clones centres are separated by right distance.

The range of values for scrollx mod 2N is roughly 0 to 2N (we can't actually get 2N). But we need a clone to have the option of positive or negative x position with equal range either side which we achieve by subtracting N.

Let's assume, for ease of calculation, the costumes are 400 pixels wide. So 2N is 800 and N is 400.

If scrollx mod 800 - 400 = 0 we have the first clone at the centre of the screen. (scrollx - 400) mod 800 - 400 gives us -400 for the x position of the second clone so it is off-screen. As the first clone moves left the second jumps to the right ready to scroll in - if clone 1 has x=-20 then clone 2 has x=380 (still 400 apart).

if the first clone keeps moving left so does the second until the position is reversed - clone 2 is at x=0 and clone 1 is about to jump to the right.