|
|
View previous topic - View next topic |
Author |
Message |
Captain Vimes Grumble Teddy
Joined: 12 May 2006 Posts: 225 Location: The City Streets
|
Posted: Wed Aug 26, 2009 3:36 pm Post subject: DyingSprite |
[quote] |
|
Well, since DevX is the most experienced person here when it comes to my game engine, I guess this question is mostly geared towards him. But if anyone else has any input, it would be greatly appreciated.
I am currently designing a Sprite subclass for all the short-lived objects in the world; most commonly, explosions resulting from the death of an enemy alien or whatever. I don't want them to hang around forever, so I'm making the DyingSprite class.
The basic thing is that it's a Sprite class that just sits in one spot and plays its animation. But when the Update(float deltaTime) function is called for the DyingSprite, it also adds the deltaTime value to the timer_ variable. When timer_ is greater than or equal to the deathTime_ variable, which is set when the DyingSprite is created, the Sprite removes itself from the game.
Here's the thing, though: I don't know how to make a Sprite remove itself from the game engine's SpriteManager. The only thing I could come up with was this:
- Take the Sprite class and add a pointer to the game engine
- Also add an integer that stores the Sprite's game engine index
-When the DyingSprite dies, have it call engine_->DeleteSprite(index_)
Would this work? Or is there another, easier way to get rid of a Sprite without mangling the class? _________________ "Sometimes it is better to light a flamethrower than to curse the darkness."
- Terry Pratchett
|
|
Back to top |
|
|
Hajo Demon Hunter
Joined: 30 Sep 2003 Posts: 779 Location: Between chair and keyboard.
|
Posted: Wed Aug 26, 2009 3:51 pm Post subject: |
[quote] |
|
I don't know your engine, but maybe I can give an idea.
In many cases an object can't safely delete itself, because it's code would still be executed in the moment of deletion, and this causes all kinds of troubles.
A clean way is to have a second class where a dying object can register to be deleted and which regularly (e.g. after a frame is drawn) deletes all registered objects.
|
|
Back to top |
|
|
Mattias Gustavsson Mage
Joined: 10 Nov 2007 Posts: 457 Location: Royal Leamington Spa, UK
|
Posted: Wed Aug 26, 2009 4:17 pm Post subject: |
[quote] |
|
yeah, I do pretty much what hajo said.
I have a flag on the sprite called IsDead, and I set it to true when I want it gone (in your case, when the time is up). Then, the code which calls update on it, would do this:
Code: |
if (sprite->IsDead())
DeleteSprite(sprite); // Will both delete the object and remove it from any list/array it is stored in
else
sprite->Update(deltaTime);
|
_________________ www.mattiasgustavsson.com - My blog
www.rivtind.com - My Fantasy world and isometric RPG engine
www.pixieuniversity.com - Software 2D Game Engine
|
|
Back to top |
|
|
DeveloperX 202192397
Joined: 04 May 2003 Posts: 1626 Location: Decatur, IL, USA
|
Posted: Wed Aug 26, 2009 5:28 pm Post subject: Re: DyingSprite |
[quote] |
|
Captain Vimes wrote: | Well, since DevX is the most experienced person here when it comes to my game engine, I guess this question is mostly geared towards him.
|
Hahaha, I wonder why that is? ;)
Quote: |
I am currently designing a Sprite subclass for all the short-lived objects in the world; most commonly, explosions resulting from the death of an enemy alien or whatever. I don't want them to hang around forever, so I'm making the DyingSprite class.
|
I wouldn't make another sprite class for that.
I would use a timed-action class, and use that in the class that needs it.
something like
Code: |
class TimedAction{};
class Explosion : public TimedAction // explosion is-a TimedAction
{
// other methods omitted
private:
Sprite* sprite_; // explosion has-a Sprite not is-a Sprite
// other members omitted
}
|
I don't have time right now to implement the TimedAction class, but maybe later.
The idea is that you have a counter that gets updated every delay frames if frame-based or milliseconds if you're using time-based anims, and when the counter reaches the actiontime then whatever action you want to trigger gets triggered.
So for the Explosion, the do_action() method would simply make the sprite visible, start the explosion animation, and tell the Explosion::update() method to run through the explosion animation frames, and once they are out, to make the sprite hidden, and set a dead boolean flag.
Your SpriteManager should then clean up any dead Sprites.
Quote: |
Here's the thing, though: I don't know how to make a Sprite remove itself from the game engine's SpriteManager. The only thing I could come up with was this:
- Take the Sprite class and add a pointer to the game engine
- Also add an integer that stores the Sprite's game engine index
-When the DyingSprite dies, have it call engine_->DeleteSprite(index_)
Would this work? Or is there another, easier way to get rid of a Sprite without mangling the class? |
You cannot have the Sprite remove itself from the spritemanager.
You should request that the sprite's manager remove the sprite.
I don't have your hammer source on hand right at this moment.
If you post your issue on the nogdus forums, it will remind me to look into your issue later when I finish working. _________________ Principal Software Architect
Rambling Indie Games, LLC
See my professional portfolio
|
|
Back to top |
|
|
Captain Vimes Grumble Teddy
Joined: 12 May 2006 Posts: 225 Location: The City Streets
|
Posted: Wed Aug 26, 2009 7:06 pm Post subject: |
[quote] |
|
Thanks for the tips, guys. I'm trying to implement them right now. I'll let you know how it goes. _________________ "Sometimes it is better to light a flamethrower than to curse the darkness."
- Terry Pratchett
|
|
Back to top |
|
|
|
Page 1 of 1 |
All times are GMT
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|