Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

can I have a export.update function in another function then use the export.update function elsewhere?

(+1)
(1 edit) (+2)

I think, what you are trying to do is to recreate the concept of Game Objects.

Pixelbox doesn't have Game Object, but this can be implement without much difficulties (you just need to manage the pool of game objects yourself). Here's a possible solution:

var flakes = [];
 
class Flake {
    constructor () {
        this.xPos = random(150);
        this.yPos = 0;
        flakes.push(this);
    }
 
    update() {
        this.yPos += 0.5;
        this.xPos -= 0.15;
        sprite(192, this.xPos, this.yPos);
 
        if (this.yPos > 128) {
            this.destroy()
        }
    }
 
    destroy() {
        flakes.splice(flakes.indexOf(this), 1);
    }
}
 
 
exports.update = function () {
    cls();
 
    for (var i = flakes.length - 1; i >= 0; i--) {
        flakes[i].update();
    }
 
    var snowChance = random(30);
    if (snowChance == 25) {
        new Flake();
    }
};

Do you mind explaining what that last part is doing? why is the update before it creates a new flake?

Thanks so much for what you have helped me with already!