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

#1 2008-05-28 20:51:09

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

scrollx - how does it work?

i know what the variable scrollx or scroll or whatever you want to call it does, i want to know how it does it. this might have been answered somewhere else already, but could you answer it again then? it would make it easier for me, and probably others, to understand scrolling.


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#2 2008-05-28 21:02:26

CFCRubiks
Scratcher
Registered: 2008-05-11
Posts: 100+

Re: scrollx - how does it work?

Alright, better place to explain how I know this. This is Scrolling:

[blocks]<when green flag clicked>[/blocks]
[blocks]<forever>[/blocks]
[blocks]<set x to( (( <{ ScrollX }>  <+> (( 480 <*> number )) ))[/blocks]

Alright, all I know is that you multiply the ScrollX by the width of the stage (480). Of course the moving is on the stage, and it just changes ScrollX's value

Offline

 

#3 2008-05-28 21:47:27

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

Re: scrollx - how does it work?

With scratch you can easily see how the code does.

But if you don't get it it isn't hard to learn. All it is, is a variable tell sprites where to go on the x axis.

So if you make a code block like this:
set x to (scrollX)

If scrollX is equal to 1000 then the sprite's x position will be set to 1000.

And since the width of the screen for scratch is 480 we will want to move a sprite over by a multiple of 480 so that the sprites appear to be synchronized.

I am going to try to give you a visual example.

The _ represents a distance of 480 horizontally from point 0.
The - also represents a distance of 480 horizontally from point 0
the 1 is sprite one and the 2 is sprite two
The scrollX variable is set to 0

_______________________________________
1-----2--------------------------------------------------

If you count the "-"s, sprite 1 is in place 0 (it starts at zero, a lot of things in programming do) and sprite 2 is in place 6

So making sprite one go to that position is easy right? Just put this script

when green flag clicked
forever
set x to (scrollX)
end

Then sprite one will set it's x position to 0 like we want it to.

But there is a bit of a problem with sprite two. Since we don't want sprite two's x position to be 0 like sprite one's we will have to put in some code that can move it.
Since sprite two is in position 6 it means that its distance horizontally from position 0 is 480 multiplied by its position.

Well its position is 6 so this script should make sprite 2 move to position 6.

when green flag clicked
forever
set x to (scrollX+ (480*6))
end

There, now our sprite's x coordinate should be 480*6 which is equal to 2880.

Now that we have that set up what will happen if the user set the scrollX variable to 480? I will demonstrate with my visual.

_______________________________________
-1-----2--------------------------------------------------

Because the scrollX variable was changed from 0 to 480, our sprites move 480 more units to the right!

By changing the scrollX variable we can make the sprite go to any point along the x axis. If you synchronize your sprites using the 480*sprite position  trick then you can create a large scrolling landscape.

Hope this was helpful. I really can't explain it any better.


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

 

#4 2008-05-28 22:02:39

CFCRubiks
Scratcher
Registered: 2008-05-11
Posts: 100+

Re: scrollx - how does it work?

Wow, I never actually thought of it that way (thought it does make sense. No confusion whatsoever)

Offline

 

#5 2008-05-29 07:35:27

funkymonkey
Scratcher
Registered: 2007-06-03
Posts: 1000+

Re: scrollx - how does it work?

wow. thanks so much. i get it now... lol now i won't have to use those tutorials anymore!


http://i243.photobucket.com/albums/ff67/hprules_photos/banner2.jpg
Kuzimu: Dawn of a New Age                                                                                                  Coming May 2010

Offline

 

#6 2008-06-02 10:14:16

jcv1235
Scratcher
Registered: 2008-03-10
Posts: 86

Re: scrollx - how does it work?

Yea, i used archmages tutorial, and now, i am like a scrolling master --lol


http://i368.photobucket.com/albums/oo122/jcv123_bucket/1.jpg

Offline

 

#7 2008-06-03 14:30:04

dingdong
Scratcher
Registered: 2007-08-09
Posts: 1000+

Re: scrollx - how does it work?

archmage didn't explain this though, scrolling enemies! he described stationary objects also, but this is for one that moves, each enemy will have their own scrollX obviously, so first we start with movement

if scrollX > myscrollX

then change scrollX by ***** (number must be positive(this is for moving left)

if scrollX < myscrollX

then change scrollX by****** (number must be negative(this is for moving right)
___________________________ end of script


this is for jumping:

if Yposition of sprite** < my Yposition

then set Yspeed to 4-6
set jumping to 1
change Y by Yspeed
repeat until touching ground color
change Y by Yspeed
change Yspeed by -0.2

once done with that
set Y speed to 0
set jumping to 0
___________________ end of script

now for sensing:

if not touching ground color and jumping = o

then repeat until touching ground color or jumping = 1
change Y by Yspeed
change Yspeed by -0.2

once done
if jumping = o
then set Yspeed to 0

if color a different color then at the feet, but similar is touching color ground color
then change Y by 5
(this allows you to go above the ground without bouncing)

(other if's can be added for sensors for things such as walls through more color sensors)
___________________ end of script

that's the basic design I use for side scrolling enemies sometimes they'll be more special and will require health variables and other things, but this covers a basic description


http://img851.imageshack.us/img851/2829/superanbanner.png
click the image for my music

Offline

 

#8 2008-08-14 12:11:36

Neoroland
Scratcher
Registered: 2008-05-25
Posts: 10

Re: scrollx - how does it work?

<when green flag clicked>
<if><mouse down?>
<switch to costume[1]>
<say[hoi]
<play sound[arggggg]
<say[im go ing to kill you]
<switch to costume[black]
lol

Offline

 

#9 2008-08-14 12:14:41

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: scrollx - how does it work?

Neoroland wrote:

<when green flag clicked>
<if><mouse down?>
<switch to costume[1]>
<say[hoi]
<play sound[arggggg]
<say[im go ing to kill you]
<switch to costume[black]
lol

what's this got to do with anything?

Offline

 

#10 2008-08-14 23:43:31

terminator355
Scratcher
Registered: 2008-07-31
Posts: 100+

Re: scrollx - how does it work?

actually the variable scrollx doesnt make it scroll all it does is keep track of numbers so the script doesnt have to be gigantor.
   what makes the thing scroll is were you set x to scrollx*480/number or what ever it is.


No new stuff for now guys  wink  join me for some Black Ops on xbox: termhn or for some rock band on PS3: rocket232

Offline

 

#11 2009-07-27 11:31:12

Neoroland
Scratcher
Registered: 2008-05-25
Posts: 10

Re: scrollx - how does it work?

coolstuff wrote:

Neoroland wrote:

<when green flag clicked>
<if><mouse down?>
<switch to costume[1]>
<say[hoi]
<play sound[arggggg]
<say[im go ing to kill you]
<switch to costume[black]
lol

what's this got to do with anything?

uhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh..................................*runs t astalia*

Offline

 

#12 2009-07-27 13:11:23

yambanshee
Scratcher
Registered: 2007-11-06
Posts: 500+

Re: scrollx - how does it work?

Once you understand the concept of how a function works such as scrolling, it all makes seance and you will start learning more of how widely used each and ever block has the potential. I find that a good way to under stand how variable based functions (once again like scrolling) works you have to 'mentaly' give the variable a random number. For example if i had 3 backgrounds that I want scrolling, i could quite simply use something like this:

Code:

Background1:
x postions is equal to ([scrollx]+(0*480))

Background2:
x postions is equal to ([scrollx]+(1*480))

Background3:
x postions is equal to ([scrollx]+(2*480))

Although this will give me a functioning script (i haven't used this code in months, so apology if its incorrect), if i simply copy and paste it, I haven't actually learned anything.

However lets mentally imagine that our variable (scrollx) is any random number, for example 450.
this is what it will look like:

Code:

Background1:
x postions is equal to ([450]+(0*480))
which is:
x postions is equal to ([450]+0)
which is:
x postions is equal to 450

Background2:
x postions is equal to ([450]+(1*480))
which is:
x postions is equal to ([450]+480)
which is:
x postions is equal to 930

Background3:
x postions is equal to ([450]+(2*480))
which is:
x postions is equal to ([450]+960)
which is:
x postions is equal to 1410

so
background 1's x is 450
background 1's x is 930
background 1's x is 1410

you might notice now that those x positions have one key distinction. their all 480 positions apart. Now is makes seance because thats the same distance as the stage's width, which must mean that their exactly aligned and a constant distance without any overlapping.

Now you know more then just what works. Now you know how it works.

Offline

 

#13 2009-07-28 01:44:18

Chrischb
Scratcher
Registered: 2008-07-24
Posts: 1000+

Re: scrollx - how does it work?

This should help: http://scratch.mit.edu/projects/Chrischb/339757


I fall: It's a tragedy. You fall: It's comedy.
Hmph enjoy your fall - I get a lovely spring... without pans of new leaves.

Offline

 

#14 2010-01-03 14:13:22

pntjr
Scratcher
Registered: 2009-12-11
Posts: 22

Re: scrollx - how does it work?

CFCRubiks wrote:

Alright, better place to explain how I know this. This is Scrolling:

[blocks]<when green flag clicked>[/blocks]
[blocks]<forever>[/blocks]
[blocks]<set x to( (( <{ ScrollX }>  <+> (( 480 <*> number )) ))[/blocks]

Alright, all I know is that you multiply the ScrollX by the width of the stage (480). Of course the moving is on the stage, and it just changes ScrollX's value

It doesnt work!

Offline

 

#15 2010-01-04 15:37:52

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

Re: scrollx - how does it work?

Try -480 instead?


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

Offline

 

#16 2010-01-04 15:53:17

juststickman
Scratcher
Registered: 2009-05-31
Posts: 1000+

Re: scrollx - how does it work?

If your object is
<{ scrollX }>+480

then when scrollX is -480, it will be in the center because 480 and -480 cancel out

So if you want your character to move forward, you actually have to move everything else back (unless there is no character, then everything is the right way round)


http://is.gd/iBQi2 Add grob to your sig and help with world dominiation!http://is.gd/iBQ9Q                                                             Hey guys, we're seriously naming our team bob?

Offline

 

#17 2010-03-20 16:33:26

spanishmonkey246
Scratcher
Registered: 2010-03-16
Posts: 2

Re: scrollx - how does it work?

I still don't get it, can someone just show me what to put on my scripts, that's all I'll need.

Offline

 

Board footer