ソースを参照

[phaser] Expose getters for preloaded skeleton data and atlases.

Mario Zechner 2 年 前
コミット
5662c75db9

+ 19 - 20
spine-ts/spine-phaser/example/basic-example.html

@@ -14,15 +14,30 @@
     <h1>Basic example</h1>
 </body>
 <script>
+    class BasicScene extends Phaser.Scene {
+        preload() {
+            this.load.spineBinary("spineboy-data", "assets/spineboy-pro.skel");
+            this.load.spineAtlas("spineboy-atlas", "assets/spineboy-pma.atlas");
+        }
+
+        create() {
+            const spineboy = this.add.spine(400, 500, 'spineboy-data', "spineboy-atlas");
+            spineboy.scale = 0.5;
+            spineboy.animationState.setAnimation(0, "walk", true);
+
+            const spineboy2 = this.make.spine({
+                x: 200, y: 500, dataKey: "spineboy-data", atlasKey: "spineboy-atlas"
+            });
+            this.add.existing(spineboy2);
+        }
+    }
+
     const config = {
         type: Phaser.AUTO,
         width: 800,
         height: 600,
         type: Phaser.WEBGL,
-        scene: {
-            preload: preload,
-            create: create,
-        },
+        scene: [BasicScene],
         plugins: {
             scene: [
                 { key: "spine.SpinePlugin", plugin: spine.SpinePlugin, mapping: "spine" }
@@ -31,22 +46,6 @@
     };
 
     const game = new Phaser.Game(config);
-
-    function preload() {
-        this.load.spineBinary("spineboy-data", "assets/spineboy-pro.skel");
-        this.load.spineAtlas("spineboy-atlas", "assets/spineboy-pma.atlas");
-    }
-
-    function create() {
-        const spineboy = this.add.spine(400, 500, 'spineboy-data', "spineboy-atlas");
-        spineboy.scale = 0.5;
-        spineboy.animationState.setAnimation(0, "walk", true);
-
-        const spineboy2 = this.make.spine({
-            x: 200, y: 500, dataKey: "spineboy-data", atlasKey: "spineboy-atlas"
-        });
-        this.add(spineboy2);
-    }
 </script>
 
 </html>

+ 5 - 5
spine-ts/spine-phaser/src/SpinePlugin.ts

@@ -112,7 +112,7 @@ export class SpinePlugin extends Phaser.Plugins.ScenePlugin {
 			return gameObject;
 		};
 
-		let makeSpineGameObject = function (this: Phaser.GameObjects.GameObjectFactory, config: SpineGameObjectConfig, addToScene: boolean) {
+		let makeSpineGameObject = function (this: Phaser.GameObjects.GameObjectFactory, config: SpineGameObjectConfig, addToScene: boolean = false) {
 			let x = config.x ? config.x : 0;
 			let y = config.y ? config.y : 0;
 			let boundsProvider = config.boundsProvider ? config.boundsProvider : undefined;
@@ -184,10 +184,10 @@ export class SpinePlugin extends Phaser.Plugins.ScenePlugin {
 	}
 
 	createSkeleton (dataKey: string, atlasKey: string) {		
-		return new Skeleton(this.createSkeletonData(dataKey, atlasKey));
+		return new Skeleton(this.getSkeletonData(dataKey, atlasKey));
 	}
 
-	createAtlas(atlasKey: string) {
+	getAtlas(atlasKey: string) {
 		let atlas: TextureAtlas;
 		if (this.atlasCache.exists(atlasKey)) {
 			atlas = this.atlasCache.get(atlasKey);
@@ -210,8 +210,8 @@ export class SpinePlugin extends Phaser.Plugins.ScenePlugin {
 		return atlas;
 	}
 
-	createSkeletonData(dataKey: string, atlasKey: string) {
-		const atlas = this.createAtlas(atlasKey)
+	getSkeletonData(dataKey: string, atlasKey: string) {
+		const atlas = this.getAtlas(atlasKey)
 		const combinedKey = dataKey + atlasKey;
 		let skeletonData: SkeletonData;		
 		if (this.skeletonDataCache.exists(combinedKey)) {

+ 6 - 0
spine-ts/spine-phaser/src/index.ts

@@ -28,4 +28,10 @@ declare global {
 			spine (config: SpineGameObjectConfig, addToScene?: boolean): SpineGameObject;
 		}
 	}
+
+	namespace Phaser {
+		export interface Scene {
+			spine: SpinePlugin;
+		}
+	}
 }