Skip to content
← Back to Community
Changing the sprite of an object in kaboom.js
Profile icon
SixBeeps

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";
Voters
Profile icon
LiamOakey
Profile icon
Quatro11
Profile icon
SixBeeps
Comments
hotnewtop
Profile icon
MrPixelGarfi

Like JamesHolmstrom wrote, for this one would use changeSprite.

mark.changeSprite('spr_2')

But thought I'd just like to add that if its to change the image of a tilemap, then you can set the frame no. directly on the GameObj:

mark.frame = 4
Profile icon
JamesHolmstrom

have you tried:
nark.changeSprite('spr_2')

Profile icon
SixBeeps

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";
Profile icon
Bookie0

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

Profile icon
SixBeeps

@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.

Profile icon
Bookie0

@SixBeeps
Ahh okay then, np.

Hope you find it

Profile icon
TheBest156
Profile icon
SixBeeps

@TheBest156
That is for Unity, a completely separate game engine.

Profile icon
TheBest156
Profile icon
SixBeeps

@TheBest156
Still for Unity...

Profile icon
TheBest156

@SixBeeps
sorry then, i don't know JS as well as css and html.