|
Another question, but it should be simple |
Simsimius
Member #6,017
July 2005
|
EDITED MOST RECENT POST (*THE* MOST RECENT ONE) Sorry for posting for help again, but this one is something I honestly have not learnt before. How would I create multiple instances of a class (e.g bullet), and how would I, written in the class, "delete" the class. I have checked other games to see if I can find out how, but it's not plainly there. ____ |
Derezo
Member #1,666
April 2001
|
Simsimius
Member #6,017
July 2005
|
Thanks for the links. It's a lot to read, and the information on queues seems to be what I'm looking for. I guess the answer isn't as simple as I was hoping for. If anyone can help me out, or at least make things a little clearer, then thanks. Otherwise, I might stick to the usual "one projectile per character" method I usually opt for. ____ |
Derezo
Member #1,666
April 2001
|
Well, the way that I would do it is to use a std::list and do this:
There are 4,890,322 things that should be changed in this code, but I think you might be able to get the idea. "He who controls the stuffing controls the Universe" |
Hrvoje Ban
Member #4,537
April 2004
|
With queue you can only add/remove at the end. For you purpose use std::list, it's good when you need to add/remove lost of objects in the middle. Here's small example in C++: class Bullet { public: Bullet() { bulletList.push_back(this); } ~Bullet() { bulletList.remove(this); } private: std::list<Bullet *> bulletList; };
|
Simsimius
Member #6,017
July 2005
|
Thanks for the examples. They've cleared it up a lot. There's a few questions left though: void Render() { circlefill(screen,x,y,2,makecol(255,0,0)); } void Update() { y -= 2.3f; }
// to cleanup the bullets: list <Bullet *>::iterator i; for(i = bullets.begin(); i != bullets.end() && !bullets.empty();) { delete *i; i = bullets.erase(i); }
The other question is about: Should I worry about them? (As in, what should I do with them, or are they part of the list/queue/thing?)
Sorry for asking, but my knowledge isn't too great. I've gotten the grasp of it, I hope. ____ |
Derezo
Member #1,666
April 2001
|
Quote: The update function changes the bullet location, etc., right? That's right. list::erase() removes a node from the list. "He who controls the stuffing controls the Universe" |
Simsimius
Member #6,017
July 2005
|
EDIT AT BOTTOM! Right. Got it. I'll try to implement it into my game, or at least a test program. Thanks for the help! EDIT:
. It works ____ |
Derezo
Member #1,666
April 2001
|
You'll need to modify that update loop list <Bullet *>::iterator i; for(i = bullets.begin(); i != bullets.end() && !bullets.empty();) { if((*i)->Update()) { (*i)->Render(); i++; } else { delete *i; i = bullets.erase(i); } } You'll also need to create the buffer AFTER setting the graphics mode. Don't use create_bitmap() as the initialization for a global variable. "He who controls the stuffing controls the Universe" |
Ariesnl
Member #2,902
November 2002
|
By the way : switch to OpenLayer it's a lot faster and you won't need that offscreen bitmap (foublebuffering comes free) Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard) |
Simsimius
Member #6,017
July 2005
|
Areisnl: Derezo:
EDIT2: ____ |
Michael Faerber
Member #4,800
July 2004
|
unsigned bullets_on_screen = bullets.size(); -- |
Ariesnl
Member #2,902
November 2002
|
OpenLayer is a "layer" above allegro and OpenGL. It has all kinds of primitives ( line,circle etc) and blitting functions and so on.. just like allegro BUT openLayer routines are hardware accellerated !!! Besides OpenLayer hase some nice things like translucency, timers, lagtime(the time it toook to complete a game cycle), double buffering etc. Here's an overview http://retrospec.sgn.net/~openlayer/ It comes with easy to understand examples. If you use OpenLayer you can still use allegro for everything else ( sound, datastorage etc) Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard) |
Audric
Member #907
January 2001
|
Quote: to limit the number of bullets fired per 3 seconds (still trying to work it out, with no prevail) The player class (or anything that shoots, anyway) could have a shoot_delay" member. This allows to set different delays for different weapons, if needed. void Player::update() { if (shoot_delay > 0) shoot_delay --; // controls if (key[KEY_SPACE] && shoot_delay == 0) { AddBullet(new Bullet(player_x, player_y)); shoot_delay = 20; // no more shooting for some time } } (edit: fixed a ( [ mismatch. I use a lousy screen at work) |
Simsimius
Member #6,017
July 2005
|
Thank you! I was too busy trying to work it out with the existing code! Thanks everyone! ____ |
|