Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Pixelbox

Create 2D games in JavaScript, made easier · By Cedric Stoquer

I cant render enemy

A topic by Nooh Alavi created Apr 15, 2020 Views: 211 Replies: 2
Viewing posts 1 to 2
(1 edit)

Not sure if this is the right place for this, but since there is no help topic....

Making a simple game to learn Pixelbox.js, and I cant seem to render my enemy. Ive been trying for hours. Here is code to spawn enemy:

function spawnEnemy() {
    var enemy = {
        key: 152,
        x: random(128),
        y: random(128),
        width: 8,
        height: 8,
        followSpeed: 4,
        update: function () {
            this.x += (player.x - this.x) / (player.x - this.x) * this.followSpeed;
            this.y += (player.y - this.y) / (player.y - this.y) * this.followSpeed;
        }
    }
    enemies.push(enemy);
}

"enemies" is an empty array. Then, in the update function, I call, 

for (var i = 0; i < enemies.length; i++) {
        var e = enemies[i];
        sprite(e.frame, e.x, e.y);
    }

For some reason, it doesnt show anything. Any idea why?

Edit: If it will help, here is my entire render function, which I call in update:

function render() {
    cls();
    draw(background, 0, 0);
    sprite(Math.round(player.frame), player.x, player.y, player.isFacingLeft);
    sprite(sword.frame, sword.x, sword.y);
    for (var i = 0; i < enemies.length; i++) {
        var e = enemies[i];
        sprite(e.frame, e.x, e.y);
    }
    draw(foreground, 0, 0);
}

Just looking at the snippets, I can't see you creating e.frame anywhere. Enemy has a key variable, which looks like it might be a sprite index. Hopefully it is just this typo :)

If that's not the case, I would check if your enemies array is > 0 during the game, maybe your spawner code does not fire up correctly?

(+1)

Omg, I think you're right! Ill try that thanks!