Multidirectional Scrolling

Материал из Поле цифровой дидактики

In ordinary scrolling, a sprite can move in only four directions; up (0 degrees), down (180 degrees), left (-90 degrees), and right (90 degrees). However, in multidirectional scrolling you can scroll in any direction intended by the degree. This is often used in birds eye scrolling games and can be very useful at times.

Movement Control

Put this script in the sprite that you want to control the scrolling with (i.e. the race car or player). The below part of the script will control the direction you are pointing in.

when flag clicked
forever
if <key (left arrow v) pressed> then
turn ccw (5) degrees
end
if <key (left arrow v) pressed> then
turn cw (5) degrees
end
end

The next part of the script can be coded in two different ways. One way can be coded very simply if the sprite controlling the scrolling is a person. In this version of the code, if no keys are pressed all sprites will immediately stop scrolling. The second option to scripting this part is a bit more advanced. It can be used so that when no keys are pressed, the sprite will drift to a stop, providing a smooth, car like scrolling experience.

Method 1 (Easy Way)

Add this script inside the previously made forever loop

if <key (up arrow v) pressed> then
set [speed v] to [5]
end
if <key (down arrow v) pressed> then
set [speed v] to [-5]
end

Method 2 (Hard Way)

Add this script inside the previously made forever loop

if <key (up arrow v) pressed> then
if <(speed) < [5]> then//"5" is the maximum speed one can scroll at
change [speed v] by [0.1]
else
set [speed v] to [5]
end
else
if <key (down arrow v) pressed> then
if <(speed) > [-5]> then//"-5" is the maximum speed you can scroll at going backwards
change [speed v] by [-0.1]
else
set [speed v] to [-5]
end
else
if <not <(speed) = [0]>> then
if <(speed) > [0]> then
change [speed v] by [-0.1]
else
change [speed v] by [0.1]
end
if <([abs v] of (speed)) < [0.1]> then //Needed because otherwise errors in double-point precision will cause the number to stay above zero.
set [speed v] to [0]
end
end
end

Scripting the Scrolling Movement

This is the last part of the script. This is the most important part and will enable you to use your multidirectional scrolling.

change [scroll x v] by (([sin v] of (direction)) * (speed))
change [scroll y v] by (([cos v] of (direction)) * (speed))

Scrolling Sprites

Next you will need to make the other sprites scroll. To do this, place the below script in all the sprites acting as the "background" and scrolling.

when flag clicked
forever
set x to ((scroll x) + ((0) * (480)))//to make the sprite farther over change 0 to 1 or even more
set y to ((scroll y) + ((0) * (360)))
end