ソースを参照

🐛 Make SpriteAnimation's fps be settable from the constructor

Pedro Schneider 3 年 前
コミット
223d23bf9f
2 ファイル変更5 行追加6 行削除
  1. 4 4
      pandora/resources/SpriteFrames.js
  2. 1 2
      src/sketch.js

+ 4 - 4
pandora/resources/SpriteFrames.js

@@ -1,6 +1,6 @@
 class SpriteAnimation
 {
-    constructor(name = "default", p5Image, rows, cols, indices)
+    constructor(name = "default", p5Image, rows, cols, indices, fps)
     {
         this.name = name;
         this.fullP5Image = p5Image;
@@ -8,7 +8,7 @@ class SpriteAnimation
         this.columns = cols;
         this.indices = indices;
         this.numFrames = this.indices.length;
-        this.frameTime = 1 / 24;
+        this.frameTime = 1 / fps;
 
         this.frames = [];
         for (let i = 0; i < this.numFrames; i++)
@@ -94,9 +94,9 @@ class SpriteFrames
         return null;
     }
 
-    addAnimation(name, p5Image, rows, cols, indices)
+    addAnimation(name, p5Image, rows, cols, indices, fps = 24)
     {
-        this.animations.push(new SpriteAnimation(name, p5Image, rows, cols, indices));
+        this.animations.push(new SpriteAnimation(name, p5Image, rows, cols, indices, fps));
         this.numAnimations++;
     }
 }

+ 1 - 2
src/sketch.js

@@ -6,7 +6,6 @@ function preload()
     AssetHandler.loadTexture("monke", "/assets/textures/monke.png");
     AssetHandler.loadFont("Lato", "/assets/fonts/Lato-Regular.ttf");
     AssetHandler.loadAudio("bonk", "/assets/audio/thonk.wav");
-    gimoi = loadImage("/assets/textures/gimoi.png");
 }
 
 function setup()
@@ -18,7 +17,7 @@ function setup()
 
     monke = AssetHandler.getP5ImageByName("monke");
     let sf = new SpriteFrames();
-    sf.addAnimation("monke", monke, 4, 4, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
+    sf.addAnimation("monke", monke, 4, 4, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 1);
     test2 = new AnimatedSprite2D("myAnimSprite", null, sf);
     test2.play();
     test2.setCurrentFPS(1);