API

Getting Started - Chapter 6 - The Switch On Event

The Switch On Event

When we click on the fountain, we want it to start. We do this by adding a function to an onPointerObservable to handle a pointer-down event that switches the particle system between stop and start.

import { PointerEventTypes } from "@babylonjs/core/Events/pointerEvents";
let switched = false; //on off flag
scene.onPointerObservable.add((pointerInfo) => {
switch (pointerInfo.type) {
case PointerEventTypes.POINTERDOWN:
if (pointerInfo.pickInfo.hit) {
pointerDown(pointerInfo.pickInfo.pickedMesh);
}
break;
}
});
const pointerDown = (mesh) => {
if (mesh === fountain) { //check that the picked mesh is the fountain
switched = !switched; //toggle switch
if(switched) {
particleSystem.start();
}
else {
particleSystem.stop();
}
}
}
Start/Stop Particles on Click

Now we add this to the village world.

Add The Fountain To The Village

So far, all the action has taken place in daylight, and now it is time to move to night, when we will need street lights.