Using Clones for Particle Effects

Материал из Поле цифровой дидактики
Описание Как создать эффект частиц с помощью клонов?
Область знаний Химия, Биология, Информатика
Область использования (ISTE) Computational Thinker
Возрастная категория 10


Поясняющее видео
Близкие рецепту понятия Flocking, Клон
Среды и средства для приготовления рецепта: Snap!, Scratch

Клоны появились в Scratch с версии 2. They allowed you to create complete duplicates of sprites, which can then individually run tasks.

Using clones properly is important — you should make sure your scripts are concise, fast, and readable. This tutorial will explain how to use clones to create a utility for particle effects. This will begin with using clones to create a 'spark' effect, then optimize the code, and finally create an easy-to-use package for a user so that they can incorporate it into a project easily. The goal is to create a single block called "Create a spark at x: () y: ()".

Instructions

Creating the Effect

First, you need to create the parent sprite. This sprite will generate all the particles of the spark effect not that it itself will never be one of the particles. The general plan of the code is:

  • To generate a spark effect, go to the required position
  • Create 10 clones, each of which is a particle of the spark
  • When the particles are created, they fly out and fall, together giving the effect of a spark

First, create your sprite. Name it "Spark" and make its costume a single orange dot. Шаблон:Note

To begin, create two local variables and call then velocity x and velocity y. Make sure that they're "for this sprite only". Шаблон:Note

Now you will need to write the common velocity-arc script under a "when I start as a clone" block:

When [space v] key pressed
repeat (10)
  create clone of [myself v]
end

When I start as a clone
set [velocity x v] to ((pick random (-5) to (5)) / (3))
set [velocity y v] to (pick random (1) to (10))
repeat (20)
  change x by (velocity x)
  change y by (velocity y)
  change [velocity y v] by (-1)
end
delete this clone

At this point, you can run the script! Just press space to see some sparks.

How come the velocity variables do not get muddled with each other and get reset by each clone? When we clicked "for this sprite only", we made it a lexically scoped variable which is duplicated along with the sprite. That is, each clone had its own velocity x and velocity y variables independent of each other.

One of the issues with this script is that if you press space multiple times, each spark duplicates itself. This is clearly wrong behavior; it happens because when space is pressed, each clone starts creating copies. So we need a new variable which tells us whether we are a clone or not:

when gf clicked
set [is clone? v] to [no]

when [space v] key pressed
if <(is clone?) = [no]> then
  repeat (10)
    create clone of [myself v]
  end
end

when I start as a clone
set [is clone? v] to [yes]
set [velocity x v] to ((pick random (-5) to (5)) / (3))
set [velocity y v] to (pick random (1) to (10))
repeat (20)
  change x by (velocity x)
  change y by (velocity y)
  change [velocity y v] by (-1)
end
delete this clone

The is clone? variable tells the sprite whether or not to duplicate itself. Make sure this is local, too. You can also fix the issue by instead of using when space key pressed you could use

when gf clicked
forever
 if <key [space v] pressed> then
  repeat (10)
   create clone of [myself v]
  end
 end
end

Optimizations and Improvements

In this section, we will add the following optimizations to the spark engine:

  • Sparks will be created gradually, instead of suddenly
  • Sparks will fade out and "die" automatically, instead of waiting to touch the edge

First, we hide the parent spark, because it should not be visible. We also make the spark creation routine a custom block. Then we add a small delay when creating a clone, which changes over time to give a more realistic effect:


when gf clicked
set [is clone? v] to [no]
hide

when I start as a clone
show
set [is clone? v] to [yes]
set [velocity x v] to ((pick random (-5) to (5)) / (3))
set [velocity y v] to (pick random (1) to (10))
repeat (20)
  change x by (velocity x)
  change y by (velocity y)
  change [velocity y v] by (-1)
end
delete this clone

define Create effect at (x) (y)
go to x: (x) y: (y)
set [creation_delay v] to [-0.05]
if <(is clone?) = [no]> then
  repeat (10)
    change [creation_delay v] by (0.001)
    wait ((creation_delay) * (creation_delay)) secs
    create clone of [myself v]
  end
end

when [space v] key pressed
Create effect at (1) (1)

To make sparks fade out, we simply change the ghost effect:

when gf clicked
set [is clone? v] to [no]
hide

when I start as a clone
show
set [is clone? v] to [yes]
set [velocity x v] to ((pick random (-5) to (5)) / (3))
set [velocity y v] to (pick random (1) to (10))
repeat (20)
  change [ghost v] effect by (5)
  change x by (velocity x)
  change y by (velocity y)
  change [velocity y v] by (-1)
end
delete this clone

define Create effect at (x) (y)
go to x: (x) y: (y)
set [creation_delay v] to [-0.03]
if <(is clone?) = [no]> then
  repeat (10)
    change [creation_delay v] by (0.001)
    wait ((creation_delay) * (creation_delay)) secs
    create clone of [myself v]
    create clone of [myself v]
    create clone of [myself v]
    create clone of [myself v]
  end
end

when [space v] key pressed
Create effect at (1) (1)

We also create more sparks for a richer effect, and tweak the creation delay mildly.

Distributing the Code

To distribute the sparks library, we need to create a custom block which can be defined on any sprite. When called by the user, it should send a broadcast to the spark sprite to generate a spark effect.

We do this by creating a global list called "(spark) preferences" which contains a list of information about the spark library. It has, in order:

  1. X position to spawn
  2. Y position to spawn
  3. Color to spawn

Adding these touches, we have:

when [space v] key pressed
create effect at (1)(1)

define create effect at (x)(y)
go to x: (x) y: (y)
set [creation_delay v] to [-0.002]
if <(is clone?)=[no]> then
 repeat (3)
  change [creation_delay v] by (0.005)
  wait ((creation_delay)*(creation_delay)) secs
  create clone of [myself v]
  create clone of [myself v]
  create clone of [myself v]
  create clone of [myself v]
 end
end

when I start as a clone
show
set [is clone? v] to [yes]
set [velocity x v] to ((pick random (-5) to (5))/(3))
set [velocity y v] to (pick random (1) to (10))
repeat (20)
 change [ghost v] effect by (5)
 change x by (velocity x)
 change y by (velocity y)
 change [velocity y v] by (-1)
end
delete this clone

when gf clicked
set [is clone? v] to [no]
hide

when I receive [(spark) spawn v]
set [color v] effect to (item (3 v) of [(spark) preferences v])
create effect at (item (1 v) of [(spark) preferences v])(item (2 v) of [(spark) preferences v])

We also create the global block, and a small demo script:

when [space v] key pressed
Spawn Spark Effect at x: (mouse x) y: (mouse y) color: (pick random (1) to (200))

define Spawn Spark Effect at x: (x) y: (y) color: (color)
replace item (1 v) of [(spark) preferences v] with (x)
replace item (2 v) of [(spark) preferences v] with (y)
replace item (3 v) of [(spark) preferences v] with (color)
broadcast [(spark) spawn v]

An issue here is that when you create a spark while an old one is running, the old spark effect stops. This is because of the broadcast. To avoid this, we use a trigger variable instead of a broadcast. We add a new item to our preferences list. Whenever a new spark is created, we simply set this to "new", and our library resets it back to blank once the effect has been created:

when gf clicked
set [is clone? v] to [no]
hide

when I start as a clone
show
set [is clone? v] to [yes]
set [velocity x v] to ((pick random (-5) to (5)) / (3))
set [velocity y v] to (pick random (1) to (10))
repeat (20)
  change [ghost v] effect by (5)
  change x by (velocity x)
  change y by (velocity y)
  change [velocity y v] by (-1)
end
delete this clone

define Create effect at (x) (y)
go to x: (x) y: (y)
set [creation_delay v] to [-0.002]
if <(is clone?) = [no]> then
  repeat (3)
    change [creation_delay v] by (0.005)
    wait ((creation_delay) * (creation_delay)) secs
    create clone of [myself v]
    create clone of [myself v]
    create clone of [myself v]
    create clone of [myself v]
  end
end

when gf clicked
forever
  if <[new] = (item (4 v) of [(sparks) preferences v])> then
    replace item (4 v) of [(sparks) preferences v] with []
    set [color v] effect to (item (3 v) of [(sparks) preferences v])
    Create effect at (item (1 v) of [(sparks) preferences v])(item (2 v) of [(sparks) preferences v])

when [space v] key pressed
Spawn Spark Effect at x: (mouse x) y: (mouse y) color: (pick random (1) to (200))

define Spawn Spark Effect at x: (x) y: (y) color: (color)
replace item (1 v) of [(spark) preferences v] with (x)
replace item (2 v) of [(spark) preferences v] with (y)
replace item (3 v) of [(spark) preferences v] with (color)
replace item (4 v) of [(spark) preferences v] with [new]

An example project can be found here.

See Also