Ask coding questions

← Back to all posts
Changing the sprite of an object in kaboom.js
SixBeeps (5137)

I have an object

var nark = add([
  sprite("spr_1")
]);

How might I change the sprite to something different programatically? Ive tried both of these, but to no avail:

nark.sprite = sprite("spr_2");
nark.sprite = "spr_2";
Comments
hotnewtop
SixBeeps (5137)

Ok, I figured it out by doing some digging. The property that holds the sprite is spriteID, not sprite. So, the following code should work:

nark.spriteID = "spr_2";
Bookie0 (6155)

Not sure, but perhaps load the sprite 2, then you can change it using the methods you've used above?

Also, according to H's tutorial here:

Note that this tutorial was made while Kaboom.js was in beta so don't be surprised if the code doesn't work for you.

And you probably have already, but maybe try the documentation here? I tried checking if there was anything about changing sprites, but unfortunately didn't find anything.

Good luck! :D

SixBeeps (5137)

@Bookie0 Kaboom auto-loads sprites, so the above methods still don't work. Documentation doesn't say anything, that's why I'm here :)

Thanks though.

Bookie0 (6155)

@SixBeeps Ahh okay then, np.

Hope you find it