Message Board


Message Board > C/C++ > help with class vectors and stuff

August 10, 2013, 02:53
DoctorN
Whiskered
91 posts
I need some quick programming help with c++. Let me explain first, I have class player and class coin. Class player is defined like so:
Code:
player *player_obj; //the player object

Class coin is defined like so:
Code:
vector<coin> coin_obj; //the coin object


When the player is created, it is created like so:
Code:
player_obj = new (std::nothrow) player( x, y );

it is executed like so:
Code:
player_obj->controls();
player_obj->move( lvlbox );
player_obj->air();
player_obj->show();

and is deleted like so:
Code:
delete player_obj;


When the coin(s) is/are created, it is like so:
Code:
coin_obj.push_back( coin( x, y ) );

executed like so:
Code:
for(std::size_t t = 0; t < coin_obj.size(); ++t)
if( !coin_obj[t].collision( player_obj->get_rects() ) )
coin_obj[t].show();
else
coin_obj.erase(coin_obj.begin()+t);

and deleted/erased like so:
Code:
coin_obj.erase(coin_obj.begin(),coin_obj.begin()+coin_obj.size());


The problem is, when the player collides into the coin, the coin does disappear (so it works for that part), but all the other coins will blink, since I have rechanged the order. Do you know how I can change the way the coins are done so that they won't all blink when one is erased? Thanks
____________
#
August 13, 2013, 23:45
DTM
Earthling!
821 posts

Why do they blink? How is the order changed? I don't understand that.

Mmkay. When you delete one item, you need to decrement the "t" index:

Code:
for(std::size_t t = 0; t < coin_obj.size(); ++t)
{
  if( !coin_obj[t].collision( player_obj->get_rects() ) )
    coin_obj[t].show();
  else
  {
    coin_obj.erase(coin_obj.begin()+t);
    t --;
  }
}


Otherwise the next item will be skipped and not .show()'d

Maybe that's the problem?
____________
:o
#
October 19, 2013, 05:43
DoctorN
Whiskered
91 posts
I fixed it awhile back to your help, never got around to thanking you. Thanks
____________
#

Message Board > C/C++ > help with class vectors and stuff

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!