This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#126 2009-01-24 10:24:05

FoxDur
Scratcher
Registered: 2008-07-07
Posts: 87

Re: Scrolling Background - Help.

Thank you! I'll use this on my next scrolling game.


Do you want an icon? Check out here. Bored? Do you want to animate something? Look here.

Offline

 

#127 2009-01-29 11:53:20

eliasm
Scratcher
Registered: 2008-04-21
Posts: 80

Re: Scrolling Background - Help.

archmage wrote:

I am going to try and explain how to scroll stuff in scratch but you better be good at programming in scratch before you try this. I do not recommend a scrolling game as your first project.

There are 2 methods of scrolling I use. Method 1 uses many sprites, is easy to implement but can cause lag if the area is too large. Method 2 uses just 2 sprites but does not lag.

METHOD 1: SCROLLING WITH MANY SPRITES
For a demonstration of this method go to http://scratch.mit.edu/projects/archmage/76150

Ok first create a new variable called scrollX

        <{  scrollX  }>

Now that we have that we will need to have our scrolling sprites.
So make a sprite called terrian0.

*It is important to name the sprite terrain0 instead of terrain1 to check and adjust a value used to scroll.*

Now some of these numbers may vary in value depending on the size of your sprite. This assumes your sprite is as wide as the screen.

The number 480 in this code block represents the stage's total width.

//This should be placed on the sprite "terrain0"
<when green flag clicked>

<forever>
<set x to( (( <{ scrollX }>  <+>  ((  480 <*>  0 )) ))

Now the 480 * 0 bit is not necessary in the first sprite you want to scroll but we will keep it in there so we can use it when we duplicate the sprite using the duplicate tool. What that number actually represents is the number at the end of the sprite's name so for the sprite terrain1 you would put

<forever>
<set x to( (( <{ scrollX }>  <+>  ((  480 <*>  1 )) ))

and for terrain2 you would put a 2 instead of a 1 and so on.

Now the number at the end of the sprites name will dictate the order of the sprites so if you keep moving right you will first see terrain1 then terrain2 then terrain3 ect.


now here is a simple script that demonstrates how to alter the scrollX variable to make your sprites scroll.
This code should be placed on the stage. Using this code on multiple sprites is not necessary and not recommended.

<when green flag clicked>

<forever if> <key[ right arrow  ]pressed?>
<change{  scrollX }by( 5


<forever if> <key[ left arrow  ]pressed?>
<change{  scrollX }by( -5

METHOD 2:SCROLLING WITH 2 SPRITES
For a demonstration of this method go to http://scratch.mit.edu/projects/archmage/82995

You should already be comfortable using the first scrolling method as this method is more difficult

You will need to create 3 different variables for this method

  <{  scrollX  }>    //This variables keeps track of how far the user has scrolled

  <{ terrainNum  }>  //This is used to let the terrain sprite know what terrain should be displayed
 
<{  xVelocity  }>  //For some reason I was unable to get linear movement to make the terrain sprite not flicker with this method. However when the scrolling variable is changed according to the xVelocity variable the scrolling works as it should and does not flicker.

*Place these scripts on the stage*

<when green flag clicked>
<forever>
<if>  <( (( <round( <{ scrollX }>  </> 480 )) <>> (( <{ scrollX }>  </> 480 )) )>
<set{ terrainNum }to( (( <round(  (( <{ scrollX }>  </> -480 )) <+> 1 ))
<else>
<set{ terrainNum }to( <round(  (( <{ scrollX }>  </> -480 ))

What this script does is it divides the scroll variable by the stage's width and then rounds it down to the nearest whole number.

<when green flag clicked>
<forever>
<if> <key[ right arrow ]pressed?>
<change{ xVelocity }by( -2
<end>
<if> <key[ left arrow ]pressed?>
<change{ xVelocity }by( 2
<end>
<set{ xVelocity }to(  (( <{ xVelocity }> <*> 0.9  ))
<change{ scrollX }by( <{ xVelocity }>
<end>

This is a basic movement code that uses the xVelocity variable to change the scrollX variable.


Now before we begin programming on the terrain sprites it is very important to remember that BOTH of the two terrain sprites must have exactly the same costumes in exactly the same order. Now doing this will result in weird changing of the terrain sprites to incorrect costumes.

Make sure you have created 2 terrain sprites called "terrain1" and "terrain2".

Now put this code on terrain1.

<when green flag clicked>
<forever>
<switch to costume[ <{ terrainNum }>
<set x to( (( <{ scrollX  }> <+>  (( 480 <*> (( <{ terrainNum }> <-> 1 )) ))))
<end>

What this script does is it sets it's costume according to the terrainNum variable and then it sets it's x position to scrollX+(480*(terrainNum-1)). When the terrainNum variable changes it makes the terrain scripts change costumes accordingly and move to the proper position.

Now put this code on terrain2.

<when green flag clicked>
<forever>
<switch to costume[ (( <{ terrainNum }> <+> 1 ))
<set x to( (( <{ scrollX  }> <+>  (( 480 <*> <{ terrainNum }> ))))
<end>

This script is similar to the script on the first terrain sprite but it has been altered to appear in front of the terrain1 sprite.

Both methods when used correctly will produce a scrolling effect.

If you want to see examples of projects that feature scrolling go to this gallery.
http://scratch.mit.edu/galleries/view/8494

that's good advice but here is a more simple way to do it without variables:  tongue  :

ON THE BACKGROUND YOU WANT TO SCROLL ON

first,make a sprite that is little. This represents your moving background.

Then,make it BIG.

Then,put this

<when[ right arrow ]key pressed>
<change x by( 3)>

Then,this

<when[ left arrow  ]key pressed>
<change x by( -3)>

Then,if you want some mario song...

<when green flag clicked>
<forever>
<play sound[ Mario song ]and waits>
<end>

But this is optional.

then,on the sprite.

<when[  right arrow ]key pressed>
<point in direction( 90)>

and you can add <next costume> if you want to make an animated run.

and then,
<when[  left arrow ]key pressed>
<point in direction( -90)>

So that's all!


Did you know that 100,000 students carry a gun to school? All of that because every 7 minutes a child is bullied. Stop bullying now.

Offline

 

#128 2009-01-29 13:07:51

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Scrolling Background - Help.

Well, you can't really make sprites too big because they won't get displayed.

But if that works for you then that is great.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#129 2009-01-31 17:59:55

frogger3140
Scratcher
Registered: 2008-12-15
Posts: 500+

Re: Scrolling Background - Help.

FoxDur wrote:

Thank you! I'll use this on my next scrolling game.

me too!


http://j.mp/piBfFy
Woo, even better minecraft clone! | I feel that programming is better than animation, because you can interact with games but not animations.

Offline

 

#130 2009-02-05 01:53:41

floppy_gunk
Scratcher
Registered: 2008-11-14
Posts: 500+

Re: Scrolling Background - Help.

How to easily make a four-background mouse-scrolling project (it is SO easy!):

First make four sprites as big as the background.
On each sprite, go to the paint editor and click on 'set rotation center'.
set the rotation center on one of the sprites to the bottom left corner, on another the bottom right corner, etc. Then on each sprite, do this:

[blocks]<when green flag clicked>
<forever>
<go to x sad  (( 0 <-> <mouse x> )) )y sad  (( 0 <-> <mouse y> ))
<end>[/blocks]

And hey presto! You have made a mouse-scrolling project!

Last edited by floppy_gunk (2009-02-05 13:40:40)


http://img163.imageshack.us/img163/1917/2856lg.jpg Get it now!  smile

Offline

 

#131 2009-02-05 07:07:26

bosox397
Scratcher
Registered: 2008-02-17
Posts: 1000+

Re: Scrolling Background - Help.

floppy_gunk wrote:

How to easily make a four-background mouse-scrolling project (it is SO easy!):

First make four sprites as big as the background.
On each sprite, go to the paint editor and click on 'set rotation center'.
set the rotation center on one of the sprites to the bottom left corner, on another the bottom right corner, etc. Then on each sprite, do this:

<when green flag clicked>
<forever>
<go to x sad  (( 0 <-> <mouse x> )) )y sad  (( 0 <-> <mouse y> ))
<end>

And hey presto! You have made a mouse-scrolling project!

and then if you wanted more than one mouse scrolling project, u do the same thing but go to x: 0-mouse x plus 478  go to y: 0-mouse y

this would put a piece of terrain to the right.


Dear Scratch Users,
I'm done with scratch, or at least making projects. I have made one last big game, thats both fun and teaches a lesson about water. It'd mean a lot if you gave me feedback.                              http://scratch.mit.edu/projects/bosox397/569201

Offline

 

#132 2009-02-05 13:48:39

floppy_gunk
Scratcher
Registered: 2008-11-14
Posts: 500+

Re: Scrolling Background - Help.

bosox397 wrote:

floppy_gunk wrote:

How to easily make a four-background mouse-scrolling project (it is SO easy!):

First make four sprites as big as the background.
On each sprite, go to the paint editor and click on 'set rotation center'.
set the rotation center on one of the sprites to the bottom left corner, on another the bottom right corner, etc. Then on each sprite, do this:

[blocks]<when green flag clicked>
<forever>
<go to x sad  (( 0 <-> <mouse x> )) )y sad  (( 0 <-> <mouse y> ))
<end>[/blocks]

And hey presto! You have made a mouse-scrolling project!

and then if you wanted more than one mouse scrolling project, u do the same thing but go to x: 0-mouse x plus 478  go to y: 0-mouse y

this would put a piece of terrain to the right.

But then you would have to use this code instead:

[blocks]<when green flag clicked>
<forever>
<go to x sad  (( 0 <-> (( <mouse x> <*> 2 )) )) )y sad  (( 0 <-> (( <mouse y> <*> 2 )) ))
<end>[/blocks]


http://img163.imageshack.us/img163/1917/2856lg.jpg Get it now!  smile

Offline

 

#133 2009-02-05 13:50:00

TheCatAndTheBanana
Scratcher
Registered: 2008-03-21
Posts: 1000+

Re: Scrolling Background - Help.

floppy_gunk wrote:

bosox397 wrote:

floppy_gunk wrote:

How to easily make a four-background mouse-scrolling project (it is SO easy!):

First make four sprites as big as the background.
On each sprite, go to the paint editor and click on 'set rotation center'.
set the rotation center on one of the sprites to the bottom left corner, on another the bottom right corner, etc. Then on each sprite, do this:

[blocks]<when green flag clicked>
<forever>
<go to x sad  (( 0 <-> <mouse x> )) )y sad  (( 0 <-> <mouse y> ))
<end>[/blocks]

And hey presto! You have made a mouse-scrolling project!

and then if you wanted more than one mouse scrolling project, u do the same thing but go to x: 0-mouse x plus 478  go to y: 0-mouse y

this would put a piece of terrain to the right.

But then you would have to use this code instead:

[blocks]<when green flag clicked>
<forever>
<go to x sad  (( 0 <-> (( <mouse x> <*> 2 )) )) )y sad  (( 0 <-> (( <mouse y> <*> 2 )) ))
<end>[/blocks]

well, if you dig around my profile, you find a mouse scrooler, very basicly made.


My Newest Project!
Ceta? wat r u doin? CETA! STAHP!

Offline

 

#134 2009-02-13 04:11:59

Maki-Tak
Scratcher
Registered: 2008-03-10
Posts: 100+

Re: Scrolling Background - Help.

I had the same problem as you. This really helped me-  http://scratch.mit.edu/projects/Chrischb/339757


http://i.cubeupload.com/zZE0XJ.png

Offline

 

#135 2009-02-16 17:20:15

sailorgrl
Scratcher
Registered: 2009-02-07
Posts: 20

Re: Scrolling Background - Help.

I used scrolling method one, however, after one sucessful attempt, the first sprite that is scrolling goes right to the end after the green flag is clicked. What should I do?

Offline

 

#136 2009-02-16 17:50:21

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Scrolling Background - Help.

Sailorgrl, try setting your scrollX variable to 0 when the flag is clicked.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#137 2009-02-16 18:57:42

sailorgrl
Scratcher
Registered: 2009-02-07
Posts: 20

Re: Scrolling Background - Help.

Thanks, it worked! My problem now is that the following sprites won't scroll!
I tried <forever>                   but it did not work!
            <set{ scrollx }to( 0

Offline

 

#138 2009-02-16 19:39:39

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Scrolling Background - Help.

Just try the scrolling example that comes with scratch.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#139 2009-02-17 11:58:59

djm111
Scratcher
Registered: 2008-10-29
Posts: 1000+

Re: Scrolling Background - Help.

sailorgrl wrote:

Thanks, it worked! My problem now is that the following sprites won't scroll!
I tried <forever>                   but it did not work!
            <set{ scrollx }to( 0

It won't scroll because your scrolling variable is always at 0.
Just do When Green flag clicked, set scroll x to 0. Don't use the forever bit.


http://i627.photobucket.com/albums/tt358/Dazzamcgazza/sig.png

Offline

 

#140 2009-02-25 18:42:34

SUMCDM
Scratcher
Registered: 2009-02-13
Posts: 5

Re: Scrolling Background - Help.

Wow thanks archmage, thatr helped  tongue


SHWING. lol inside joke  big_smile   smile 
Check out some of my games and comment  big_smile   tongue

Offline

 

#141 2009-03-06 22:55:26

toto-tests
Scratcher
Registered: 2009-03-06
Posts: 1

Re: Scrolling Background - Help.

it wont work?<change{  }by(

Offline

 

#142 2009-04-11 10:24:34

Masterdan
Scratcher
Registered: 2009-03-28
Posts: 58

Re: Scrolling Background - Help.

I tried the 2nd guide but it didn't work. Can someone please try and fix it.

http://scratch.mit.edu/projects/Masterdan/485478

Also click on this for some jolly good games that do work  big_smile

http://scratch.mit.edu/users/Masterdan

Last edited by Masterdan (2009-04-11 10:25:51)


http://img145.imageshack.us/img145/7089/puzzleadvert.pnghttp://www.neopets.com/refer.phtml?username=dan_84_dan Click there for fun stuff and to get me free stuff!

Offline

 

#143 2009-04-14 22:27:51

ScratchMusicGroup
Scratcher
Registered: 2009-04-12
Posts: 100+

Re: Scrolling Background - Help.

archmage wrote:

I am going to try and explain how to scroll stuff in scratch but you better be good at programming in scratch before you try this. I do not recommend a scrolling game as your first project.

There are 2 methods of scrolling I use. Method 1 uses many sprites, is easy to implement but can cause lag if the area is too large. Method 2 uses just 2 sprites but does not lag.

METHOD 1: SCROLLING WITH MANY SPRITES
For a demonstration of this method go to http://scratch.mit.edu/projects/archmage/76150

Ok first create a new variable called scrollX

        <{  scrollX  }>

Now that we have that we will need to have our scrolling sprites.
So make a sprite called terrian0.

*It is important to name the sprite terrain0 instead of terrain1 to check and adjust a value used to scroll.*

Now some of these numbers may vary in value depending on the size of your sprite. This assumes your sprite is as wide as the screen.

The number 480 in this code block represents the stage's total width.

//This should be placed on the sprite "terrain0"
<when green flag clicked>

<forever>
<set x to( (( <{ scrollX }>  <+>  ((  480 <*>  0 )) ))

Now the 480 * 0 bit is not necessary in the first sprite you want to scroll but we will keep it in there so we can use it when we duplicate the sprite using the duplicate tool. What that number actually represents is the number at the end of the sprite's name so for the sprite terrain1 you would put

<forever>
<set x to( (( <{ scrollX }>  <+>  ((  480 <*>  1 )) ))

and for terrain2 you would put a 2 instead of a 1 and so on.

Now the number at the end of the sprites name will dictate the order of the sprites so if you keep moving right you will first see terrain1 then terrain2 then terrain3 ect.


now here is a simple script that demonstrates how to alter the scrollX variable to make your sprites scroll.
This code should be placed on the stage. Using this code on multiple sprites is not necessary and not recommended.

<when green flag clicked>

<forever if> <key[ right arrow  ]pressed?>
<change{  scrollX }by( 5


<forever if> <key[ left arrow  ]pressed?>
<change{  scrollX }by( -5

METHOD 2:SCROLLING WITH 2 SPRITES
For a demonstration of this method go to http://scratch.mit.edu/projects/archmage/82995

You should already be comfortable using the first scrolling method as this method is more difficult

You will need to create 3 different variables for this method

  <{  scrollX  }>    //This variables keeps track of how far the user has scrolled

  <{ terrainNum  }>  //This is used to let the terrain sprite know what terrain should be displayed
 
<{  xVelocity  }>  //For some reason I was unable to get linear movement to make the terrain sprite not flicker with this method. However when the scrolling variable is changed according to the xVelocity variable the scrolling works as it should and does not flicker.

*Place these scripts on the stage*

<when green flag clicked>
<forever>
<if>  <( (( <round( <{ scrollX }>  </> 480 )) <>> (( <{ scrollX }>  </> 480 )) )>
<set{ terrainNum }to( (( <round(  (( <{ scrollX }>  </> -480 )) <+> 1 ))
<else>
<set{ terrainNum }to( <round(  (( <{ scrollX }>  </> -480 ))

What this script does is it divides the scroll variable by the stage's width and then rounds it down to the nearest whole number.

<when green flag clicked>
<forever>
<if> <key[ right arrow ]pressed?>
<change{ xVelocity }by( -2
<end>
<if> <key[ left arrow ]pressed?>
<change{ xVelocity }by( 2
<end>
<set{ xVelocity }to(  (( <{ xVelocity }> <*> 0.9  ))
<change{ scrollX }by( <{ xVelocity }>
<end>

This is a basic movement code that uses the xVelocity variable to change the scrollX variable.


Now before we begin programming on the terrain sprites it is very important to remember that BOTH of the two terrain sprites must have exactly the same costumes in exactly the same order. Now doing this will result in weird changing of the terrain sprites to incorrect costumes.

Make sure you have created 2 terrain sprites called "terrain1" and "terrain2".

Now put this code on terrain1.

<when green flag clicked>
<forever>
<switch to costume[ <{ terrainNum }>
<set x to( (( <{ scrollX  }> <+>  (( 480 <*> (( <{ terrainNum }> <-> 1 )) ))))
<end>

What this script does is it sets it's costume according to the terrainNum variable and then it sets it's x position to scrollX+(480*(terrainNum-1)). When the terrainNum variable changes it makes the terrain scripts change costumes accordingly and move to the proper position.

Now put this code on terrain2.

<when green flag clicked>
<forever>
<switch to costume[ (( <{ terrainNum }> <+> 1 ))
<set x to( (( <{ scrollX  }> <+>  (( 480 <*> <{ terrainNum }> ))))
<end>

This script is similar to the script on the first terrain sprite but it has been altered to appear in front of the terrain1 sprite.

Both methods when used correctly will produce a scrolling effect.

If you want to see examples of projects that feature scrolling go to this gallery.
http://scratch.mit.edu/galleries/view/8494

Simply Genius


http://i44.tinypic.com/2yjx4ht.jpg                  http://i44.tinypic.com/xljspe.png        http://i40.tinypic.com/16c3rdi.png        http://i39.tinypic.com/16jowol.png

Offline

 

#144 2009-04-15 09:18:05

Masterdan
Scratcher
Registered: 2009-03-28
Posts: 58

Re: Scrolling Background - Help.

How do I get rid of the flashing white line between two terrains?


http://img145.imageshack.us/img145/7089/puzzleadvert.pnghttp://www.neopets.com/refer.phtml?username=dan_84_dan Click there for fun stuff and to get me free stuff!

Offline

 

#145 2009-04-15 16:20:25

ikuehne
Scratcher
Registered: 2009-04-15
Posts: 15

Re: Scrolling Background - Help.

Uhhm... Sorry, but couldn't you just make a background (a sky, for example) and make sprites for pieces of ground, etc. move with the arrow keys instead of what you're supposed to be controlling moving? and isn't that considerably less time-consuming? I'm not an expert on this, but I have some scrolling projects of my own on my personal computer (most of my projects are just ____-offs which I don't put online) which use this technique.

Offline

 

#146 2009-04-15 17:12:29

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Scrolling Background - Help.

That wont work in scratch because sprites cannot move past the stage.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#147 2009-04-15 17:19:57

ikuehne
Scratcher
Registered: 2009-04-15
Posts: 15

Re: Scrolling Background - Help.

I just make them hide, go to the other end of the page, and show again, and it works fine.

Offline

 

#148 2009-04-15 17:27:32

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Scrolling Background - Help.

But your sprites STILL connot move past the stage. With your method, it doesnt matter if you keep moving it to the right for a minute or for an hour because once the sprite is at its limit it cannot go farther. With my methods this limit does not apply.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#149 2009-04-16 04:08:50

ikuehne
Scratcher
Registered: 2009-04-15
Posts: 15

Re: Scrolling Background - Help.

But if you make them hide and show at different times then you can make it so that the background will change at different places. As long as none of the sprites are too long, I don't think that the limit will apply for mine either.

Offline

 

#150 2009-04-16 08:41:24

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Scrolling Background - Help.

Making the sprites hide and show changes nothing.

Is it possible to create this using your method?
http://scratch.mit.edu/projects/archmage/76150


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

Board footer