How to Go Off the Screen Edge

Материал из Поле цифровой дидактики
Версия от 11:33, 21 июля 2022; Patarakin (обсуждение | вклад) (1 версия импортирована)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)

In the Scratch Player, sprites are prevented from moving off the edge of the screen; if one tries to make a sprite do so by setting its x or y position to a very large positive or negative number, the sprite will be unable to move completely off of the Stage.

However, it is possible to move a sprite completely off the screen by using a trick involving different costumes. There are multiple ways to pull off this trick, one of which is detailed below.

Steps

First of all, make two new costumes. Leave one of them blank, and fill the other one entirely using the fill bucket (so that its width is 480 pixels and its height is 360 pixels). It can be any solid color. If other costumes are needed, create them; they are not subject to any requirements.

Next, create the following block (enable "run without screen refresh"):

define go offscreen to x: (x) y: (y)
set [old costume v] to (costume [number v]) // back up costume
set [old size v] to (size) // back up size
switch costume to (blank costume v)
set size to ((1)/(0))% // or some similarly high number
switch costume to (completely filled costume v)
go to x: (x) y: (y)
set size to (old size)% // restore size
switch costume to (old costume) // restore costume

To use it:

when gf clicked
. . .
go offscreen to x: (wanted x) y: (wanted y) :: custom
. . .

Details

This can be combined with Overriding the Costume Size Limit for use in scrollers.

A quick rundown on how the above definition works:

It first backs up the old costume ID and the old size for later.

It then uses a technique that can set a sprite's size to an arbitrarily large number. Basically, an empty costume's size can be set to any number between 100 and 54,000, and changing a sprite's costume does not change its size. This means that, by switching a sprite's costume to the empty costume, setting the size, and then switching back, a sprite can be set to practically any size that is needed.

Next, it exploits the different x and y position limits of different costumes and the fact that a sprite's costume changing will not change its x or y position. A larger costume can move farther off of the screen than a smaller costume, simply because it is larger; this fact can be magnified by using the set size block as shown previously.

Then it just restores the costume and size.

See also