• Runtimes
  • trying to use Phaser runtime but "this.add.spine is not a function"

Hi

I am trying out the Phaser runtime and is getting the following error when following the sample project from
EsotericSoftware/spine-runtimestree/4.1/spine-ts/spine-phaser/example/typescript

Uncaught TypeError: this.add.spine is not a function

while the following is recognized correctly.

this.load.spineBinary
this.load.spineAtlas

mouse over "this.add.spine" function show me definition in VSCode but give the error mentioned above.

some code:

const NAME = "HeroScene";

export class HeroScene extends Phaser.Scene {
  constructor() {
    super(NAME);
  }

  preload() {
    // console.log("HeroScene.preload() ENTER");

    this.load.spineBinary("data", "assets/spineboy-pro.skel");
    this.load.spineAtlas("atlas", "assets/spineboy-pma.atlas");

    // console.log("HeroScene.preload() EXIT");
  }

  create() {
    console.log(NAME + ".create() ENTER");

    const spineHero = this.add.spine(0, 0, "data", "atlas");
    spineHero.animationState.setAnimation(0, "walk", true);

    console.log(NAME + ".create() Exit");
  }
}
import * as spine from "@esotericsoftware/spine-phaser";
import { useEffect, useRef } from "react";
import { HeroScene } from "./HeroScene";

interface Props {
  flip?: boolean;

  width?: number;
  height?: number;
  size?: number; // if size is larger than 0 , it will override width and height
}

const SpineScene: React.FC<Props> = ({
  flip = false,
  width = 256,
  height = 256,
}) => {
  const gameRef = useRef<HTMLDivElement>(null);

  flip = !flip;

  useEffect(() => {
    const config: Phaser.Types.Core.GameConfig = {
      type: Phaser.AUTO,
      scale: {
        mode: Phaser.Scale.NONE,
        parent: gameRef.current,
        width: width,
        height: height,
      },
      transparent: true,
      scene: [HeroScene],
      plugins: {
        scene: [
          {
            key: "spine.SpinePlugin",
            plugin: spine.SpinePlugin,
            mapping: "spine",
          },
        ],
      },
    };

    const game = new Phaser.Game(config);
    return () => {
      game.destroy(true);
    };
  }, [height, width]);

  return (
    <div
      ref={gameRef}
      style={{
        width: width,
        height: height,
        transform: flip ? "scaleX(-1)" : undefined,
      }}
    ></div>
  );
};

export default SpineScene;

Sample project to reproduce the issue:

Thanks

Related Discussions
...

Based on the information provided and the spine-phaser runtime documentation, it seems like your setup and usage of the spine-phaser plugin within a Phaser project are correct. The error Uncaught TypeError: this.add.spine is not a function typically indicates that the Spine plugin has not been properly registered or recognized by Phaser at the time this.add.spine is called. This can happen if the plugin is not correctly added to the Phaser game configuration or if there's a version mismatch that causes compatibility issues.

Given that this.load.spineBinary and this.load.spineAtlas are recognized correctly, it suggests that the plugin is partially working but might not be fully integrated or there's an issue with how Phaser recognizes the plugin's extension of the GameObjectFactory (Scene.add).

Ensure that the version of the spine-phaser runtime matches the version of Phaser you are using, as major updates in either could introduce breaking changes or compatibility issues. Also, double-check the plugin registration in your game configuration to ensure it matches the expected format and that there are no typos or errors.

If the issue persists, it might be related to the specific versions of Phaser and spine-phaser you are using, or a more subtle configuration issue that isn't immediately apparent from the provided code and documentation excerpts.

Ok. I changed to version from 3.80 to 3.60 as the exact same version of the sample and now it loads.
However, how do I check which version is supported beside looking at the the sample project?
The Readme.md is not mentioning anything. ( EsotericSoftware/spine-runtimestree/4.1/spine-ts/spine-phaser )

Apologies, I will update the README.md to point the the official documentation which you can find here. That also does not mention the Phaser version it is compatible with, so I'll update this as well.

That said, spine-phaser should be compatible with Phaser 3.80, as I don't see any changes in their changelog regarding the object factories. I have opened an issue

7 أيام لاحقا

Finally found time to investigate. I can not reproduce the issue using Phaser 3.60, 3.70, nor 3.80. In all cases, our example projects work as intended.

E.g. here is our basic spine-phaser example running with Phaser 3.80 and our latest spine-phaser 4.1.x runtime:

If you can provide a self-contained small example project with instructions on how to compile and run it, I can take a look.

Because this is a TypeError I think it's typescript configuration issues. Your project's tsconfig.json should not have "strictPropertyInitialization" or "strict" set to true, as Phaser 3.x is not type-safe in that way. VSCode can default to having both active, see if disabling them helps. I think the function exists, TS is just confused because it is injected at runtime.

شهر واحد لاحقا

I have the exact same issue. This seems to only happen on my M1 Mac. I've done a clean git clone, npm install for both my mac & windows device. when i do console.log(this.add.spine) it appears as undefined in my mac, but not on my windows device.