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)
  • (pos x)
  • (pos y)

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:

define update map
set [pos x v] to (((x) - ((x) * [2])) / (10)
set [pos y v] to (((y) - ((y) * [2])) / (10)

when green flag clicked
forever
update map
go to x: (pos x) y: (pos y)

Coding the Player

The scripts are as shown:

when green flag clicked
set [x v] to (0)
set [y v] to (0)
forever
movement//defined later
end

define movement
point towards (mouse-pointer v)
change [x v] by ((mouse x)/ (3)
change [y v] by ((mouse y)/ (3)

Adding Objects To Scrolling Games

Sometimes in a scrolling game, interactive objects are added. The go to x: () 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:

when green flag clicked
forever
go to x:((x) - (pos x)) y:((y) - (pos y))
	if <<((x) - (pos x)) = (x position)> and<((y) - (pos y)) = (y position)>> then
	show
	else
	hide
	end
	...//anything else can go here or just start another line of code somewhere else
end

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:

define clone at (x) (y)//run without screen refresh
set [x v] to (x)
set [y v] to (y)
create clone of (myself v)

when green flag clicked
clone at () ()//change these two labels to the clone coordinates

Then for the positioning for the clones objects, it is the same thing as the singular object except replace the when gf clicked with when I start as a clone hat block:

when I start as a clone
forever
go to x:((x) - (pos x)) y:((y) - (pos y))
	if <<((x) - (pos x)) = (x position)> and<((y) - (pos y)) = (y position)>> then
	show
	else
	hide
	end
	...//anything else can go here or just start another line of code somewhere else
end

Example Projects