Scratch programming q and a
1) How to make the background move in scratch?
2) How do you ensure that none of the scratch characters go through the wall?
3) How do you give the gravity to the scratch characters?
Note - No gooogle answers, no spaam
Answers
Answer:
scroll
x
: "for all sprites", this tracks the virtual x position.
my
x
: "for this sprite" on the Object sprite, this controls the object's virtual x position.
Programming
Setup
In the Hitbox sprite, add the following script:
when
clicked
set
ghost
effect
to
99
not visible to the user, but still not technically hidden
forever
go
to
Player
This will make the hitbox hidden, but other sprites can still check if they are touching it. The hitbox will also move so that it covers the Player correctly.
In the Player sprite, add the following script:
when
clicked
set
scroll x
to
0
reset virtual x to 0
go
to
x:
0
y:
0
the sprite itself should never move
set
rotation
style
left-right
it should point left or right only
This will simply initialize the positions.
In the Background sprite, add the following script:
when
clicked
go
to
x:
0
y:
0
go
to
back
layer
the background should always be at the back
This will move the background to the center and move it behind all other sprites.
In the Object sprite, add the following script:
when
clicked
set
y
to
0
this can be a different value if necessary
set
my x
to
100
this can be customized
Basic Movement
Expand the script in the Player sprite as follows:
when
clicked
set
scroll x
to
0
go
to
x:
0
y:
0
set
rotation
style
left-right
forever
if
key
right arrow
pressed?
then
change
scroll x
by
5
point
in
direction
90
if
key
left arrow
pressed?
then
change
scroll x
by
-5
point
in
direction
-90
This will change the virtual x so that the backgrounds will move.
Background Movement
Expand the script in the Background sprite as follows:
when
clicked
go
to
x:
0
y:
0
go
to
back
layer
create
clone
of
myself
forever
set
x
to
-
scroll
x
mod
480
This will make the background move so that it appears as if the player was moving.
Add the following new script as well:
when
I
start
as
a
clone
go
to
back
layer
forever
set
x
to
480
-
scroll
x
mod
480
This will add a copy of the background shifted one Stage width to the right, so that the illusion is not broken by the first copy letting the white Stage through.