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.

whenclickedforeverifkeyleftarrowpressedthenturn5degreesendifkeyleftarrowpressedthenturn5degreesendend

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

ifkeyuparrowpressedthensetspeedto5endifkeydownarrowpressedthensetspeedto-5end

Method 2 (Hard Way)

Add this script inside the previously made forever loop

ifkeyuparrowpressedthenifspeed<5then"5"isthemaximumspeedonecanscrollatchangespeedby0.1elsesetspeedto5endelseifkeydownarrowpressedthenifspeed>-5then"-5"isthemaximumspeedyoucanscrollatgoingbackwardschangespeedby-0.1elsesetspeedto-5endelseifnotspeed=0thenifspeed>0thenchangespeedby-0.1elsechangespeedby0.1endifabsofspeed<0.1thenNeededbecauseotherwiseerrorsindouble-pointprecisionwillcausethenumbertostayabovezero.setspeedto0endendend

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.

changescrollxbysinofdirection*speedchangescrollybycosofdirection*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.

whenclickedforeversetxtoscrollx+0*480tomakethespritefartheroverchange0to1orevenmoresetytoscrolly+0*360end