melonJS
    Preparing search index...

    Class Stage

    a default "Stage" object. every "stage" object (title screen, credits, ingame, etc...) to be managed through the state manager must inherit from this base class.

    state

    Index
    • Parameters

      • Optionalsettings: Partial<StageSettings>

        The stage parameters

        • cameras

          a list of cameras (experimental)

        • onResetEvent

          called by the state manager when reseting the object

        • onDestroyEvent

          called by the state manager before switching to another state

      Returns Stage

    ambientLight: Color

    an ambient light that will be added to the stage rendering. Its alpha is what gates the lighting pass: at alpha 0 (the default) drawLighting returns early and the lights still draw as additive glows.

    rgba(0,0,0,0)
    
    class PlayScene extends Stage {
    onResetEvent() {
    // near-black night. The alpha is what turns the pass ON:
    // leave it at 0 and lights still glow, but nothing darkens
    this.ambientLight.parseCSS("#0a0a1ee0");
    this.addChild(new Light2d(x, y, 200, 200, "#ffcc88", 1));
    }
    }
    ambientLightingColor: Color

    Base light level applied to every normal-mapped sprite in the lit rendering path. Unlike Stage#ambientLight (which is the dark overlay punched by each light's cutout), this color is added to every lit pixel so unlit areas don't render pure black. Defaults to black (0, 0, 0) — sprites without a normalMap ignore it entirely.

    "#000000"
    
    class PlayScene extends Stage {
    onResetEvent() {
    // without this the hemisphere facing away from every light
    // renders pure black, which reads as a hole in the sprite
    this.ambientLightingColor.setColor(60, 60, 70);
    }
    }
    cameras: Map<string, Camera2d>

    The list of active cameras in this stage. Cameras will be rendered based on this order defined in this list. Only the "default" camera will be resized when the window or canvas is resized.

    lights: Map<string, Light2d>

    The list of active lights in this stage.

    Since 19.3.0, Light2d is a first-class world Renderable — the recommended pattern is to add lights directly to app.world (or any container, including a sprite, so the light follows it via parent transforms). The lights Map remains for backward compatibility: any entry added via this.lights.set(name, light) in onResetEvent() is automatically adopted into the world tree at stage reset time so it renders normally.

    • Light2d
    • Stage.ambientLight
    // recommended:
    const whiteLight = new Light2d(100, 100, 140, 140, "#fff", 0.7);
    app.world.addChild(whiteLight);

    // legacy (still works, auto-adopted into world):
    this.lights.set("whiteLight", whiteLight);

    this.ambientLight.parseCSS("#1117");
    settings: StageSettings

    The given constructor options

    • draw the current stage

      Lights are rendered as part of the world tree (they're now first-class Renderables) and the ambient overlay pass runs inside each Camera's post-effect FBO bracket via Stage#drawLighting.

      Override it to draw under or over the world, calling super.draw(...) where the world itself belongs in that order.

      Parameters

      • renderer: Renderer

        the renderer object to draw with

      • world: World

        the world object to draw

      Returns void

    • Draw the stage's ambient-light overlay with cutouts for each active light. Called from each Camera2d inside its post-effect FBO bracket — lights themselves render via the world tree (they're standard Renderables); this pass only paints the dark fill that the lights cut holes through.

      Subclasses can override this method to implement custom lighting (e.g. per-pixel normal-mapped lighting via a custom shader). Called once per camera per frame.

      Parameters

      • renderer: Renderer

        the active renderer

      • camera: Camera2d

        the camera currently rendering this stage

      • translateX: number = ...

        the same world-to-screen X translate that Camera2d.draw() applies to the world container (i.e. camera.pos.x + camera.offset.x for the default camera, plus the container's own offset for non-default cameras)

      • translateY: number = ...

        the world-to-screen Y translate (see translateX)

      Returns void

    • onResetEvent function
      called by the state manager when resetting the object this is typically where you will load a level, add renderables, etc...

      Parameters

      • app: Application

        the current application instance

      • ...args: unknown[]

        optional arguments passed when switching state

      Returns void

      state#change

    • Update the stage. Override it to run your own per-frame logic, and call super.update(dt) so the world and cameras still advance.

      Parameters

      • dt: number

        time since the last update in milliseconds.

      Returns boolean

      true if the stage needs to be redrawn