Mouse Scrolling

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

Mouse scrolling is a form of scrolling, but can be simpler due to there being less code required than regular scrolling.

Mouse Scrolling Engine

Setting Up

To set up the project with the ability to scroll using only a mouse pointer, two sprites, and four variables are needed. The four variables required are the following:

  • x
  • y
  • posx
  • posy

After creating these variables, the two sprites need to be created. The map and the character. The map can be made of any size. If the map sprite cannot get very large, please refer to this to learn how to bypass the size limit.

Coding

Coding the Map

For coding the map of the project, the following code is needed:

defineupdatemapsetposxtox-x*2/10setposytoy-y*2/10whengreenflagclickedforeverupdatemapgotox:posxy:posy

Coding the Player

The scripts are as shown:

whenclickedsetxto0setyto0forevermovementdefinedlaterenddefinemovementpointtowardsmouse-pointerchangexbymousex/3changeybymousey/3

Adding Objects To Scrolling Games

Sometimes in a scrolling game, interactive objects are added. The gotox:y: block can be used but the object(s) will just stay right on the screen. Here are a couple different methods on how to put objects in a set location on a scrolling game:

A Singular Object

In order to have a single object stay in place on a scrolling game, a certain code is needed to that the object will not stick to a set location on the screen. First, only 2 local variables are needed:

  • x
  • y

Once you have those required variables, all you need to do now is give the object the code that will make it stay in one location (not on the screen) and will give it the ability to go on and off the screen:

whenclickedforevergotox:x-posxy:y-posyifx-posx=xpositionandy-posy=ypositionthenshowelsehideend...anythingelsecangohereorjuststartanotherlineofcodesomewhereelseend

Cloning The Same Object

The other way to add multiple objects to a project is by cloning them. For this sprite, the x and y variable are needed.

A custom block is the preferred method for cloning the sprite to prevent errors in the coding:

definecloneatxyrunwithoutscreenrefreshsetxtoxsetytoycreatecloneofmyselfwhenclickedcloneatchangethesetwolabelstotheclonecoordinates

Then for the positioning for the clones objects, it is the same thing as the singular object except replace the whenclicked with whenIstartasaclone hat block:

whenIstartasacloneforevergotox:x-posxy:y-posyifx-posx=xpositionandy-posy=ypositionthenshowelsehideend...anythingelsecangohereorjuststartanotherlineofcodesomewhereelseend

Example Projects