4

I want to animate the jewels (see below the code and the jewel). The animation works, but unfortunately at the end of the animation there is a "white pic" shown. I mean that the animation isn't fluent, the pic disappears for a second (or frame).

the pic:

enter image description here

Json:

{
    "frames": [
        {
            "name": "sprite-00",
            "position": {
                "x": 0,
                "y": 0,
                "w": 32,
                "h": 32
            }
        },
        {
            "name": "sprite-01",
            "position": {
                "x": 32,
                "y": 0,
                "w": 32,
                "h": 32
            }
        },
        {
            "name": "sprite-02",
            "position": {
                "x": 64,
                "y": 0,
                "w": 32,
                "h": 32
            }
        },
        {
            "name": "sprite-03",
            "position": {
                "x": 96,
                "y": 0,
                "w": 32,
                "h": 32
            }
        },
        {
            "name": "sprite-04",
            "position": {
                "x": 128,
                "y": 0,
                "w": 32,
                "h": 32
            }
        },
        {
            "name": "sprite-05",
            "position": {
                "x": 160,
                "y": 0,
                "w": 32,
                "h": 32
            }
        },
        {
            "name": "sprite-06",
            "position": {
                "x": 192,
                "y": 0,
                "w": 32,
                "h": 32
            }
        },
        {
            "name": "sprite-07",
            "position": {
                "x": 224,
                "y": 0,
                "w": 32,
                "h": 32
            }
        },
        {
            "name": "sprite-08",
            "position": {
                "x": 256,
                "y": 0,
                "w": 32,
                "h": 32
            }
        }
    ]
}

Sprite.js:

function Sprite(animation, x, y, speed) {
    this.speed = speed;
    this.x = x;
    this.y = y;
    this.len = animation.length;
    this.animation = animation;
    this.index = 0;

    this.show = function() {
        let index = floor(this.index) % this.len;
        image(this.animation[index], this.x, this.y);
    }

    this.animate = function() {
        this.index = this.index + this.speed;
    }
}

sketch.js:

let spritesheetJewelGreen32;
let spritedataJewelGreen32;
let greenJewel = [];
let greenJ = [];

function preload() {
spritedataJewelGreen32 = loadJSON('green-jewel32.json');
  spritesheetJewelGreen32 = loadImage('assets/crystals/crystal-32-green.png');
}

function setup() {
let frames = spritedataJewelGreen32.frames;
  for (let i = 0; i < frames.length; i++) {
    let pos = frames[i].position;
    let img = spritesheetJewelGreen32.get(pos.x, pos.y, pos.w, pos.h);
    greenJewel.push(img);
  }
  for (let i = 0; i < 5; i++) {
    greenJ[i] = new Sprite(greenJewel, i * 100,100, 1);
  }
}

function draw() {
for (let greenJs of greenJ) {
      greenJs.show();
      greenJs.animate();
    }
}

What can I do?

Looking forward to your answers!

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

The issue is simple. The sprite sheet contains 8 images, with the indices from 0 to 7. But you have 9 entries in the JSON, with indices from 0 to 8. That causes that the last sprite can't be read from the sheet and greenJewel contains an "empty" entry at the end of the array.
Remove the last entry ("name": "sprite-08", ...) from the JSON, to solve the issue.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174