Question about changing sprites

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
emanevitaerc
Prole
Posts: 17
Joined: Sun Mar 13, 2016 7:21 am

Question about changing sprites

Post by emanevitaerc »

Sorry, this question really isn't LOVE specific, but it pertains to game development potentially in LOVE. So, keeping track of graphical state is annoying. It can lead to unintended behavior whose origins are obnoxious to track down, and it couples game logic with graphics. I had the (probably not original) idea to resolve these problems by having a sort of "stateless" test in the update function of each drawn entity, like so:

Code: Select all

if self.isStanding then
    self.sprite = sprite.standing
elseif self.isWalking then
    self.sprite = sprite.walking
    self.sprite:update(dt) --probably an easy way to have this not trigger on the first frame you play, but that's not important for this example
elseif self.isDucking then
    self.sprite = sprite.ducking
end
And so on, and you could have nested scenarios as well.

My question is, is there anything significantly wrong with doing it this way? I feel like it's fairly obvious, but I've never seen anyone use it. It's obviously not as efficient as just setting it one time, coupled with the player's behavior, but does testing a few conditions every frame really affect performance that much, in practice? I'm pretty clueless when it comes to performance, so I'd appreciate any feedback.
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Question about changing sprites

Post by sphyrth »

Nothing really wrong since it works for you. The only significant disadvantage I can say is that you'll have to do a lot of re-writing if ever you plan on adding and/or removing states.
User avatar
emanevitaerc
Prole
Posts: 17
Joined: Sun Mar 13, 2016 7:21 am

Re: Question about changing sprites

Post by emanevitaerc »

Thanks for the reply, I'll keep that in mind :awesome:
alloyed
Citizen
Posts: 80
Joined: Thu May 28, 2015 8:45 pm
Contact:

Re: Question about changing sprites

Post by alloyed »

I actually go one step further, I don't even keep sprites in objects: instead I have a sprite/animation manager that I pull from, and that has its own internal state to do, for example, switching between frames over time. then my draw methods look something like

Code: Select all

function player:draw()
    assert(Animations:get("player", self.spriteState)):draw(...)
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 48 guests