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

#1 2012-06-10 19:30:41

powerpoint56
Scratcher
Registered: 2012-04-19
Posts: 500+

ScrollY and ScrollX in any Direction

So, I'm doing this car racing game where your car can go in any direction and it changes scroll-x/scroll-y appropriately. I found a script I could use to make it scroll in any direction:

when gf clicked
forever
change [scroll-x v] by (([sin v] of (direction)) * (x-velocity))
change [scroll-y v] by (([cos v] of (direction)) * (y-velocity))
end
But now I'm totally dumbfounded. What do I set the velocities to? What do I do to make the screen move when the arrow keys? I know how to do regular scrolling with x and y, but not anything of the sort above.
Thanks!  smile

Last edited by powerpoint56 (2012-06-11 16:47:20)


http://i48.tinypic.com/2072ctw.gif

Offline

 

#2 2012-06-10 19:49:33

chanmanpartyman
Scratcher
Registered: 2011-05-30
Posts: 500+

Re: ScrollY and ScrollX in any Direction

Just make it one variable, velocity. You're probably looking for:

when gf clicked
forever
change [scroll x v] by (([sin v] of (direction)) * (velocity))
change [scroll y v] by (([cos v] of (direction)) * (velocity))
if <key [up arrow v] pressed?>
 change [velocity v] by (1)
end
set [velocity v] to ((velocity) * (0.87))
EDIT: 501st post!

Last edited by chanmanpartyman (2012-06-10 19:50:28)

Offline

 

#3 2012-06-11 04:29:53

Wes64
Scratcher
Registered: 2011-08-19
Posts: 1000+

Re: ScrollY and ScrollX in any Direction

Do you understand trig? If you don't ill be glad to summarize what this script does and why it works.


Experienced 2.0 Tester: Ask me questions!
Using Firefox 13.0, Flash plugin version 11.4.402.287, and Windows XP Professional.

Offline

 

#4 2012-06-11 16:45:31

powerpoint56
Scratcher
Registered: 2012-04-19
Posts: 500+

Re: ScrollY and ScrollX in any Direction

chanmanpartyman wrote:

Just make it one variable, velocity. You're probably looking for:

when gf clicked
forever
change [scroll x v] by (([sin v] of (direction)) * (velocity))
change [scroll y v] by (([cos v] of (direction)) * (velocity))
if <key [up arrow v] pressed?>
 change [velocity v] by (1)
end
set [velocity v] to ((velocity) * (0.87))
EDIT: 501st post!

Thanks!!!


http://i48.tinypic.com/2072ctw.gif

Offline

 

#5 2012-06-11 16:46:37

powerpoint56
Scratcher
Registered: 2012-04-19
Posts: 500+

Re: ScrollY and ScrollX in any Direction

Wes64 wrote:

Do you understand trig? If you don't ill be glad to summarize what this script does and why it works.

Thanks! Can you?


http://i48.tinypic.com/2072ctw.gif

Offline

 

#6 2012-06-11 18:28:12

Wes64
Scratcher
Registered: 2011-08-19
Posts: 1000+

Re: ScrollY and ScrollX in any Direction

powerpoint56 wrote:

Wes64 wrote:

Do you understand trig? If you don't ill be glad to summarize what this script does and why it works.

Thanks! Can you?

So in very rough, basic terms...

Imagine a circle with a radius. No matter what direction the radius is pointed in, it always is the same length, right? Basically, sin(Direction) and cos(Direction) describe a radius of that circle. They give you the slope of that radius.

So in the script, your "radius" length is equal to the velocity variable, and the sin and cos give you the slope of that radius. Because you have the slope, you can scroll in any direction, and the velocity thing makes it smooth.


Experienced 2.0 Tester: Ask me questions!
Using Firefox 13.0, Flash plugin version 11.4.402.287, and Windows XP Professional.

Offline

 

#7 2012-06-16 19:21:25

powerpoint56
Scratcher
Registered: 2012-04-19
Posts: 500+

Re: ScrollY and ScrollX in any Direction

Thank you for explaining!  smile   tongue


http://i48.tinypic.com/2072ctw.gif

Offline

 

#8 2012-06-18 17:36:45

captaincrunch
New Scratcher
Registered: 2012-06-17
Posts: 12

Re: ScrollY and ScrollX in any Direction

is it possable to make x volocity in a scrolling game?

Offline

 

#9 2012-06-20 19:48:43

powerpoint56
Scratcher
Registered: 2012-04-19
Posts: 500+

Re: ScrollY and ScrollX in any Direction

captaincrunch wrote:

is it possable to make x volocity in a scrolling game?

Yes. Here's what I'd do:

when gf clicked
forever
change [scroll-x v] by (x-velocity)
if <key [right-arrow v] pressed?>
change (x-velocity) by (-1) //same with below (if left-arrow pressed)
end
if <key [left-arrow v] pressed?>
change (x-velocity) by (1)
end
set (x-velocity) to (((x-velocity)*(0.95)))
end
Of course, then you would set the other sprites to "scroll x + 480 * ?" like usual.
I think this will work; I'll go check and edit if I need to.


http://i48.tinypic.com/2072ctw.gif

Offline

 

#10 2012-06-20 19:51:31

powerpoint56
Scratcher
Registered: 2012-04-19
Posts: 500+

Re: ScrollY and ScrollX in any Direction

powerpoint56 wrote:

captaincrunch wrote:

is it possable to make x volocity in a scrolling game?

I think this will work; I'll go check and edit if I need to.

Works!


http://i48.tinypic.com/2072ctw.gif

Offline

 

#11 2012-06-21 03:11:29

Wes64
Scratcher
Registered: 2011-08-19
Posts: 1000+

Re: ScrollY and ScrollX in any Direction

powerpoint56 wrote:

captaincrunch wrote:

is it possable to make x volocity in a scrolling game?

Yes. Here's what I'd do:

when gf clicked
forever
change [scroll-x v] by (x-velocity)
if <key [right-arrow v] pressed?>
change [x-velocity v] by [-1] //same with below (if left-arrow pressed)
end
if <key [left-arrow v] pressed?>
change [x-velocity v] by (1)
end
set [x-velocity v] to <(x-velocity) * [0.95]>
Of course, then you would set the other sprites to "scroll x + 480 * ?" like usual.
I think this will work; I'll go check and edit if I need to.

Fixed


Experienced 2.0 Tester: Ask me questions!
Using Firefox 13.0, Flash plugin version 11.4.402.287, and Windows XP Professional.

Offline

 

Board footer